Day14学习Java

Collections集合工具类

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 = Collection.min(list);
    System.out.println("最小值" + min);
    
    //替换所有元素
    Collection.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()));
}

ConcurrentHashMap的使用

ConcurrentHashMap<String,Integer> map = new ConcurrentHashMap<>();
​
//添加元素
Integer put1 = map.put("麻生希", 28);
Integer put2 = map.put("椎名空", 23);
Integer put3 = map.put("水菜丽", 29);
Integer put4 = map.put("朝桐光", 21);
Integer put5 = map.put("北岛玲", 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("椎名空" , 24);
System.out.println("put6:" + put6);//23
​
//替换 - 如果有key就替换value,返回被替换的值,如果没有key就返回null
Integer replace1 = map.replace("椎名空",25);
System.out.println("replace1:" + replace1);//24
​
//替换 -- 通过key+value替换
boolean replace2 = map.replace("椎名空", 25, 26);
System.out.println("replace2:" + replace2);
​
//将newMap中所有的元素添加到map集合中
ConcurrentHashMap<String, Integer> newMap = new ConcurrentHashMap<>();
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("朝桐光aaa", 22);
System.out.println("putIfAbsent:" + putIfAbsent);//null
​
//通过key获取value
System.out.println("通过Key获取Value:" + map.get("麻生希"));//28
​
//通过key获取value,如果可以不存在则返回默认值
System.out.println("通过Key获取Value:" + map.getOrDefault("麻生希1", 888));//888
        
System.out.println("判断集合中是否有指定的key:" + map.containsKey("麻生希"));//true
System.out.println("判断集合中是否有指定的value:" + map.containsValue(28));//true
System.out.println("判断集合中是否没有元素:" + map.isEmpty());//false
​
//根据key删除元素,返回被删除的value
Integer remove1 = map.remove("水菜丽");
System.out.println("remove1:" + remove1);
​
//根据key+value删除元素,删除成功返回true,否则返回false
boolean remove2 = map.remove("椎名空", 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();
​
//遍历集合 -- 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);
}
​
//遍历集合 -- 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);
}

ConcurrentHashMap的特点

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

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);
    }
}

HashMap vs LinkedHashMap vs Hashtable vs ConcurrentHashMap

特点的区别:

HashMap:无序且去重

LinkedHashMap:有序且去重 Hashtable:无序且去重 + 线程安全(方法上加锁,已弃用)

ConcurrentHashMap:无序且去重 + 线程安全(局部加锁+CAS,效率更高)

存储null键null值的区别: HashMap:ok LinkedHashMap:ok Hashtable:no ConcurrentHashMap:no

EnumMap -- 可以存储枚举的Map集合

注意:将枚举对象存储在key的位置

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;
}

EnumSet -- 可以存储枚举的Set集合

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);
    }
}

HashMap的使用

