商城网站建设快速服务,建筑网课推荐,做网站造假,闵行区天气简单介绍常用的三种Map#xff1a;不足之处#xff0c;欢迎指正#xff01;
HashMap#xff1a;put数据是无序的#xff1b; TreeMap#xff1a;key值按一定的顺序排序#xff1b;数字做key#xff0c;put数据是有序#xff0c;非数字字符串做key#xff0c;put数据…简单介绍常用的三种Map不足之处欢迎指正
HashMapput数据是无序的 TreeMapkey值按一定的顺序排序数字做keyput数据是有序非数字字符串做keyput数据无序 LinkedHashMapLinkedHashMap是有序的且默认为插入顺序
测试案例
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;public class day12 {public static void main(String[] args) {ListString list1 Arrays.asList(黄,河,之,水,天,上,来,奔,流,到,海,不,复,回);ListInteger list2 Arrays.asList(1 , 2 , 3 , 4 , 5 , 6 ,7 , 8 , 9 ,10 , 11 ,12 , 13 , 14);MapString,Object hashMap new HashMap();MapString,Object treeMap new TreeMap();MapString,Object linkedHashMap new LinkedHashMap();MapInteger,Object treeMap2 new TreeMap();for (int i 0; i list1.size(); i) {hashMap.put(list1.get(i), list2.get(i));treeMap.put(list1.get(i), list2.get(i));linkedHashMap.put(list1.get(i), list2.get(i));treeMap2.put(list2.get(i), list1.get(i));}ListString list5 new ArrayList();ListString list6 new ArrayList();IteratorMap.EntryString, Object it hashMap.entrySet().iterator();while(it.hasNext()){Map.EntryString, Object entry it.next();String key entry.getKey();String value String.valueOf(entry.getValue());list5.add(key);list6.add(value);}System.out.println(hashMap:无序案例);System.out.println(hashMap:hashMap);System.out.println(list5ap:list5);System.out.println(list6ap:list6);System.out.println(--------------------------------------------------);ListString list7 new ArrayList();ListString list8 new ArrayList();IteratorMap.EntryString, Object it2 treeMap.entrySet().iterator();while(it2.hasNext()){Map.EntryString, Object entry it2.next();String key entry.getKey();String value String.valueOf(entry.getValue());list7.add(key);list8.add(value);}System.out.println(treeMap:无序案例);System.out.println(treeMap:treeMap);System.out.println(list7ap:list7);System.out.println(list8ap:list8);System.out.println(--------------------------------------------------);ListString list9 new ArrayList();ListString list10 new ArrayList();IteratorMap.EntryString, Object it3 linkedHashMap.entrySet().iterator();while(it3.hasNext()){Map.EntryString, Object entry it3.next();String key entry.getKey();String value String.valueOf(entry.getValue());list9.add(key);list10.add(value);}System.out.println(linkedHashMap:有序案例);System.out.println(linkedHashMap:linkedHashMap);System.out.println(list9dHashMap:list9);System.out.println(list10dHashMap:list10);System.out.println(--------------------------------------------------);ListString list11 new ArrayList();ListString list12 new ArrayList();IteratorMap.EntryInteger,Object it4 treeMap2.entrySet().iterator();while(it4.hasNext()){Map.EntryInteger,Object entry it4.next();Integer key entry.getKey();String value String.valueOf(entry.getValue());list11.add(key.toString());list12.add(value);}System.out.println(treeMap2:有序案例);System.out.println(treeMap2:treeMap2);System.out.println(list11ap:list11);System.out.println(list12ap:list12);}
}
测试输出
hashMap:无序案例
hashMap:{流9, 黄1, 来7, 天5, 上6, 之3, 不12, 复13, 到10, 河2, 水4, 奔8, 海11, 回14}
list5ap:[流, 黄, 来, 天, 上, 之, 不, 复, 到, 河, 水, 奔, 海, 回]
list6ap:[9, 1, 7, 5, 6, 3, 12, 13, 10, 2, 4, 8, 11, 14]
--------------------------------------------------
treeMap:无序案例
treeMap:{上6, 不12, 之3, 到10, 回14, 复13, 天5, 奔8, 来7, 水4, 河2, 流9, 海11, 黄1}
list7ap:[上, 不, 之, 到, 回, 复, 天, 奔, 来, 水, 河, 流, 海, 黄]
list8ap:[6, 12, 3, 10, 14, 13, 5, 8, 7, 4, 2, 9, 11, 1]
--------------------------------------------------
linkedHashMap:有序案例
linkedHashMap:{黄1, 河2, 之3, 水4, 天5, 上6, 来7, 奔8, 流9, 到10, 海11, 不12, 复13, 回14}
list9dHashMap:[黄, 河, 之, 水, 天, 上, 来, 奔, 流, 到, 海, 不, 复, 回]
list10dHashMap:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
--------------------------------------------------
treeMap2:有序案例
treeMap2:{1黄, 2河, 3之, 4水, 5天, 6上, 7来, 8奔, 9流, 10到, 11海, 12不, 13复, 14回}
list11ap:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
list12ap:[黄, 河, 之, 水, 天, 上, 来, 奔, 流, 到, 海, 不, 复, 回]