TreeMap: Sort values of a map with keys moving along with values
By : compazizo
Date : March 29 2020, 07:55 AM
like below fixes the issue i have the following TreeMap: , you can build a TreeSet, that guarantees insertion order: code :
@Test
public void treeMapSortedByValue() {
// given the following map:
TreeMap<Integer, Double> map = new TreeMap<Integer, Double>();
map.put(2, Math.E);
map.put(1, Math.PI);
map.put(3, 42.0);
// build a TreeSet of entries
Set<Map.Entry<Integer, Double>> sortedEntries = new TreeSet<Map.Entry<Integer, Double>>(new DoubleComparator());
sortedEntries.addAll(map.entrySet());
// optionally you can build a List<Double> with the sorted
List<Double> doubles = new LinkedList<Double>();
for (Map.Entry<Integer, Double> entry : sortedEntries) {
doubles.add(entry.getValue());
}
}
class DoubleComparator implements Comparator<Map.Entry<Integer, Double>> {
@Override
public int compare(Entry<Integer, Double> o1, Entry<Integer, Double> o2) {
return Double.compare(o1.getValue(), o2.getValue());
}
}
|
Map column values in TreeMap as key with line number as values
By : Jack Anderson
Date : March 29 2020, 07:55 AM
it helps some times Don't use ArrayList because ArrayList itself is a container to hold your one dimensional line numbers, instead use like below: code :
TreeMap<Integer, ArrayList<Integer>> clusterMap = new TreeMap<Integer, ArrayList<Integer>>();
if(clusterMap.containsKey(num )){
clusterMap.get(num).add(lineNumber);
}
else{
ArrayList<Integer> list = new ArrayList<>();
list.add(lineNumber);
clusterMap.put(num, list);
}
|
subtitude keys and values in TreeMap with other TreeMap in Java
By : user3316879
Date : March 29 2020, 07:55 AM
Hope this helps Assuming I have a TreeMap first; and TreeMap Origin; And each key and each value in first has an equivalent key in origin. How can I substitute key and values of first with related value from Origin? For example , You have to iterate over the entries on your first TreeMap. code :
for(Map.Entry<String, List<String>> entry : first.entrySet()) {
String oldKey= entry.getKey();
List<String> oldValues= entry.getValue();
String newKey = origin.get(oldKey);
List<String> newValues = new ArrayList<>();
for(String s : oldValues) {
newValues.add(origin.get(s));
}
result.put(newKey, newValues);
|
TreeMap floorEntry function not working with integer values(Treemap defined as long)
By : user3680024
Date : March 29 2020, 07:55 AM
it helps some times An IP address is a 32-bit unsigned integer. In Java, ints are 32-bit signed integers. If you use a signed int to represent an IP address, you'll have to accommodate into your code the fact that the upper half of all IP addresses will in fact be negative.
|
Java TreeMap sorting based on values removing duplicate values
By : Anamika
Date : March 29 2020, 07:55 AM
Hope this helps The Comparator of a TreeMap is meant to compare keys. Your workaround here basically tells the map that "red" and "green" are the same key, and thus it dropped one of them. You can look here for how to sort a map by values.
|