日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

对HashMap对象的键值对内容进行排序

發布時間:2025/3/15 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对HashMap对象的键值对内容进行排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、首先,HashMap集合對象存儲的是無序的鍵值對是不能對HashMa集合對象排序,但是我們可以取出HashMap集合對象的鍵值對內容,對這個進行排序。

2、HashMap對象可通entrySet()將鍵值對內容取出返回的是Set<Entry<K,V>>集合,然后可以同過ArrayList的構造函數(public ArrayList(Collection<? extends E> c))將set集合轉出成List集合

3、看代碼:

<span style="font-size:14px;">package cn.com.lcx.test;import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set;import cn.com.lcx.model.Student; import cn.com.lcx.model.Teacher;public class MapSort {/*** @param args*/public static void main(String[] args) {Student[] stuArr= {new Student("lwx-1", 15, 60),new Student("lwx-2", 17, 90),new Student("lwx-3", 10, 60),new Student("lwx-4", 25, 50),new Student("lwx-5", 15, 90),new Student("lwx-6", 25, 70)};Teacher[] teaArr = {new Teacher("tea-1", "語文", 50, 100),new Teacher("tea-2", "數學", 20, 40),new Teacher("tea-2", "數學", 20, 40),new Teacher("tea-2", "數學", 20, 40),new Teacher("tea-3", "英語", 20, 60),new Teacher("tea-4", "英語", 80, 10)};HashMap<Student,Teacher> map = new HashMap<Student,Teacher>();/*** 組裝HashMap* key Student對象 value Teacher對象 */for(int i=0;i<stuArr.length;i++){map.put(stuArr[i], teaArr[i]);}System.out.println("HashMap原始排序----------------");//排序前HashMap數據printMap(map);/*** map集合內容的entry對象放到list集合中。* map集合對象是無序的不能排序,我們可以把map集合對象的 鍵值對內容取出來排序*/List<Entry<Student,Teacher>> mapList = new ArrayList<Entry<Student,Teacher>>(map.entrySet()); /** * 1、對HashMap內容按key Student對象排序,年齡升序 成績降序* 對List集合中的內容進行自定義排序,一種是List集合存放的類型實現Comparable接口,* 第二種實現Comparator接口自定義排序規則類。因為Entry類已經定義好且沒有實現Comparable接口,所以用第二方法* 此處沒有單獨定義排序規則類,為了方法采用了匿名內部類生成排序規則對象*/Collections.sort(mapList,new Comparator<Entry<Student,Teacher>>() {@Overridepublic int compare(Entry<Student, Teacher> o1,Entry<Student, Teacher> o2) {/*** 按年齡升序(if條件和返回值相同) 年齡相同 按成績降序(if條件和返回值相反)*/if(o1.getKey().getAge()> o2.getKey().getAge()){return 1;}else if(o1.getKey().getAge()< o2.getKey().getAge()){return -1;}else{if(o1.getKey().getScore()> o2.getKey().getScore()){return -1;}else if(o1.getKey().getScore()< o2.getKey().getScore()){return 1;}else{return 0;}}}});System.out.println("HashMap內容按key值排序----------------");printEntry(mapList);/*** 2、對HashMap內容按value Teacher對象排序,年齡降序 學生人數升序*/Collections.sort(mapList,new Comparator<Entry<Student,Teacher>>() {@Overridepublic int compare(Entry<Student, Teacher> o1,Entry<Student, Teacher> o2) {/*** 按年齡降序(if條件和返回值相同) 年齡相同 按學生人數升序(if條件和返回值相反)*/if(o1.getValue().getAge()> o2.getValue().getAge()){return -1;}else if(o1.getValue().getAge()< o2.getValue().getAge()){return 1;}else{if(o1.getValue().getStuNum()> o2.getValue().getStuNum()){return 1;}else if(o1.getValue().getStuNum()< o2.getValue().getStuNum()){return -1;}else{return 0;}}}});System.out.println("HashMap內容按value值排序----------------");printEntry(mapList);}/*** 打印map集合信息* @param map*/public static void printMap(Map map){//Set<Map.Entry<Student,Teacher>> s = map.entrySet();for(Entry<Student,Teacher> entry : (Set<Entry<Student,Teacher>>)map.entrySet()){System.out.println(entry.getKey()+":"+entry.getValue());}}/*** 打印List集合信息* @param mapList*/public static void printEntry(List<Entry<Student,Teacher>> mapList){//Set<Map.Entry<Student,Teacher>> s = map.entrySet();for(Entry<Student,Teacher> entry : mapList){System.out.println(entry.getKey()+":"+entry.getValue());}}} </span> 驗證結果:

4、其實Map集合中有自帶排序功能的集合TreeMap,它是按key進行排序的,如果key值是基本類型 TreeMap對象就會自動排序 因為基本類型的包裝類都實現了Comparable接口,如果是自定義的類類型,那么該類必須實現Comparable接口,然后按照自定義的排序規則 自動進行排序。

總結

以上是生活随笔為你收集整理的对HashMap对象的键值对内容进行排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。