简体   繁体   English

如何从hashMap查找所有第一个值

[英]How to find all the first values from hashMap

I have a list with HashMap<Integer, Point3d> format like in the following way. 我有一个HashMap<Integer, Point3d>格式的列表,如下所示。

 {
   {key1,(x1,y1,z1)},
   {key2,(x2,y2,z2)},
   {key3,(x1,y3,z3)},
   {key4,(x1,y4,z4)},
   {key5,(x5,y5,z5)},
   ..
 }

First I want to seperate first elements from all the points like {x1,x2,x1,x1,x5} and then remove duplicates like {x1,x2,x5} finally length of the result means {x1,x2,x5}.size() 首先,我想将所有元素中的第一个元素与{x1,x2,x1,x1,x5}分开,然后删除诸如{x1,x2,x5}类的重复元素{x1,x2,x5}最后结果的长度意味着{x1,x2,x5}.size()

In the same way I want to implement for 2nd and 3rd elements of the Points . 以同样的方式,我想为Points第二和第三元素实现。

I tried allot but I didn't find anyway.because we can't able to retrieve values from HashMap based on index . 我尝试了分配,但还是没有找到。因为我们无法基于indexHashMap检索值。 we can able to retrieve values based on Key value But I don't know the Keyvalues hashMap. 我们可以基于Key value检索值,但我不知道Keyvalues hashMap。

I tried in the following way. 我尝试了以下方式。

  public int xSideLength(HashMap<Integer, Point3d> coOrdinates) {
    int length=0;
    Set<Integer> keyValues=coOrdinates.keySet();
    Integer[] array=(Integer[]) keyValues.toArray();
    for (int index = 0; index < array.length; index++) {
        //logical code
    }
    return length;
}

Can anyone suggest me. 谁能建议我。

What do you think of the following: 您如何看待以下方面:

Set<Integer> values = new HashSet<Integer>();
for(Point3d point : coordinates.values()) {
    values.add(point.x());
}

return values.size();

You can follow the following steps, 您可以按照以下步骤操作,

  1. Put the values of the HashMap into a Set with your custom Comparator . 使用自定义ComparatorHashMap的值放入Set中。
  2. Implement your compare method. 实现您的比较方法。
  3. Return the set size. 返回设置的大小。

The snippet looks like below, 该代码段如下所示,

public int xSideLength(HashMap<Integer, Point3d> coOrdinates) {
    int length=0;
    Set<Point3d> values = new TreeSet<Point>(new Comparator<Point3d>() {
        @Override
        public int compare(Point3d e1, Point3d e2) {
            return e1.getX().compareTo(e2.getX());
        }
    });
    values.addAll(coOrdinates.values());
    return values.size();
}

You can get elements from the map using the key like this: 您可以使用以下键从地图中获取元素:

Set<Integer> keyValues = coOrdinates.keySet();
for (Integer key : keyValues) {
    Point3d point = coOrdinates.get(key);   
    // Your logical code goes here.
}
public static void zz() {
    Map<String,Point3d> m = new HashMap<String,Point3d>();

    m.put("k1", new Point3d(1.0, 1.0, 4.0));
    m.put("k2", new Point3d(2.0, 2.0, 2.0));
    m.put("k3", new Point3d(1.0, 3.0, 2.0));
    m.put("k4", new Point3d(1.0, 3.0, 4.0));
    m.put("k5", new Point3d(5.0, 3.0, 2.0));

    Set xvals = new HashSet();
    Set yvals = new HashSet();
    Set zvals = new HashSet();

    double[] coords = new double[3];
    for (Point3d p : m.values()) {
        p.get(coords);
        xvals.add(coords[0]);
        yvals.add(coords[1]);
        zvals.add(coords[2]);
    }

    System.out.println("# unique x: " + xvals.size() + ": " + xvals);
    System.out.println("# unique y: " + yvals.size() + ": " + yvals);
    System.out.println("# unique z: " + zvals.size() + ": " + zvals);
    }
for (Map.Entry<Integer, Point3d> entry : coOrdinates.entrySet()) {
    Point3d point = entry.getValue());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM