一、Map接口
1.1、Map接口概述
将键映射到值的对象
一个映射不能包含重复的键
每个键最多只能映射到一个值
1.2、Map接口和Collection接口的不同
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set是唯一的
Map集合的数据结构值针对键有效,跟值无关
Collection集合的数据结构是针对元素有效
1.3、Map接口成员方法
V put(K key,V value)
V remove(Object key)
void clear()
boolean containsKey(Object key)
boolean containsValue(Object value)
boolean isEmpty()
int size()
V get(Object key)
Set<K> keySet()
Collection<V> values()
Set<Map.Entry<K,V>> entrySet()
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("张三","李四");//添加功能
map.remove("张三");//删除功能
map.containsKey("张三");//是否包含键
map.isEmpty();//判断集合是否为空
map.get("张三");
Set<String> key = map.keySet();//获取键集合
Collection<String> values = map.values();//获取值集合
}
1.4、Map集合遍历
方式1:根据键找值
获取所有键的集合
遍历键的集合,获取到每一个键
根据键找值
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
Set<String> set = map.keySet();
for (String s : set) {
String ss = map.get(s);
System.out.println(s+"==="+ss);
}
}
方式2:根据键值对对象找键和值
获取所有键值对对象的集合
遍历键值对对象的集合,获取到每一个键值对对象
根据键值对对象找键和值
public static void main(String[] args) {
Map<String,String> map = new HashMap<String,String>();
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
Set<Map.Entry<String, String>> set =map.entrySet();
for (Map.Entry<String, String> me : set) {
String key = me.getKey();
String value = me.getValue();
System.out.println(key+"==="+value);
}
}
二、HashMap类
2.1、HashMap类概述
键是哈希表结构,可以保证键的唯一性
2.2、HashMap案例
HashMap<String,String>
public static void main(String[] args) {
HashMap<String, String> hashmap = new HashMap<String,String>();
hashmap.put("郭靖","黄蓉");
hashmap.put("杨过","小龙女");
Set<Map.Entry<String, String>> set = hashmap.entrySet();
for (Map.Entry<String, String> me : set) {
String key = me.getKey();
String value = me.getValue();
System.out.println(key+"===="+value);
}
}
HashMap<Integer,String>
public static void main(String[] args) {
HashMap<Integer, String> hashmap = new HashMap<>();
hashmap.put(1,"01");
hashmap.put(2,"02");
Set<Integer> set = hashmap.keySet();
for (Integer in : set) {
String values = hashmap.get(in);
System.out.println(in+"=="+values);
}
}
HashMap<String,Student>
public static void main(String[] args) {
HashMap<String, Student> hashmap = new HashMap<String, Student>();
hashmap.put("学生一",new Student("张三",23));
hashmap.put("学生二",new Student("李四",30));
Set<Map.Entry<String, Student>> set =hashmap.entrySet();
for (Map.Entry<String, Student> me : set) {
String key = me.getKey();
Student value = me.getValue();
System.out.println(key);
System.out.println(value.getName()+"==="+value.getAge());
}
}
HashMap<Student,String>
public static void main(String[] args) {
HashMap<Student, String> hashmap = new HashMap<Student, String>();
hashmap.put(new Student("张三",23),"学生一");
hashmap.put(new Student("李四",30),"学生二");
Set<Student> set = hashmap.keySet();
for (Student s : set) {
String ss = hashmap.get(s);
System.out.println(s+"==="+ss);
}
}
2.3、LinkedHashMap类概述
Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序。
三、TreeMap类
3.1、TreeMap类概述
键是红黑树结构,可以保证键的排序和唯一性
3.2、TreeMap案例
TreeMap<String,String>
public static void main(String[] args) {
TreeMap<String, String> treemap = new TreeMap<>();
treemap.put("郭靖","黄蓉");
treemap.put("杨过","小龙女");
Set<Map.Entry<String, String>> set =treemap.entrySet();
for (Map.Entry<String, String> me : set) {
String key = me.getKey();
String value = me.getValue();
System.out.println(key+"=="+value);
}
}
TreeMap<Student,String>
public static void main(String[] args) {
TreeMap<Student, String> treemap = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num=s1.getAge()-s2.getAge();
int num2=num==0?s1.getName().compareTo(s2.getName()):num;
return num2;
}
});
treemap.put(new Student("张三",23),"学生一");
treemap.put(new Student("李四",30),"学生二");
Set<Map.Entry<Student, String>> set = treemap.entrySet();
for (Map.Entry<Student, String> me : set) {
Student key = me.getKey();
String value = me.getValue();
System.out.println(key+"==="+value);
}
}
四、Map集合案例
(1)“aababcabcdabcde”,获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
public static void main(String[] args) {
String str="aababcabcdabcde";
char[] chars = str.toCharArray();
TreeMap<Character, Integer> treemap = new TreeMap<>();
for (char ch : chars) {
Integer i=treemap.get(ch);
if(i==null){
treemap.put(ch,1);
}else{
i++;
treemap.put(ch,i);
}
}
StringBuffer sb = new StringBuffer();
Set<Map.Entry<Character, Integer>> set =treemap.entrySet();
for (Map.Entry<Character, Integer> en : set) {
Integer value = en.getValue();
Character key = en.getKey();
sb.append(key).append("(").append(value).append(")");
}
String s=sb.toString();
System.out.println(s);
}
HashMap嵌套HashMap:
public static void main(String[] args) {
HashMap<String, HashMap<String, Integer>> hashmap = new HashMap<>();
HashMap<String, Integer> jchashmap = new HashMap<>();
jchashmap.put("张三",23);
jchashmap.put("李四",30);
HashMap<String, Integer> jyhashmap = new HashMap<>();
jyhashmap.put("王五",25);
jyhashmap.put("赵六",26);
hashmap.put("基础",jchashmap);
hashmap.put("就业",jyhashmap);
Set<String> set = hashmap.keySet();
for (String s : set) {
HashMap<String, Integer> valueHashMap = hashmap.get(s);
Set<Map.Entry<String, Integer>> en = valueHashMap.entrySet();
for (Map.Entry<String, Integer> entry : en) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(s+" "+key+"=="+value);
}
}
}
HashMap和Hashtable的区别:
HashMap:
线程不同步,效率高;可以存储null键null值
Hashtable:
线程同步,数据安全;不可以存储null键null值
五、Collections类
5.1、Collections类概述
针对集合操作 的工具类
5.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)
模拟斗地主发牌:
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
String[] colors={"♠","♥","♣","♦"};
String[] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (String color : colors) {
for (String num : nums) {
arrayList.add(color.concat(num));
}
}
arrayList.add("大王");
arrayList.add("小王");
Collections.shuffle(arrayList);
ArrayList<String> arr1 = new ArrayList<>();
ArrayList<String> arr2 = new ArrayList<>();
ArrayList<String> arr3 = new ArrayList<>();
ArrayList<String> arr4 = new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
if(i>=arrayList.size()-3){
arr4.add(arrayList.get(i));
}else if(i%3==0){
arr1.add(arrayList.get(i));
}else if(i%3==1){
arr2.add(arrayList.get(i));
}else if(i%3==2){
arr3.add(arrayList.get(i));
}
}
look("张三",arr1);
look("李四",arr2);
look("王五",arr3);
look("底牌",arr4);
}
public static void look(String name,ArrayList<String> arr){
System.out.print(name);
for (String s : arr) {
System.out.print(s+" ");
}
System.out.println();
}