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 -
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值