Java_从入门到JavaEE_17

Day17

一、集合(续)

1.初识Map

Map存储的元素为键值对,通常称为key-value

而key是不允许重复的

Set唯一

2.HashMap

  1. HashMap的使用

    public class Test {
    	public static void main(String[] args) {
    		HashMap<String,Integer> map = new HashMap<>();
    		//添加元素
    		Integer put1 = map.put("aaa", 28);
    		Integer put2 = map.put("bbb", 23);
    		Integer put3 = map.put("nnn", 29);
    		Integer put4 = map.put("ccc", 21);
    		Integer put5 = map.put("ddd", 28);
    		System.out.println("put1:" + put1);//null
    		System.out.println("put2:" + put2);//null
    		System.out.println("put3:" + put3);//null
    		System.out.println("put4:" + put4);//null
    		System.out.println("put5:" + put5);//null
    		
    		//替换 - 如果有key就替换value,返回被替换的值
    		Integer put6 = map.put("aaa", 24);
    		System.out.println("put6:" + put6);//23
    		
    		//替换 - 如果有key就替换value,返回被替换的值;如果没有key就返回null
    		Integer replace1 = map.replace("aaa", 25);
    		System.out.println("replace1:" + replace1);//24
    		
    		//替换 -- 通过key+value替换
    		boolean replace2 = map.replace("aaa", 25, 26);
    		System.out.println("replace2:" + replace2);
    		
    		//将newMap中所有的元素添加到map集合中
    		HashMap<String, Integer> newMap = new HashMap<>();
    		newMap.put("aaa", 10);
    		newMap.put("bbb", 20);
    		newMap.put("ccc", 30);
    		map.putAll(newMap);
    		
    		//如果map集合中有相同的key,就返回value
    		//如果map集合中没有相同的key,就做添加操作并返回null
    		Integer putIfAbsent = map.putIfAbsent("caaa", 22);
    		System.out.println("putIfAbsent:" + putIfAbsent);//null
    		
    		//通过key获取value
    		System.out.println("通过Key获取Value:" + map.get("ddd"));//28
    		
    		//通过key获取value,如果可以不存在则返回默认值
    		System.out.println("通过Key获取Value:" + map.getOrDefault("aaa1", 888));//888
    		
    		System.out.println("判断集合中是否有指定的key:" + map.containsKey("aaa"));//true
    		System.out.println("判断集合中是否有指定的value:" + map.containsValue(28));//true
    		System.out.println("判断集合中是否没有元素:" + map.isEmpty());//false
    		
    		//根据key删除元素,返回被删除的value
    		Integer remove1 = map.remove("bbb");
    		System.out.println("remove1:" + remove1);
    		
    		//根据key+value删除元素,删除成功返回true,否则返回false
    		boolean remove2 = map.remove("nnn", 24);
    		System.out.println("remove2:" + remove2);//true
    		
    		//获取元素个数
    		System.out.println("获取元素个数:" + map.size());//8
    		
    		//获取map集合中所有的value
    		Collection<Integer> values = map.values();
    		System.out.println(Arrays.toString(values.toArray()));//集合->数组->字符串
    		
    		//清空集合
    		//map.clear();
    		
    		System.out.println("--------------------------------");
    		
    		//遍历集合 -- keySet()
    		//遍历思路:keySet()将Map中所有的key获取出,放在Set集合中,遍历Set集合依次获取key,利用map.get(key)获取对应的value
    		Set<String> keySet = map.keySet();
    		for (String key : keySet) {
    			Integer value = map.get(key);
    			System.out.println(key + " -- " + value);
    		}
    		
    		System.out.println("--------------------------------");
    		
    		//遍历集合 -- entrySet()
    		//遍历思路:entrySet()将Map中所有的映射关系对象获取出,放在Set集合中,遍历Set集合依次遍历出映射关系对象,映射关系对象中包含了key和value
    		Set<Entry<String,Integer>> entrySet = map.entrySet();
    		for (Entry<String, Integer> entry : entrySet) {
    			String key = entry.getKey();
    			Integer value = entry.getValue();
    			System.out.println(key + " -- " + value);
    		}
    		
    		
    	}
    }
    
    
  2. HashMap的特点

    public class Test {
    	//特点:无序且key去重(唯一)
    	public static void main(String[] args) {
    		HashMap<String,Integer> map = new HashMap<>();
    		map.put("aaa", 28);
    		map.put("bbb", 23);
    		map.put("ccc", 29);
    		map.put("ddd", 21);
    		map.put("eee", 21);
    		map.put("fff", 28);
    		map.put("fff", 30);
    		
    		Set<Entry<String,Integer>> entrySet = map.entrySet();
    		for (Entry<String, Integer> entry : entrySet) {
    			System.out.println(entry);
    		}
    		
    	}
    }
    
    
  3. 案例:给HashMap的value排序

    思路:

    HashMap 获取映射关系对象的Set集合 -> ArrayList对象 -> list.sort(外置比较器)

    public class Test {
    	public static void main(String[] args) {	
    		HashMap<String,Integer> map = new HashMap<>();
    		
    		map.put("aaa", 28);
    		map.put("bbb", 23);
    		map.put("ccc", 29);
    		map.put("ddd", 21);
    		map.put("eee", 21);
    		map.put("fff", 28);
    		
    		//获取映射关系对象的集合
    		Set<Entry<String,Integer>> entrySet = map.entrySet();
    		
    		//将Set集合转换为ArraryList集合
    		ArrayList<Entry<String,Integer>> list = new ArrayList<>(entrySet);
    		
    		//排序
    		list.sort(new Comparator<Entry<String,Integer>>() {
    			@Override
    			public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
    				return o1.getValue() - o2.getValue();
    			}
    		});
    		
    		for (Entry<String, Integer> entry : list) {
    			System.out.println(entry);
    		}
    		
    		
    	}
    }
    
    

