大家好,感謝邀請,今天來為大家分享一下java的問題,以及和對map進行排序的一些困惑,大家要是還不太明白的話,也沒有關系,因為接下來將為大家分享,希望可以幫助到大家,解決大家的問題,下面就開始吧!
java中需要排序的數據,為什么都是用hashmap,而不是直接用sortmap
sortmap每次添加數據的時候都會進行排序運算,在數據節點非常多的情況下,會嚴重影響系統性能。所以更多的用性能好的hashmap,需要排序的時候進行一次排序運算。在數據節點小或者對性能要求不高的情況下,sortmap也是一個不錯的選擇。
南京萬和Java培訓分享Java高頻面試題—如何對HashMap按鍵值排序
Java中HashMap是一種用于存儲“鍵”和“值”信息對的數據結構。不同于Array、ArrayList和LinkedLists,它不會維持插入元素的順序。
1.HashMap存儲每對鍵和值作為一個Entry<K,V>對象。例如,給出一個HashMap,
[html]viewplaincopyprint?
Map<String,Integer>aMap=newHashMap<String,Integer>();
鍵的每次插入,都會有值對應到散列映射上,生成一個Entry<K,V>對象。通過使用這個Entry<K,V>對象,我們可以根據值來排序HashMap。
2.創建一個簡單的HashMap,并插入一些鍵和值。
[java]viewplaincopyprint?
Map<String,Integer>aMap=newHashMap<String,Integer>();
//addingkeysandvalues
aMap.put("Five",5);
aMap.put("Seven",7);
aMap.put("Eight",8);
aMap.put("One",1);
aMap.put("Two",2);
aMap.put("Three",3);
3.從HashMap恢復entry集合,如下所示。
[html]viewplaincopyprint?
Set<Entry<String,Integer>>mapEntries=aMap.entrySet();
4.從上述mapEntries創建LinkedList。我們將排序這個鏈表來解決順序問題。我們之所以要使用鏈表來實現這個目的,是因為在鏈表中插入元素比數組列表更快。
[java]viewplaincopyprint?
List<Entry<String,Integer>>aList=newLinkedList<Entry<String,Integer>>(mapEntries);
5.通過傳遞鏈表和自定義比較器來使用Collections.sort()方法排序鏈表。
[java]viewplaincopyprint?
Collections.sort(aList,newComparator<Entry<String,Integer>>(){
@Override
publicintcompare(Entry<String,Integer>ele1,
Entry<String,Integer>ele2){
returnele1.getValue().compareTo(ele2.getValue());
}
});
6.使用自定義比較器,基于entry的值(Entry.getValue()),來排序鏈表。
7.ele1.getValue().compareTo(ele2.getValue())——比較這兩個值,返回0——如果這兩個值完全相同的話;返回1——如果第一個值大于第二個值;返回-1——如果第一個值小于第二個值。
8.Collections.sort()是一個內置方法,僅排序值的列表。它在Collections類中重載。這兩種個方法是
[java]viewplaincopyprint?
publicstatic<TextendsComparable<?superT>>voidsort(List<T>list)
publicstatic<T>voidsort(List<T>list,Comparator<?superT>c)
9.現在你已經排序鏈表,我們需要存儲鍵和值信息對到新的映射中。由于HashMap不保持順序,因此我們要使用LinkedHashMap。
[java]viewplaincopyprint?
Map<String,Integer>aMap2=newLinkedHashMap<String,Integer>();
for(Entry<String,Integer>entry:aList){
aMap2.put(entry.getKey(),entry.getValue());
}
10.完整的代碼如下。
[java]viewplaincopyprint?
packagecom.speakingcs.maps;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.HashMap;
importjava.util.LinkedHashMap;
importjava.util.LinkedList;
importjava.util.List;
importjava.util.Map;
importjava.util.Map.Entry;
importjava.util.Set;
publicclassSortMapByValues{
publicstaticvoidmain(String[]args){
Map<String,Integer>aMap=newHashMap<String,Integer>();
//addingkeysandvalues
aMap.put("Five",5);
aMap.put("Seven",7);
aMap.put("Eight",8);
aMap.put("One",1);
aMap.put("Two",2);
aMap.put("Three",3);
sortMapByValues(aMap);
}
privatestaticvoidsortMapByValues(Map<String,Integer>aMap){
Set<Entry<String,Integer>>mapEntries=aMap.entrySet();
System.out.println("ValuesandKeysbeforesorting");
for(Entry<String,Integer>entry:mapEntries){
System.out.println(entry.getValue()+"-"+entry.getKey());
}
//usedlinkedlisttosort,becauseinsertionofelementsinlinkedlistisfasterthananarraylist.
List<Entry<String,Integer>>aList=newLinkedList<Entry<String,Integer>>(mapEntries);
//sortingtheList
Collections.sort(aList,newComparator<Entry<String,Integer>>(){
@Override
publicintcompare(Entry<String,Integer>ele1,
Entry<String,Integer>ele2){
returnele1.getValue().compareTo(ele2.getValue());
}
});
//StoringthelistintoLinkedHashMaptopreservetheorderofinsertion.
Map<String,Integer>aMap2=newLinkedHashMap<String,Integer>();
for(Entry<String,Integer>entry:aList){
aMap2.put(entry.getKey(),entry.getValue());
}
//printingvaluesaftersoringofmap
System.out.println("Value"+"-"+"Key");
for(Entry<String,Integer>entry:aMap2.entrySet()){
System.out.println(entry.getValue()+"-"+entry.getKey());
}
}
}
java中的冒泡排序
publicvoidbubbleSort(int[]data,StringsortType){
if(sortType.equals("asc")){//正排序,從小排到大
//比較的輪數
for(inti=1;i<data.length;i++){
//將相鄰兩個數進行比較,較大的數往后冒泡
for(intj=0;j<
data.length
-i;j++){
if(data[j]>data[j+1]){
//交換相鄰兩個數
swap(data,j,j+1);
}
}
}
}elseif(sortType.equals("desc")){//倒排序,從大排到小
//比較的輪數
for(inti=1;i<data.length;i++){
//將相鄰兩個數進行比較,較大的數往后冒泡
for(intj=0;j<
data.length
-i;j++){
if(data[j]<data[j+1]){
//交換相鄰兩個數
swap(data,j,j+1);
}
}
}
}else{
System.out.println("您輸入的排序類型錯誤!");
}
printArray(data);//輸出冒泡排序后的數組值
}
java中map和list的區別
1、Java中的集合包括三大類,它們是Set、List和Map,它們都處于java.util包中,Set、List和Map都是接口,它們有各自的實現類。Set的實現類主要有HashSet和TreeSet,List的實現類主要有ArrayList,Map的實現類主要有HashMap和TreeMap。
2、List中的對象按照索引位置排序,可以有重復對象,允許按照對象在集合中的索引位置檢索對象,如通過list.get(i)方式來獲得List集合中的元素。List是有序的Collection,使用此接口能夠精確的控制每個元素插入的位置。用戶能夠使用索引(元素在List中的位置,類似于數組下標)來訪問List中的元素,這類似于Java的數組。List允許有相同的元素。實現List接口的常用類有LinkedList,ArrayList,Vector和Stack。
3、Map中的每一個元素包含一個鍵對象和值對象,它們成對出現。鍵對象不能重復,值對象可以重復。Map提供key到value的映射。一個Map中不能包含相同的key,每個key只能映射一個value。Map接口提供3種集合的視圖,Map的內容可以被當作一組key集合,一組value集合,或者一組key-value映射。
javaset集合的值可以排序嗎
Set集合的排序我們知道,Set集合是無序的,可以使用TreeSet類,那么TreeSet進行排序的規則是怎樣的呢?1TreeSet支持兩種排序方式,自然排序和定制排序,在默認情況下,TreeSet采用自然排序.自然排序:TreeSet會調用集合元素的compareTo(Objectobj)方法來比較元素之間的大小關系,然后將集合的元素按升序排列,這種方式就是自然排序.為什么集合元素有compareTo方法,因為集合元素對象實現了Comparable接口,該方法返回一個整數值,當一個對象調用該方法與另一個對象進行比較,例如:obj1.compareTo(obj2)如果返回0,表示這兩個對象相等,如果該方法返回一個正整數,表示obj1大于obj2如果該方法返回一個負整數,表示obj1小于obj2所以需要使用TreeSet集合進行自然排序,元素必須實現Comparable接口,但是Java一些常用的類已經實現了該接口,例如:StringCharacterBooleanDateTimeBigDecimalBigInteger等如:TreeSet<String>ts=newTreeSet<String>();ts.add("b");ts.add("c");ts.add("a");System.out.println(ts);結果:abc
關于java和對map進行排序的介紹到此就結束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關注本站。