public static void main(String[] args) {
        
    HashMap<String,Integer> map = new HashMap<>();
        
    //添加元素
    Integer put1 = map.put("麻生希", 28);
    Integer put2 = map.put("椎名空", 23);
    Integer put3 = map.put("水菜丽", 29);
    Integer put4 = map.put("朝桐光", 21);
    Integer put5 = map.put("北岛玲", 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("椎名空", 24);
    System.out.println("put6:" + put6);//23
    
    //替换 - 如果有key就替换value,返回被替换的值;如果没有key就返回null
    Integer replace1 = map.replace("椎名空", 25);
    System.out.println("replace1:" + replace1);//24
        
    //替换 -- 通过key+value替换
    boolean replace2 = map.replace("椎名空", 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("朝桐光aaa", 22);
    System.out.println("putIfAbsent:" + putIfAbsent);//null
    
    //通过key获取value
    System.out.println("通过Key获取Value:" + map.get("麻生希"));//28
    
    //通过key获取value,如果可以不存在则返回默认值
    System.out.println("通过Key获取Value:" + map.getOrDefault("麻生希1", 888));//888
        
    System.out.println("判断集合中是否有指定的key:" + map.containsKey("麻生希"));//true
    System.out.println("判断集合中是否有指定的value:" + map.containsValue(28));//true
    System.out.println("判断集合中是否没有元素:" + map.isEmpty());//false
    
    //根据key删除元素,返回被删除的value
    Integer remove1 = map.remove("水菜丽");
    System.out.println("remove1:" + remove1);
    
    //根据key+value删除元素,删除成功返回true,否则返回false
    boolean remove2 = map.remove("椎名空", 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();
    
    //遍历集合 -- 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);
    }
    
    //遍历集合 -- 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);
    }

HashMap的特点

特点:无序 且 key去重(唯一)

public static void main(String[] args) {

HashMap<String,Integer> map = new HashMap<>();
    
map.put("麻生希", 28);
map.put("椎名空", 23);
map.put("水菜丽", 29);
map.put("朝桐光", 21);
map.put("侯小康", 21);
map.put("北岛玲", 28);
map.put("北岛玲", 30);
    
Set<Entry<String,Integer>> entrySet = map.entrySet();
for (Entry<String, Integer> entry : entrySet) {
    System.out.println(entry);
}

HashMap的面试题

需求:给HashMap的value排序

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

HashMap<String,Integer> map = new HashMap<>();
		
map.put("麻生希", 28);
map.put("椎名空", 23);
map.put("水菜丽", 29);
map.put("朝桐光", 21);
map.put("侯小康", 21);
map.put("北岛玲", 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);
}

Hashtable的使用

public static void main(String[] args) {
		
	Hashtable<String,Integer> map = new Hashtable<>();
		
	//添加元素
	Integer put1 = map.put("麻生希", 28);
	Integer put2 = map.put("椎名空", 23);
	Integer put3 = map.put("水菜丽", 29);
	Integer put4 = map.put("朝桐光", 21);
	Integer put5 = map.put("北岛玲", 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("椎名空", 24);
	System.out.println("put6:" + put6);//23
		
	//替换 - 如果有key就替换value,返回被替换的值;如果没有key就返回null
	Integer replace1 = map.replace("椎名空", 25);
	System.out.println("replace1:" + replace1);//24
		
	//替换 -- 通过key+value替换
	boolean replace2 = map.replace("椎名空", 25, 26);
	System.out.println("replace2:" + replace2);
		
	//将newMap中所有的元素添加到map集合中
	Hashtable<String, Integer> newMap = new Hashtable<>();
	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("朝桐光aaa", 22);
	System.out.println("putIfAbsent:" + putIfAbsent);//null
		
	//通过key获取value
	System.out.println("通过Key获取Value:" + map.get("麻生希"));//28
		
	//通过key获取value,如果可以不存在则返回默认值
	System.out.println("通过Key获取Value:" + map.getOrDefault("麻生希1", 888));//888
		
	System.out.println("判断集合中是否有指定的key:" + map.containsKey("麻生希"));//true
	System.out.println("判断集合中是否有指定的value:" + map.containsValue(28));//true
	System.out.println("判断集合中是否没有元素:" + map.isEmpty());//false
		
	//根据key删除元素,返回被删除的value
	Integer remove1 = map.remove("水菜丽");
	System.out.println("remove1:" + remove1);
		
	//根据key+value删除元素,删除成功返回true,否则返回false
	boolean remove2 = map.remove("椎名空", 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);
	}
}

Hashtable的特点

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

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);
	}
}

LinkedHashMap的使用

public static void main(String[] args) {
		
	LinkedHashMap<String,Integer> map = new LinkedHashMap<>();
		
	//添加元素
	Integer put1 = map.put("麻生希", 28);
	Integer put2 = map.put("椎名空", 23);
	Integer put3 = map.put("水菜丽", 29);
	Integer put4 = map.put("朝桐光", 21);
	Integer put5 = map.put("北岛玲", 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("椎名空", 24);
	System.out.println("put6:" + put6);//23
	
	//替换 - 如果有key就替换value,返回被替换的值;如果没有key就返回null
	Integer replace1 = map.replace("椎名空", 25);
	System.out.println("replace1:" + replace1);//24
		
	//替换 -- 通过key+value替换
	boolean replace2 = map.replace("椎名空", 25, 26);
	System.out.println("replace2:" + replace2);
		
	//将newMap中所有的元素添加到map集合中
	LinkedHashMap<String, Integer> newMap = new LinkedHashMap<>();
	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("朝桐光aaa", 22);
	System.out.println("putIfAbsent:" + putIfAbsent);//null
		
	//通过key获取value
	System.out.println("通过Key获取Value:" + map.get("麻生希"));//28
		
	//通过key获取value,如果可以不存在则返回默认值
	System.out.println("通过Key获取Value:" + map.getOrDefault("麻生希1", 888));//888
		
	System.out.println("判断集合中是否有指定的key:" + map.containsKey("麻生希"));//true
	System.out.println("判断集合中是否有指定的value:" + map.containsValue(28));//true
	System.out.println("判断集合中是否没有元素:" + map.isEmpty());//false
		
	//根据key删除元素,返回被删除的value
	Integer remove1 = map.remove("水菜丽");
	System.out.println("remove1:" + remove1);
		
	//根据key+value删除元素,删除成功返回true,否则返回false
	boolean remove2 = map.remove("椎名空", 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);
	}
}		

LinkedHashMap的特点

继承关系:class LinkedHashMap<K,V> extends HashMap 注意:LinkedHashMap在HashMap的基础上添加了双向链表

特点:有序且Key去重

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);
	}
}

Properties

public static void main(String[] args) throws IOException {
		
	//配置文件对象
	Properties properties = new Properties();
		
	//将配置文件加载到对象中
		properties.load(Test01.class.getClassLoader().getResourceAsStream("DBConfig.properties"));
		
	//获取配置文件里的数据
	String username = properties.getProperty("username");
	String password = properties.getProperty("password");
	System.out.println(username + " -- " + password);
}
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值