Map集合
1.Map集合概述
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值
2.Map集合和Collection的区别
- Map是双列的,Collection是单列的
- Map的键唯一,Collection的Set是唯一的
- Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效
3.Map集合的功能
- put(K,V):添加元素
public class Demo17 {
public static void main(String[] args) {
HashMap<Integer,String> stringHashMap=new HashMap<>();
stringHashMap.put(1,"ls");
stringHashMap.put(2,"zz");
stringHashMap.put(3,"lq");
stringHashMap.put(null,null);//HashMap可以存储null值和null键
stringHashMap.put(3,"yy");
}
}
- clear :移除所有的键值对元素
public class Demo18 {
public static void main(String[] args) {
HashMap<Integer,String> stringHashMap=new HashMap<>();
stringHashMap.put(1,"ls");
stringHashMap.clear(); //清空集合
}
}
- remove:根据Key值删除Value,并返回值
- containsKey:判断集合是否包含指定的键值
- containsValue:判断集合是否包含指定的值
- isEmpty: 判断集合是否为空
public class Demo19 {
public static void main(String[] args) {
HashMap<Integer,String> hashMap=new HashMap<>();
hashMap.put(520,"ls");
hashMap.put(250,"yy");
String remove = hashMap.remove(250);//移除指定位置元素并返回Value
System.out.println(remove);
boolean b = hashMap.containsKey(520);//判断是否包含指定Key值
boolean ls = hashMap.containsValue("ls");//判断是否包含指定值
System.out.println(b+"\t"+ls);
boolean empty = hashMap.isEmpty();//判断集合是否为空
System.out.println(empty);
}
}
- entrySet:返回一个键值对的set集合
public class Demo20 {
public static void main(String[] args) {
HashMap<Integer,String> hashMap=new HashMap<>();
hashMap.put(520,"ls");
hashMap.put(250,"yy");
hashMap.put(333,"zz");
Set<Map.Entry<Integer, String>> entries = hashMap.entrySet();
for (Map.Entry<Integer, String> data:
entries) {
Integer key = data.getKey();
String value = data.getValue();
System.out.println(key+"===="+value);
}
}
}
- keySet:返回一个集合中所有key的集合
public class Demo20 {
public static void main(String[] args) {
HashMap<Integer,String> hashMap=new HashMap<>();
hashMap.put(520,"ls");
hashMap.put(250,"yy");
hashMap.put(333,"zz");
Set<Integer> integers = hashMap.keySet();
for ( Integer data:
integers) {
String s = hashMap.get(data);
System.out.println(data+"======="+s);
}
}
}
- foreach
public class Demo20 {
public static void main(String[] args) {
HashMap<Integer,String> hashMap=new HashMap<>();
hashMap.put(520,"ls");
hashMap.put(250,"yy");
hashMap.put(333,"zz");
hashMap.forEach(new BiConsumer<Integer, String>() {
@Override
public void accept(Integer integer, String s) {
System.out.println(integer+"======="+s);
}
});
}
}
4.如何保证HashMap中Key值的唯一性
HashMap中Key值实则为HashSet,也就是说HashMap的底层数据结构为哈希表
哈希表保证数据的唯一性必须要重写HashCode和equals方法
当不重写HashCode和equals方法时,不能保证唯一性
我们常用的类已经被重写了,所以不用我们重写
当自定义的类时切记不要忘记重写
5.LinkedHashMap
- 概述:Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序
- 特点:底层的数据结构是链表和哈希表 元素有序 并且唯一
6.TreeMap
-
键的数据结构是红黑树,可保证键的排序和唯一性
-
排序分为自然排序和比较器排序
-
线程是不安全的效率比较高
-
TreeMap中可以有null值,不可以有null键。
7.统计字符串中每个字符出现的次数
public class Demo21 {
public static void main(String[] args) {
String s1= "aababcabcdabcde";
HashMap<Character,Integer> hashMap=new HashMap<>();
for (int i = 0; i < s1.length(); i++) {
char c = s1.charAt(i);
if(!hashMap.containsKey(c)){
hashMap.put(c,1);
}else{
Integer integer = hashMap.get(c);
integer++;
hashMap.put(c,integer);
}
}
Set<Character> characters = hashMap.keySet();
for (Character data:
characters) {
Integer value = hashMap.get(data);
System.out.println(data+"=="+ value);
}
}
}
8.Collections
1:Collections类概述: 针对集合操作 的工具类
2:Collections成员方法
public static <T> void sort(List<T> list): 排序,默认按照自然顺序
public static <T> int binarySearch(List<?> list,T key): 二分查找
public static <T> T max(Collection<?> coll): 获取最大值
public static void reverse(List<?> list): 反转
public static void shuffle(List<?> list):
随机置换