Sort by values ​​in a HashMap class using Java

I am trying to get HashMap results sorted by value.

These are the keys and values ​​of the HashMap:

map.put("ertu", 5); map.put("burak", 4); map.put("selin", 2); map.put("can", 1); 

I am trying to get these results:

 1 = can 2 = selin 4 = burak 5 = ertu 

Here is my code:

 import java.util.*; public class mapTers { public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("ertu", 5); map.put("burak", 4); map.put("selin", 2); map.put("can", 1); Integer dizi[] = new Integer[map.size()]; Set anahtarlar = map.keySet(); Iterator t = anahtarlar.iterator(); int a = 0; while (t.hasNext()) { dizi[a] = map.get(t.next()); a++; } Arrays.sort(dizi); for (int i = 0; i < map.size(); i++) { while (t.hasNext()) { if (dizi[i].equals(map.get(t.next()))) { System.out.println(dizi[i] + " = " + t.next()); } } } } } 
+4
source share
5 answers

Each time you call t.next (), the iterator pointer moves forward. In the end, the iterator reaches the end. You need to reset the iterator. In addition, calling t.next () moves the pointer twice twice.

Here is my solution:

 import java.util.*; public class mapTers { public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("ertu", 5); map.put("burak", 4); map.put("selin", 2); map.put("can", 1); Integer dizi[] = new Integer[map.size()]; Set anahtarlar = map.keySet(); Iterator t = anahtarlar.iterator(); int a = 0; while (t.hasNext()) { dizi[a] = map.get(t.next()); a++; } Arrays.sort(dizi); for (int i = 0; i < map.size(); i++) { t = anahtarlar.iterator(); while (t.hasNext()) { String temp = (String)t.next(); if (dizi[i].equals(map.get(temp))) { System.out.println(dizi[i] + " = " + temp); } } } } } 
+1
source

You can sort the entries as follows (but note that this will not sort the map itself, and also the HashMap cannot be sorted) -

 List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet()); Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return o1.getValue().compareTo(o2.getValue()); } }); 
+2
source

You cannot do this with Map . At least not directly.

Get keys / records, get all map data in a more suitable structure (hint: a class that encapsulates both attributes and is stored in a sortable one (hint2: SortedSet , List )) and sorts.

Remember to extend Comparable (and implement compareTo ) or, otherwise, create a Comparator .

+1
source

This is one solution: fooobar.com/questions/23042 / ...

Just go to the unsorted card and you will get the sorted one.

 private static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap, final boolean order) { List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet()); // Sorting the list based on values Collections.sort(list, new Comparator<Entry<String, Integer>>() { public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { if (order) { return o1.getValue().compareTo(o2.getValue()); } else { return o2.getValue().compareTo(o1.getValue()); } } }); // Maintaining insertion order with the help of LinkedList Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); for (Entry<String, Integer> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } 

To print, do a simple iteration over a set of records:

 public static void printMap(Map<String, Integer> map) { for (Entry<String, Integer> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : "+ entry.getValue()); } } 
0
source

You probably have the wrong data structure for this problem. Or:

  • Draw a card, so integers are keys and words of meaning and make a card a SortedMap , or
  • Use the bidirectional map provided by libraries such as Google Guava.

Inverted card

 private final SortedMap<Integer, String> TRANSLATIONS; static { SortedMap<Integer, String> map = new TreeMap<>(); map.put(1, "can"); // ... TRANSLATIONS = Collections.unmodifiableSortedMap(map); } 

Guava bimap

 private final BiMap TRANSLATIONS = new ImmutableBiMap.Builder<String, Integer>() .put("ertu", 5); .put("burak", 4); .put("selin", 2); .put("can", 1); .build(); 

Then, iterate over the sorted version of the set of keys or values ​​set as needed. For instance,

 TRANSLATIONS.inverse.get(4); // "burak" 

I'm just curious. What languages ​​are your lines in?

0
source

Source: https://habr.com/ru/post/1486137/


All Articles