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);
}
Baz
  • 36,440
  • 11
  • 68
  • 94
user369932
  • 271
  • 5
  • 19
  • This link may help you too... http://stackoverflow.com/questions/4109167/how-can-i-create-a-multimap-in-java-on-android Apart from using a Bean class, Multimaps can also be used. – NamingException Oct 08 '12 at 12:57
  • @Naveen: a Multimap is (conceptually) a `Map>`. That would not match the requirement, which is closer to `Map>` (but even that would be wrong; the answer by Anders demonstrates the correct approach). – Joachim Sauer Oct 08 '12 at 13:07
  • @joachim sauer: i totally agree with you.... I shared it for giving related information... Thus, provided in comment... :) – NamingException Oct 08 '12 at 13:40

5 Answers5

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,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
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;
    }
}
schoeggii
  • 149
  • 3
  • 11
1

I think using a HashMap would be your best bet.

Baz
  • 36,440
  • 11
  • 68
  • 94
  • 2
    OP already tried using a HashMap. Telling him **how** to use it would be a good answer. – Baz Oct 08 '12 at 11:56
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);

subodh
  • 6,136
  • 12
  • 51
  • 73
  • 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"). – Joachim Sauer Oct 08 '12 at 12:17
  • But at the time of retrieving the data ,how we can get the key(face name) ,and value(corresponding left,right,top,bottom positions)? :( – user369932 Oct 09 '12 at 04:37
  • call the hashMap.get("faceKey"), it will return you the value which you putted. – subodh Oct 10 '12 at 04:20
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..

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525