3.LinkedHashMap

  1. LinkedHashMap的使用(与HashMap的使用基本一致替换即可)

  2. LinkedHashMap的特点

    继承关系:class LinkedHashMap<K,V> extends HashMap

    注意:LinkedHashMap在HashMap的基础上添加了双向链表

    特点:有序且Key去重

    public class Test02 {
    	public static void main(String[] args) {
    		LinkedHashMap<String, Integer> map = new LinkedHashMap<>();		
    		map.put("aaa", 40);
    		map.put("bbb", 10);
    		map.put("ccc", 30);
    		map.put("ddd", 20);
    		map.put("eee", 40);
    		map.put("eee", 50);		
    		Set<Entry<String,Integer>> entrySet = map.entrySet();
    		for (Entry<String, Integer> entry : entrySet) {
    			System.out.println(entry);
    		}
    	}
    }
    

4.Hashtable

  1. Hashtable的使用(与HashMap的使用基本一致替换即可)

  2. Hashtable的特点

    特点:无序且key去重 + 线程安全(方法上加锁)

    public class Test {
    	public static void main(String[] args) {
    		Hashtable<String, Integer> map = new Hashtable<>();
    		map.put("aaa", 40);
    		map.put("bbb", 10);
    		map.put("ccc", 30);
    		map.put("ddd", 20);
    		map.put("eee", 40);
    		map.put("eee", 50);	
    		Set<Entry<String,Integer>> entrySet = map.entrySet();
    		for (Entry<String, Integer> entry : entrySet) {
    			System.out.println(entry);
    		}
    	}
    }
    

5.ConcurrentHashMap

  1. ConcurrentHashMap的使用(与HashMap的使用基本一致替换即可)

  2. ConcurrentHashMap的特点

    特点:无序且key去重 + 线程安全(局部加锁)

    public class Test {
    	public static void main(String[] args) {
    		ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();	
    		map.put("aaa", 40);
    		map.put("bbb", 10);
    		map.put("ccc", 30);
    		map.put("ddd", 20);
    		map.put("eee", 40);
    		map.put("eee", 50);
    		Set<Entry<String,Integer>> entrySet = map.entrySet();
    		for (Entry<String, Integer> entry : entrySet) {
    			System.out.println(entry);
    		}
    	}
    }
    

