I have a list with HashMap<Integer, Point3d>
format like in the following way.
{
{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()
In the same way I want to implement for 2nd and 3rd elements of the Points
.
I tried allot but I didn't find anyway.because we can't able to retrieve values from HashMap
based on index
. we can able to retrieve values based on Key value
But I don't know the 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,
HashMap
into a Set with your custom Comparator . 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());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.