Java基礎學習筆記(十五)—— java中的常用容器Set與Map

Java基礎學習筆記(十五)—— java中的常用容器Set與Map

People can't do something by themselves; they wanna tell you you can not do it.

| @Author:TTODS


Set

Set集合繼承了 Collection接口,與List的區別是,Set中的元素是無序的,且不能包含重複值。

HashSet的常用方法

構造方法
HashSet()

構造一個新的空集合; HashMap實例具有默認初始容量(16)和負載因子(0.75)。

HashSet(Collection<? extends E> c)

構造一個包含指定集合中的元素的新集合。

HashSet<String>  myHashSet = new HashSet<>(Arrays.asList("one","two","three","four"));
System.out.println(myHashSet); //[four, one, two, three]
HashSet(int initialCapacity)

構造一個新的空集合; 實例具有指定的初始容量和默認負載因子(0.75)。

HashSet(int initialCapacity, float loadFactor)

構造一個新的空集合; 實例具有指定的初始容量和指定的負載因子。

常用方法
add(E e)

將指定的元素添加到此集合(如果尚未存在)。返回boolean值,添加成功返回true,否則,false.

HashSet<String>  myHashSet = new HashSet<>(Arrays.asList("one","two","three","four"));
System.out.println(myHashSet.add("one"));//false
System.out.println(myHashSet);//[four, one, two, three]
System.out.println(myHashSet.add("five"));//true
System.out.println(myHashSet);//[four, one, two, three, five]
clear()

從此集合中刪除所有元素。

clone()

返回此 HashSet實例的淺層副本:元素本身不被克隆。

contains(Object o)

如果此集合包含指定的元素,則返回 true 。

isEmpty()

如果此集合不包含元素,則返回 true 。

HashSet<String>  myHashSet = new HashSet<>(Arrays.asList("one","two","three","four"));
System.out.println(myHashSet.isEmpty());//false
System.out.println(myHashSet);//[four, one, two, three]
myHashSet.clear();
System.out.println(myHashSet);//[]
System.out.println(myHashSet.isEmpty());//true
iterator()

返回此集合中元素的迭代器。

remove(Object o)

如果存在,則從該集合中刪除指定的元素。

size()

返回此集合中的元素數(其基數)。

Map

Map(映射)集合表示一種非常複雜的集合,允許按照某個鍵來訪問元素。Map集合是由兩個集合構成的,一個是鍵(key)集合,一個是值(value)集合。鍵集合是Set類型,因此不能有重複的元素。而值集合是Collection類型,可以有重複的元素。Map集合中的鍵和值是成對出現的。HashMap是常用的Map實現類。

HashMap的常用方法

構造方法
HashMap()

構造一個空的 HashMap ,默認初始容量(16)和默認負載係數(0.75)。

HashMap(int initialCapacity)

構造一個空的 HashMap具有指定的初始容量和默認負載因子(0.75)。

HashMap(int initialCapacity, float loadFactor)

構造一個空的 HashMap具有指定的初始容量和負載因子。

HashMap(Map<? extends K,? extends V> m)

構造一個新的 HashMap與指定的相同的映射 Map 。

HashMap<Integer,Character> myHashMap = new HashMap<>();
myHashMap.put(1,'A');
myHashMap.put(2,'B');
myHashMap.put(3,'C');
myHashMap.put(4,'D');
System.out.println(myHashMap);//{1=A, 2=B, 3=C, 4=D}
HashMap<Integer,Character> myHashMap1 = new HashMap<>(myHashMap);
System.out.println(myHashMap1);//{1=A, 2=B, 3=C, 4=D}
常用方法
clear()

從這張地圖中刪除所有的映射。

clone()

返回此 HashMap實例的淺拷貝:鍵和值本身不被克隆。

containsKey(Object key)

如果此映射包含指定鍵的映射,則返回 true 。

HashMap<Integer,Character> myHashMap = new HashMap<>();
myHashMap.put(1,'A');
myHashMap.put(2,'B');
myHashMap.put(3,'C');
myHashMap.put(4,'D');
System.out.println(myHashMap);//{1=A, 2=B, 3=C, 4=D}
System.out.println(myHashMap.containsKey((Integer)1));//true
System.out.println(myHashMap.containsKey((Integer)5));//false
containsValue(Object value)

如果此地圖將一個或多個鍵映射到指定值,則返回 true 。

HashMap<Integer,Character> myHashMap = new HashMap<>();
myHashMap.put(1,'A');
myHashMap.put(2,'B');
myHashMap.put(3,'C');
myHashMap.put(4,'D');
System.out.println(myHashMap);
System.out.println(myHashMap.containsValue((Character)'D'));//true
System.out.println(myHashMap.containsValue((Character)'E'));//false
get(Object key)

通過key來獲取Map中與key對應的值,若Map中不存在該key,則返回null

HashMap<Integer,Character> myHashMap = new HashMap<>();
myHashMap.put(1,'A');
myHashMap.put(2,'B');
myHashMap.put(3,'C');
myHashMap.put(4,'D');
myHashMap.put(5,null);
System.out.println(myHashMap);//{1=A, 2=B, 3=C, 4=D, 5=null}
Character ch = myHashMap.get(1);
Character ch1 = myHashMap.get(6);
System.out.println(ch);//A
System.out.println(ch1);//null
isEmpty()

