对HashMap对象的键值对内容进行排序
生活随笔
收集整理的這篇文章主要介紹了
对HashMap对象的键值对内容进行排序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、首先,HashMap集合對象存儲的是無序的鍵值對是不能對HashMa集合對象排序,但是我們可以取出HashMap集合對象的鍵值對內(nèi)容,對這個進行排序。
2、HashMap對象可通entrySet()將鍵值對內(nèi)容取出返回的是Set<Entry<K,V>>集合,然后可以同過ArrayList的構(gòu)造函數(shù)(public ArrayList(Collection<? extends E> c))將set集合轉(zhuǎn)出成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", "數(shù)學(xué)", 20, 40),new Teacher("tea-2", "數(shù)學(xué)", 20, 40),new Teacher("tea-2", "數(shù)學(xué)", 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數(shù)據(jù)printMap(map);/*** map集合內(nèi)容的entry對象放到list集合中。* map集合對象是無序的不能排序,我們可以把map集合對象的 鍵值對內(nèi)容取出來排序*/List<Entry<Student,Teacher>> mapList = new ArrayList<Entry<Student,Teacher>>(map.entrySet()); /** * 1、對HashMap內(nèi)容按key Student對象排序,年齡升序 成績降序* 對List集合中的內(nèi)容進行自定義排序,一種是List集合存放的類型實現(xiàn)Comparable接口,* 第二種實現(xiàn)Comparator接口自定義排序規(guī)則類。因為Entry類已經(jīng)定義好且沒有實現(xiàn)Comparable接口,所以用第二方法* 此處沒有單獨定義排序規(guī)則類,為了方法采用了匿名內(nèi)部類生成排序規(guī)則對象*/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內(nèi)容按key值排序----------------");printEntry(mapList);/*** 2、對HashMap內(nèi)容按value Teacher對象排序,年齡降序 學(xué)生人數(shù)升序*/Collections.sort(mapList,new Comparator<Entry<Student,Teacher>>() {@Overridepublic int compare(Entry<Student, Teacher> o1,Entry<Student, Teacher> o2) {/*** 按年齡降序(if條件和返回值相同) 年齡相同 按學(xué)生人數(shù)升序(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內(nèi)容按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> 驗證結(jié)果:4、其實Map集合中有自帶排序功能的集合TreeMap,它是按key進行排序的,如果key值是基本類型 TreeMap對象就會自動排序 因為基本類型的包裝類都實現(xiàn)了Comparable接口,如果是自定義的類類型,那么該類必須實現(xiàn)Comparable接口,然后按照自定義的排序規(guī)則 自動進行排序。
總結(jié)
以上是生活随笔為你收集整理的对HashMap对象的键值对内容进行排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 栈应用:实现二进制转八进制、十进制、十六
- 下一篇: Wycieczki 线性代数