1

I am trying to store one name and corresponding left,right,top,bottom dimensions of particular view .I tried with hashmap its storing only (key, value). Please someone tell me which Collection should i use to fulfill my requirement.

for (int i = 0; i < numberOfFaceDetected; i++) {
    android.media.FaceDetector.Face face = myFace[i];
    Log.i("FACE","FACE TAGGING   : "+myFace[i].toString());
    String facename = myFace[i].toString();
    PointF myMidPoint = new PointF();
    face.getMidPoint(myMidPoint);
    myEyesDistance = face.eyesDistance();
    dx = (int) (myMidPoint.x - myEyesDistance);
    dy = (int) (myMidPoint.y - myEyesDistance);
    dz = (int) (myMidPoint.x + myEyesDistance);
    dt = (int) (myMidPoint.y + myEyesDistance);
    //here i want to store facename,dx,dy,dz,dt values in  same  collection
    canvas.drawRect((int) dx, dy, dz, dt, myPaint);
}
3
  • This link may help you too... stackoverflow.com/questions/4109167/… Apart from using a Bean class, Multimaps can also be used. Commented Oct 8, 2012 at 12:57
  • @Naveen: a Multimap is (conceptually) a Map<K,Set<V>>. That would not match the requirement, which is closer to Map<K,List<V>> (but even that would be wrong; the answer by Anders demonstrates the correct approach). Commented Oct 8, 2012 at 13:07
  • @joachim sauer: i totally agree with you.... I shared it for giving related information... Thus, provided in comment... :) Commented Oct 8, 2012 at 13:40

5 Answers 5

5

How about a Map<YourKeyClass, YourValuesClass>?

Eg.

class YourValuesClass
{
    int dx, dy, dz, dt;

    // getters and setters
    // ...
}

Map<String,YourValueClass> map = new HashMap<String,YourValueClass>();

Cheers,

Sign up to request clarification or add additional context in comments.

2 Comments

+1 - exactly. The OP is not thinking in terms of objects to a sufficient degree.
+1: another case of an OP in object denial.
1

i prefer making an object representing the dimensions (three values) and use hashmap key=facename value=faceDimension (your object) in order to fullfill the concept of object oriented programming ;)

public class FaceDimension {
    private int dx;
    private int dy;
    private int dz;
    private int dt;

    public FaceDimension(int dx, int dy, int dz, int dt) {
        super();
        this.dx = dx;
        this.dy = dy;
        this.dz = dz;
        this.dt = dt;
    }
}

Comments

1

I suggest you to use HashMap, but as you have 4 values corresponding to a key(faceName). Create a Data Transfer Object (DTO) with having four properties to carry the data and put them to the map such as hashMap.put("myface",myDto);

For example,

class Position
{
    private int left;
    private int right;
    private int top;
    private int bottom;
    public int getLeft() {
        return left;
    }
    public void setLeft(int left) {
        this.left = left;
    }
    public int getRight() {
        return right;
    }
    public void setRight(int right) {
        this.right = right;
    }
    public int getTop() {
        return top;
    }
    public void setTop(int top) {
        this.top = top;
    }
    public int getBottom() {
        return bottom;
    }
    public void setBottom(int bottom) {
        this.bottom = bottom;
    }


}

Map<String, Position> faceMap = new HashMap<String, Position>();

Inside your for loop,

Position facePosition = new Position();

facePosition.setLeft(((int) (myMidPoint.x - myEyesDistance)));

set all the properties then faceMap.put("myface",facePosition);

3 Comments

Making Position immutable (remove setters, make fields final, set values in constructor) would could the lines of code in half (roughly) and improve the readability (since you can be sure that a Position object never changes "right under you").
But at the time of retrieving the data ,how we can get the key(face name) ,and value(corresponding left,right,top,bottom positions)? :(
call the hashMap.get("faceKey"), it will return you the value which you putted.
1

I think using a HashMap would be your best bet.

1 Comment

OP already tried using a HashMap. Telling him how to use it would be a good answer.
0

I tried with hashmap its storing only (key, value)

Yeah, but that value can be of any type right?? So, use a List as a value in your HashMap.. So, you can store multiple values..

Map<Integer, List<String>> mapping = new HashMap<Integer, List<String>>();

I have assumed type of key to be Integer and type of values to String.. You can use appropriate type..

Or if you have 4-fixed values, that I just saw you have.. You can create your Custom Type.. just have a class containing all those values, and use HashMap<Integer, YourCustomClass>

public class MyDimension {
    private int top; 
    private int bottom;
    private int right;
    private int left;

    // Your Constructor

    // Use only getters, to make the object immutable..
}

Now, use a HashMap<Integer, MyDimension> with your MyDimension object storing all the 4 values..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.