目录
map接口
将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值
map接口和collection接口的不同
map是双列的,collection是单列的
map的键唯一,collection的子体系set是唯一的
map集合的数据结构值针对键有效,取值无关,collection集合的数据结构是针对元素有效
map集合的功能概述
添加功能
V put(K key,V value) //添加元素
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存在,就用值把以前的值替换
删除功能
void clear() //移除所有的健值对元素
V remove(Object key) //根据键删除键值对元素,返回以前的值
判断功能
boolean containsKey(Object key) //判断集合中是否包含指定的键
boolean containsValue(Object value) //判断集合是否包含指定的值
boolean isEmpty() //判断集合是否为空
获取功能
Set<Map.Entry<K,V>> entrySet() //map.Entry,Entry是map接口的内部接口
V get(Object key) //根据键获取值
Set<K> keySet() //获取集合中的所有键的集合
Collection<V> values() //获取集合中所有值的集合
长度功能
int size() //返回集合中的键值对的个数
map集合的遍历之键找值
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ddddd {
public static void main(String[] args) {
// TODO Auto-generated method stub
//map集合没有迭代器
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 24);
map.put("王五", 25);
map.put("马六", 26);
//获取所有键(方法1
Set<String> keyset = map.keySet(); //获取所有键的集合
Iterator<String> it = keyset.iterator(); //获取迭代器
while(it.hasNext()) { //判断集合中是否有元素
String key = it.next(); //获取键
System.out.println(key + map.get(key)); //获取值
}
//增强for循环遍历(方法2
for(String key :map.keySet()) { //map.keyset()是所有键的集合
System.out.println(key + map.get(key));
}
}
}
map集合的遍历之键值对对象找键和值
把双列集合的键值对,变成单列集合的键值对对象,然后遍历这个集合获取每个键值对对象,然后根据键值对对象找到键和值
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class demo {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("张三", 23);
map.put("李四", 24);
map.put("王五", 25);
map.put("马六", 23);
Set<Map.Entry<String, Integer>> entryset = map.entrySet(); //将键和值封装成对象,并存储在set集合中
Iterator<Map.Entry<String, Integer>> it = entryset.iterator(); //获取迭代器
while(it.hasNext()) { //判断有无元素
Map.Entry<String, Integer> en = it.next(); //获取每一个对象
String key = en.getKey(); //获取键
Integer value = en.getValue(); //获取值
System.out.println(key + value);
}
//增强for循环方法
for(Map.Entry<String, Integer> en : map.entrySet()) {
System.out.println(en.getKey() + en.getValue());
}
}
}
HashMap集合键是student值是String的案例
import java.util.HashMap;
import student.Student;
public class hashmap {
public static void main(String[] args) {
HashMap<Student, String> hm = new HashMap<>();
hm.put(new Student("张三",23), "北京");
hm.put(new Student("张三",23), "上海");
hm.put(new Student("李四",24), "广州");
hm.put(new Student("张三",25), "深圳");
hm.put(new Student("张三",26), "西安");
System.out.println(hm);
}
}
student类中记得重写tostring方法equals方法以及hashcode方法,否则比较的是地址值,打印的也是地址值,不会覆盖掉相同对象
LinkedHashMap的概述以及使用
import java.util.LinkedHashMap;
public class linkedhashmap {
public static void main(String[] args) {
LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>();
lhm.put("lay",23);
lhm.put("zhang", 24);
lhm.put("loey", 25);
lhm.put("chanyeol", 26);
System.out.println(lhm);
}
//按照顺序存储
}
TreeMap集合键是Student值是String的案例
import java.util.TreeMap;
import student.Student;
public class Treemap {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeMap<Student, String> tm = new TreeMap<>();
tm.put(new Student("张三",23), "北京");
tm.put(new Student("张三",23), "上海");
tm.put(new Student("李四",24), "广州");
tm.put(new Student("张三",25), "深圳");
tm.put(new Student("张三",26), "西安");
//会报错,由于Treeset会排序,因此需要student实现comparable接口
System.out.println(tm);
}
}
比较器(匿名内部类
import java.util.Comparator;
import java.util.TreeMap;
import student.Student;
public class Treemap {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int num = o1.getName().compareTo(o2.getName());
return num == 0 ? o1.getAge()-o2.getAge() : num;
}
});
tm.put(new Student("张三",23), "北京");
tm.put(new Student("张三",23), "上海");
tm.put(new Student("李四",24), "广州");
tm.put(new Student("张三",25), "深圳");
tm.put(new Student("张三",26), "西安");
//会报错,由于Treeset会排序,因此需要student实现comparable接口
System.out.println(tm);
}
}
统计字符串中每个字符出现的个数
import java.util.HashMap;
public class test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "qqqqqwwwweeee";
char[] arr = str.toCharArray();
HashMap<Character, Integer> map = new HashMap<>();
for(char c : arr) {
// if(map.containsKey(c)) { //如果有这个键
// map.put(c, map.get(c) + 1); //值+1
// }else { //否则
// map.put(c, 1 ) //值存1
// }
map.put(c,map.containsKey(c) ? map.get(c) + 1 : 1); //转化为三元运算符
}
System.out.println(map);
}
}
集合嵌套之HashMap嵌套HashMap
import java.util.HashMap;
import student.Student;
public class hashmaphashmap {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Student,String> hm1 = new HashMap<>(); //第一个hashmap
hm1.put(new Student("张",23), "北京");
hm1.put(new Student("li",24), "北京");
hm1.put(new Student("三",25), "北京");
hm1.put(new Student("wu",26), "北京");
HashMap<Student,String> hm2 = new HashMap<>(); //第二个hashmap
hm2.put(new Student("loey",23), "北京");
hm2.put(new Student("park",24), "北京");
hm2.put(new Student("lay",25), "北京");
hm2.put(new Student("zhang",26), "北京");
HashMap<HashMap<Student,String>,String> map = new HashMap<>(); //把两个集合当作键存进hashmap
map.put(hm2,"1");
map.put(hm1,"2");
for(HashMap<Student,String> h : map.keySet()) {
String value = map.get(h); //获取大集合中的值
System.out.println(value);
for(Student s :h.keySet()) {
System.out.println(s +"=" + h.get(s)); //获取小集合中的值
}
}
}
}
HashMap和Hashtable的区别
共同点:底层都是hash算法实现的,都是双列集合
区别:HashMap是线程不安全的,效率高JDK1.2版本的
Hashtable是线程安全的,效率低,JDK1.0版本的
HashMap可以存储null键和null值
Hashtable不可以存储null键和null值
Collections工具类的概述和常见方法
都是静态方法
public static <T> void sort(List<T> list) //排序
public static <T> int binarySearch(List<T> list,T key) //二分查找(前提要有序
public static <T> T max(Collection<?> coll) //获取最大值(底层会排序
public static void reverse(List<?> list) //反转
public static void shuffle(List<?> list) //随机置换,相当于洗牌
二分查找如果没有该元素则返回-插入点-1
泛型固定下边界
? super E
泛型固定上边界 ? extends E
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;
import student.Student;
import student.base;
public class demo_Genric {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet<Student> ts = new TreeSet<>(new CompareByAge());
ts.add(new Student("张三",23));
System.out.println(ts);
TreeSet<base> ts1 = new TreeSet<>(new CompareByAge()); //泛型固定下边界
ts1.add(new base("李四",24));
System.out.println(ts1);
}
public static void demo() {
ArrayList<Student> list1 = new ArrayList<>();
list1.add(new Student("张三",23));
list1.add(new Student("李四",24));
ArrayList<base> list11 = new ArrayList<>();
list11.add(new base("wang",23));
list11.add(new base("zhang",24));
list1.addAll(list11); //泛型固定上边界
}
}
class CompareByAge implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
int num = o1.getAge()-o2.getAge();
return num == 0 ?o1.getName().compareTo(o2.getName()):num;
}
}