People can't do something by themselves; they wanna tell you you can not do it.
| @Author:TTODS
Set集合继承了 Collection接口,与List的区别是,Set中的元素是无序的,且不能包含重复值。
构造一个新的空集合; HashMap实例具有默认初始容量(16)和负载因子(0.75)。
构造一个包含指定集合中的元素的新集合。
HashSet<String> myHashSet = new HashSet<>(Arrays.asList("one","two","three","four"));
System.out.println(myHashSet); //[four, one, two, three]
构造一个新的空集合; 实例具有指定的初始容量和默认负载因子(0.75)。
构造一个新的空集合; 实例具有指定的初始容量和指定的负载因子。
将指定的元素添加到此集合(如果尚未存在)。返回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]
从此集合中删除所有元素。
返回此 HashSet实例的浅层副本:元素本身不被克隆。
如果此集合包含指定的元素,则返回 true 。
如果此集合不包含元素,则返回 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
返回此集合中元素的迭代器。
如果存在,则从该集合中删除指定的元素。
返回此集合中的元素数(其基数)。
Map(映射)集合表示一种非常复杂的集合,允许按照某个键来访问元素。Map集合是由两个集合构成的,一个是键(key)集合,一个是值(value)集合。键集合是Set类型,因此不能有重复的元素。而值集合是Collection类型,可以有重复的元素。Map集合中的键和值是成对出现的。HashMap是常用的Map实现类。
构造一个空的 HashMap ,默认初始容量(16)和默认负载系数(0.75)。
构造一个空的 HashMap具有指定的初始容量和默认负载因子(0.75)。
构造一个空的 HashMap具有指定的初始容量和负载因子。
构造一个新的 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}
从这张地图中删除所有的映射。
返回此 HashMap实例的浅拷贝:键和值本身不被克隆。
如果此映射包含指定键的映射,则返回 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
如果此地图将一个或多个键映射到指定值,则返回 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
通过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
如果此地图不包含键值映射,则返回 true 。
返回此地图中包含的键的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]
将指定的值与此映射中的指定键相关联。
将指定地图的所有映射复制到此地图。若存在相同的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}
如果指定的键尚未与某个值相关联(或映射到 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}
从该地图中删除指定键的映射(如果存在)。返回key对应的value,若key不存在,返回null
仅当指定的密钥当前映射到指定的值时删除该条目。
只有当目标映射到某个值时,才能替换指定键的条目。替换成功,返回被替换的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}
仅当当前映射到指定的值时,才能替换指定键的条目。返回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}
返回此地图中键值映射的数量。
返回此地图中包含的值的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]