我们以a的形式获得了学生得分的详细信息 哈希图 ,其中学生的名字是关键,分数是价值。我们的任务是根据关键值对地图进行排序,即按照字母(词典)顺序排列学生的姓名。 例如:
Input : Key = Jayant, Value = 80 Key = Anushka, Value = 80 Key = Amit, Value = 75 Key = Abhishek, Value = 90 Key = Danish, Value = 40Output : Sorted Map according to Names: Key = Abhishek, Value = 90 Key = Amit, Value = 75 Key = Anushka, Value = 80 Key = Danish, Value = 40 Key = Jayant, Value = 80
使用TreeMap(putAll方法)
其想法是将HashMap的所有数据放入 树图 .树形图如下 红黑树 基于应用的实现。地图根据其键的自然顺序进行排序。 点击这里获取更多信息 .
JAVA
// Java Code to sort Map by key value import java.util.*; class sortmapKey { // This map stores unsorted values static Map<String, Integer> map = new HashMap<>(); // Function to sort map by Key public static void sortbykey() { // TreeMap to store values of HashMap TreeMap<String, Integer> sorted = new TreeMap<>(); // Copy all data from hashMap into TreeMap sorted.putAll(map); // Display the TreeMap which is naturally sorted for (Map.Entry<String, Integer> entry : sorted.entrySet()) System.out.println( "Key = " + entry.getKey() + ", Value = " + entry.getValue()); } // Driver Code public static void main(String args[]) { // putting values in the Map map.put( "Jayant" , 80 ); map.put( "Abhishek" , 90 ); map.put( "Anushka" , 80 ); map.put( "Amit" , 75 ); map.put( "Danish" , 40 ); // Calling the function to sortbyKey sortbykey(); } } |
Key = Abhishek, Value = 90Key = Amit, Value = 75Key = Anushka, Value = 80Key = Danish, Value = 40Key = Jayant, Value = 80
注: TreeMap提供了有保证的 日志(n) 时间成本 康纳斯基 , 收到 , 放 并删除操作。
使用树映射(构造函数)
JAVA
// Java Code to sort Map by key value import java.util.*; class sortmapKey { // This map stores unsorted values static Map<String, Integer> map = new HashMap<>(); // Function to sort map by Key public static void sortbykey() { // TreeMap to store values of HashMap TreeMap<String, Integer> sorted = new TreeMap<>(map); // Display the TreeMap which is naturally sorted for (Map.Entry<String, Integer> entry : sorted.entrySet()) System.out.println( "Key = " + entry.getKey() + ", Value = " + entry.getValue()); } // Driver Code public static void main(String args[]) { // putting values in the Map map.put( "Jayant" , 80 ); map.put( "Abhishek" , 90 ); map.put( "Anushka" , 80 ); map.put( "Amit" , 75 ); map.put( "Danish" , 40 ); // Calling the function to sortbyKey sortbykey(); } } |
Key = Abhishek, Value = 90Key = Amit, Value = 75Key = Anushka, Value = 80Key = Danish, Value = 40Key = Jayant, Value = 80
使用ArrayList
在这种方法中,我们使用 ArrayList 建造师。然后我们使用 收藏。排序() 方法
JAVA
// Java Code to sort Map by key value import java.util.*; class sortmapKey { // This map stores unsorted values static Map<String, Integer> map = new HashMap<>(); // Function to sort map by Key public static void sortbykey() { ArrayList<String> sortedKeys = new ArrayList<String>(map.keySet()); Collections.sort(sortedKeys); // Display the TreeMap which is naturally sorted for (String x : sortedKeys) System.out.println( "Key = " + x + ", Value = " + map.get(x)); } // Driver Code public static void main(String args[]) { // putting values in the Map map.put( "Jayant" , 80 ); map.put( "Abhishek" , 90 ); map.put( "Anushka" , 80 ); map.put( "Amit" , 75 ); map.put( "Danish" , 40 ); // Calling the function to sortbyKey sortbykey(); } } |
Key = Abhishek, Value = 90Key = Amit, Value = 75Key = Anushka, Value = 80Key = Danish, Value = 40Key = Jayant, Value = 80
使用Java8Lambdas
在这里,我们将改变我们如何进行排序,并将使用 lambda表达式 用于分类。逻辑是相同的,即使我们也传递了comparator对象,但只使用lambda。
以下是上述方法的实施情况:
JAVA
// Java Code to sort Map by key value import java.util.*; class sortmapKey { // This map stores unsorted key static Map<String, Integer> map = new HashMap<>(); // function to sort hashmap by keys public static Map<String, Integer> sortByKey(Map<String, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<String, Integer> > list = new LinkedList<Map.Entry<String, Integer> >( hm.entrySet()); // Sort the list using lambda expression Collections.sort( list, (i1, i2) -> i1.getKey().compareTo(i2.getKey())); // put data from sorted list to hashmap HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>(); for (Map.Entry<String, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } // Driver Code public static void main(String args[]) { // putting values in the Map map.put( "Jayant" , 80 ); map.put( "Abhishek" , 90 ); map.put( "Anushka" , 80 ); map.put( "Amit" , 75 ); map.put( "Danish" , 40 ); // Calling the function to sortbyKey Map<String, Integer> hm1 = sortByKey(map); // print the sorted hashmap for (Map.Entry<String, Integer> en : hm1.entrySet()) { System.out.println( "Key = " + en.getKey() + ", Value = " + en.getValue()); } } } |
Key = Abhishek, Value = 90Key = Amit, Value = 75Key = Anushka, Value = 80Key = Danish, Value = 40Key = Jayant, Value = 80
使用Java8流
在这里,我们将使用流对地图进行排序。我们将使用 流() 方法获取入口集流,后跟 排序() 方法对流进行排序,最后,我们将使用 toMap() 方法在toMap()方法中,我们使用 LinkedHashMap::新建 方法参考 以保留地图的排序顺序。
JAVA
// Java Code to sort Map by key value import static java.util.stream.Collectors.*; import java.lang.*; import java.util.*; import java.util.stream.*; import java.util.stream.Collectors; class sortmapKey { // This map stores unsorted values static Map<String, Integer> map = new HashMap<>(); // Function to sort map by Key public static void sortbykey() { HashMap<String, Integer> temp = map.entrySet() .stream() .sorted((i1, i2) -> i1.getKey().compareTo( i2.getKey())) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap:: new )); // Display the HashMap which is naturally sorted for (Map.Entry<String, Integer> entry : temp.entrySet()) { System.out.println( "Key = " + entry.getKey() + ", Value = " + entry.getValue()); } } // Driver Code public static void main(String args[]) { // putting values in the Map map.put( "Jayant" , 80 ); map.put( "Abhishek" , 90 ); map.put( "Anushka" , 80 ); map.put( "Amit" , 75 ); map.put( "Danish" , 40 ); // Calling the function to sortbyKey sortbykey(); } } |
Key = Abhishek, Value = 90Key = Amit, Value = 75Key = Anushka, Value = 80Key = Danish, Value = 40Key = Jayant, Value = 80
本文由 丹麦卡里姆 和 阿纳夫·K·曼达尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。