我应该使用哪个集合在java中存储一个键和三个值?
我尝试存储一个名称和特定视图的相应左、右、上、下维度。我尝试使用hashmap仅存储它(键、值)。请有人告诉我,我应该使用哪一个集合来满足我的要求我应该使用哪个集合在java中存储一个键和三个值?,java,collections,Java,Collections,我尝试存储一个名称和特定视图的相应左、右、上、下维度。我尝试使用hashmap仅存储它(键、值)。请有人告诉我,我应该使用哪一个集合来满足我的要求 for (int i = 0; i < numberOfFaceDetected; i++) { android.media.FaceDetector.Face face = myFace[i]; Log.i("FACE","FACE TAGGING : "+myFace[i].toString()); String
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);
}
for(int i=0;i
我尝试使用hashmap仅存储它(键、值)
是的,但是值可以是任何类型的,对吗??因此,在HashMap
中使用列表作为值。。因此,您可以存储多个值
Map<Integer, List<String>> mapping = new HashMap<Integer, List<String>>();
现在,对存储所有4个值的MyDimension
对象使用一个HashMap
我尝试使用hashmap仅存储它(键、值)
是的,但是值可以是任何类型的,对吗??因此,在HashMap
中使用列表作为值。。因此,您可以存储多个值
Map<Integer, List<String>> mapping = new HashMap<Integer, List<String>>();
现在,对存储所有4个值的MyDimension
对象使用HashMap
。使用映射如何
例如
将您的值分类为类
{
int-dx,dy,dz,dt;
//接球手和接球手
// ...
}
Map Map=newhashmap();
干杯,来张地图怎么样
例如
将您的值分类为类
{
int-dx,dy,dz,dt;
//接球手和接球手
// ...
}
Map Map=newhashmap();
干杯,我更喜欢制作一个表示维度的对象(三个值)
并使用hashmap key=facename value=faceDimension(对象)
为了充实面向对象编程的概念;)
我更喜欢制作一个表示尺寸的对象(三个值)
并使用hashmap key=facename value=faceDimension(对象)
为了充实面向对象编程的概念;)
我认为使用HashMap
是最好的选择 我认为使用HashMap
是最好的选择 我建议您使用,但因为您有4个值对应于一个键(faceName)。
创建一个数据传输对象(DTO
),该对象具有四个属性来承载数据,并将它们放入映射,例如hashMap.put(“myface”,myDto)代码>
比如说,
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
faceMap=newhashmap()代码>
在for循环中
Position facePosition = new Position();
facePosition.setLeft(((int) (myMidPoint.x - myEyesDistance)));
设置所有属性
然后faceMap.put(“myface”,facePosition)代码>我建议您使用,但因为您有4个值对应于一个键(faceName)。
创建一个数据传输对象(DTO
),该对象具有四个属性来承载数据,并将它们放入映射,例如hashMap.put(“myface”,myDto)代码>
比如说,
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
faceMap=newhashmap()代码>
在for循环中
Position facePosition = new Position();
facePosition.setLeft(((int) (myMidPoint.x - myEyesDistance)));
设置所有属性
然后faceMap.put(“myface”,facePosition)代码>+1-完全正确。OP没有充分考虑对象。+1:OP的另一个例子。+1-完全正确。OP没有充分考虑对象。+1:OP in.OP的另一个例子已经尝试使用HashMap。告诉他如何使用它将是一个很好的答案。OP已经尝试使用HashMap。告诉他如何使用它将是一个很好的答案。使位置
不可变(删除setter,使字段
最终
,在构造函数中设置值)可以将代码行减少一半(大致上),并提高可读性(因为您可以确保位置
对象永远不会“就在您下面”更改).但在检索数据时,我们如何获取键(面名称)和值(对应的左、右、上、下位置)?:(调用hashMap.get(“faceKey”),它将返回您输入的值。使位置
不可变(删除setter,使字段
最终
,在构造函数中设置值)可以将代码行对半(大致)并提高可读性(因为您可以确保位置
对象永远不会更改)“就在你下面”)。但是在检索数据时,我们如何获取键(面名称)和值(对应的左、右、上、下位置)?:(调用hashMap.get(“面键”),它将返回你输入的值。这个链接可能也会帮助你…除了使用Bean类之外,还可以使用多重映射。@Naveen:多重映射是(从概念上讲,地图
。这与要求不符,更接近于地图
(但即使这样也会是错误的;安德斯的回答证明了正确的方法)。@joachim sauer:我完全同意你的观点……我分享它是为了提供相关信息……因此,在评论中提供了……)这个链接也可能对您有所帮助……除了使用Bean类之外,还可以使用Multimaps。@Naveen:Multimap(概念上)是一个Map
。这与要求不符,它更接近Map
(但即使这样也会错;Anders的回答说明了正确的方法)@joachim sauer:我完全同意你的看法……我分享它是为了提供相关信息……因此,在评论中提供……)