如果此地圖不包含鍵值映射,則返回 true 。

keySet()

返回此地圖中包含的鍵的Set視圖。

HashMap<Integer,Character> myHashMap = new HashMap<>();
myHashMap.put(1,'A');
myHashMap.put(2,'B');
myHashMap.put(3,'C');
myHashMap.put(4,'D');
myHashMap.put(5,null);
Set keys = myHashMap.keySet();
System.out.println(keys); //[1, 2, 3, 4, 5]
put(K key, V value)

將指定的值與此映射中的指定鍵相關聯。

putAll(Map<? extends K,? extends V> m)

將指定地圖的所有映射覆制到此地圖。若存在相同的key則原Map的內容會被覆蓋

HashMap<Integer,Character> myHashMap = new HashMap<>();
myHashMap.put(1,'A');
myHashMap.put(2,'B');
myHashMap.put(3,'C');
myHashMap.put(4,'D');
myHashMap.put(5,null);
System.out.println(myHashMap);//{1=A, 2=B, 3=C, 4=D, 5=null}
HashMap<Integer,Character> myHashMap1 = new HashMap<>();
myHashMap1.put(1,'E');
myHashMap1.put(2,'F');
myHashMap1.put(3,'G');
myHashMap1.put(5,'H');
myHashMap1.put(6,'I');
myHashMap1.put(7,'J');
System.out.println(myHashMap1);//{1=E, 2=F, 3=G, 5=H, 6=I, 7=J}
myHashMap1.putAll(myHashMap);
System.out.println(myHashMap1);//{1=A, 2=B, 3=C, 4=D, 5=null, 6=I, 7=J}
putIfAbsent(K key, V value)

如果指定的鍵尚未與某個值相關聯(或映射到 null ),則將其與給定值相關聯並返回 null ,否則返回當前值。

HashMap<Integer,Character> myHashMap = new HashMap<>();
myHashMap.put(1,'A');
myHashMap.put(2,'B');
myHashMap.put(3,'C');
myHashMap.put(4,'D');
myHashMap.put(5,null);
System.out.println(myHashMap);//{1=A, 2=B, 3=C, 4=D, 5=null}
System.out.println(myHashMap.putIfAbsent(5,(Character)'E'));//null
System.out.println(myHashMap);//{1=A, 2=B, 3=C, 4=D, 5=E}
System.out.println(myHashMap.putIfAbsent(5,(Character)'F'));//E
System.out.println(myHashMap.putIfAbsent(6,(Character)'G'));//null
System.out.println(myHashMap);//{1=A, 2=B, 3=C, 4=D, 5=E, 6=G}
remove(Object key)

從該地圖中刪除指定鍵的映射(如果存在)。返回key對應的value,若key不存在,返回null

remove(Object key, Object value)

僅當指定的密鑰當前映射到指定的值時刪除該條目。

replace(K key, V value)

只有當目標映射到某個值時,才能替換指定鍵的條目。替換成功,返回被替換的value,否則返回null

HashMap<Integer,Character> myHashMap = new HashMap<>();
myHashMap.put(1,'A');
myHashMap.put(2,'B');
myHashMap.put(3,'C');
myHashMap.put(4,'D');
myHashMap.put(5,null);
System.out.println(myHashMap);//{1=A, 2=B, 3=C, 4=D, 5=null}
System.out.println(myHashMap.replace((Integer)1,(Character)'T'));//A
System.out.println(myHashMap);//{1=T, 2=B, 3=C, 4=D, 5=null}
System.out.println(myHashMap.replace((Integer)6,(Character)'T'));//null
System.out.println(myHashMap);//{1=T, 2=B, 3=C, 4=D, 5=null}
replace(K key, V oldValue, V newValue)

僅噹噹前映射到指定的值時,才能替換指定鍵的條目。返回boolean值。

HashMap<Integer,Character> myHashMap = new HashMap<>();
myHashMap.put(1,'A');
myHashMap.put(2,'B');
myHashMap.put(3,'C');
myHashMap.put(4,'D');
myHashMap.put(5,null);
System.out.println(myHashMap);//{1=A, 2=B, 3=C, 4=D, 5=null}
System.out.println(myHashMap.replace((Integer)1,(Character)'A',(Character)'T'));//true
System.out.println(myHashMap);//{1=T, 2=B, 3=C, 4=D, 5=null}
System.out.println(myHashMap.replace((Integer)6,(Character)'A',(Character)'O'));//false
System.out.println(myHashMap);//{1=T, 2=B, 3=C, 4=D, 5=null}
size()

返回此地圖中鍵值映射的數量。

values()

返回此地圖中包含的值的Collection視圖。

myHashMap.put(1,'A');
myHashMap.put(2,'B');
myHashMap.put(3,'C');
myHashMap.put(4,'D');
myHashMap.put(5,null);
System.out.println(myHashMap.values());//[A, B, C, D, null]

- THE END -
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章