6.HashMap,LinkedHashMap,Hashtable,ConcurrentHashMap区别

  1. 特点的区别:
    1. HashMap:无序且去重
    2. LinkedHashMap:有序且去重
    3. Hashtable:无序且去重 + 线程安全(方法上加锁,已弃用)
    4. ConcurrentHashMap:无序且去重 + 线程安全(局部加锁+CAS,效率更高)
  2. 存储null键null值的区别:
    1. HashMap:ok
    2. LinkedHashMap:ok
    3. Hashtable:no
    4. ConcurrentHashMap:no

7.TreeMap

  1. TreeMap的使用(与HashMap的使用基本一致替换即可)

  2. TreeMap的特点

    特点:针对于key进行自然排序

    public class Test {
    	public static void main(String[] args) {
    		TreeMap<String, Integer> map = new TreeMap<>();
    		map.put("ddd", 20);
    		map.put("eee", 40);
    		map.put("bbb", 10);
    		map.put("ccc", 30);
    		map.put("aaa", 40);
    		map.put("eee", 50);
    		Set<Entry<String,Integer>> entrySet = map.entrySet();
    		for (Entry<String, Integer> entry : entrySet) {
    			System.out.println(entry);
    		}
    	}
    }
    

8.Properties

  1. 案例及解析

    import java.io.IOException;
    import java.util.Properties;
    public class Test {
    	public static void main(String[] args) throws IOException {
    		//配置文件对象
    		Properties properties = new Properties();	
    		//将配置文件加载到对象中
            properties.load(Test.class.getClassLoader(). getResourceAsStream("DBConfig.properties"));//找到创建的DBConfig.properties文件
    		//获取配置文件里的数据
    		String username = properties.getProperty("username");
    		String password = properties.getProperty("password");
    		System.out.println(username + " -- " + password);
    	}
    }
    
    

9.Collections集合工具类

  1. 案例及解析

    public class Test {
    	public static void main(String[] args) {	
    		ArrayList<Integer> list = new ArrayList<>();	
    		//批量添加
    		Collections.addAll(list, 5,8,3,4,1,2,7,9,6);		
    		//排序 -- 内置比较器(按照元素所属类的排序规则)
    		Collections.sort(list);		
    		//注意:查找之前必须先排序
    		int index = Collections.binarySearch(list, 3);
    		System.out.println("获取元素的下标:" + index);		
    		//排序 -- 外置比较器
    		Collections.sort(list, new Comparator<Integer>() {
    			@Override
    			public int compare(Integer o1, Integer o2) {
    				return o2-o1;
    			}
    		});		
    		Integer max = Collections.max(list);
    		System.out.println("最大值:" + max);		
    		Integer min = Collections.min(list);
    		System.out.println("最小值:" + min);		
    		//替换所有元素
    		Collections.fill(list, 888);		
    		//获取线程安全的List集合
    		List<Integer> synchronizedList = Collections.synchronizedList(list);	
    		//[888, 888, 888, 888, 888, 888, 888, 888, 888]
    		System.out.println(Arrays.toString(synchronizedList.toArray()));
    	}
    }
    
    

10.EnumSet

  1. 可以存储枚举的Set集合

  2. 案例:

    public class Test {
    	public static void main(String[] args) {		
    		//Set集合中存储了Signal的对象
    		EnumSet<Signal> set = EnumSet.allOf(Signal.class);		
    		set.remove(Signal.RED);		
    		Iterator<Signal> it = set.iterator();
    		while(it.hasNext()){
    			Signal next = it.next();
    			System.out.println(next);
    		}
    	}
    }
    public enum Signal {
    	RED, YELLOW, GREEN;
    }
    

11.EnumMap

  1. 可以存储枚举的Map集合(注意:将枚举对象存储在key的位置)

  2. 案例:

    public class Test {
    	public static void main(String[] args) {	
    		EnumMap<Signal,String> map = new EnumMap<>(Signal.class);	
    		map.put(Signal.RED, "红灯");
    		map.put(Signal.YELLOW, "黄灯");
    		map.put(Signal.GREEN, "绿灯");	
    		Set<Entry<Signal,String>> entrySet = map.entrySet();
    		for (Entry<Signal, String> entry : entrySet) {
    			System.out.println(entry);
    		}
    	}
    }
    public enum Signal {
    	RED, YELLOW, GREEN;
    }
    
  • 21
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值