Java集合-Map家族

集合-Map家族

1 各实现类的特点
  1. HashMap:存key+value,key去重,无序,线程不安全
  2. LinkedHashMap:存key+value,key去重,有序,线程不安全
  3. Hashtable:弃用,存key+value,key去重,无序,线程安全,方法加锁-效率低
  4. ConcurrentHashMap:存key+value,key去重,无序,线程安全,局部加锁、CAS-效率高
  5. TreeMap:存key+value,针对于key进行自然排序
  6. Properties:配置文件
2 HashMap
2.1 HashMap的使用
package com.qf.hashmap_class;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test01 {
	/**
	 * 知识点: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();
		
		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.2 HashMap的特点
package com.qf.hashmap_class;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test02 {
	/**
	 * 知识点: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);
		}
		
	}
}

2.3 HashMap的面试题
package com.qf.hashmap_class;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test03 {
	/**
	 * 知识点:HashMap的面试题
	 * 
	 * 需求:给HashMap的value排序
	 * 思路:
	 * 		HashMap 获取映射关系对象的Set集合 -> ArrayList对象 -> list.sort(外置比较器) 
	 */
	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();
		
		//将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
3.1 LinkedHashMap的使用(同HashMap)
3.2 LinkedHashMap的特点
package com.qf.linkedhashmap_class;

import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Test02 {
	/**
	 * 知识点: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);
		}
	}
}

4 Hashtable
4.1 Hashtable的使用(同HashMap)
4.2 Hashtable的特点
public class Test02 {
	/**
	 * 知识点: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);
		}
	}
}
5 ConcurrentHashMap
5.1 ConcurrentHashMap的使用(同HashMap)
5.2 ConcurrentHashMap的特点
package com.qf.concurrenthashmap_class;

import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class Test02 {
	/**
	 * 知识点: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);
		}
	}
}

6 TreeMap
6.1 TreeMap的使用(同HashMap)
6.2 TreeMap的特点
package com.qf.treemap_class;

import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class Test02 {
	/**
	 * 知识点:TreeMap的特点
	 * 
	 * 特点:针对于key进行自然排序
	 */
	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);
		}
	}
}

7 Properties

public class Test01 {
	/**
	 * 知识点:Properties
	 * @throws IOException 
	 */
	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);
	}
}

7 各种集合的应用场景

ArrayList:存数据,线程不安全

LinkedList:队列模式、栈模式,线程不安全

Vector:弃用,线程安全

Stack:弃用,线程安全

HashSet:去重+无序,线程不安全

LinkedHashSet:去重+有序,线程不安全

TreeSet:排序,线程不安全

HashMap:存key+value,key去重,无序,线程不安全

LinkedHashMap:存key+value,key去重,有序,线程不安全

Hashtable:弃用,存key+value,key去重,无序,线程安全,方法加锁-效率低

ConcurrentHashMap:存key+value,key去重,无序,线程安全,局部加锁、CAS-效率高

TreeMap:存key+value,针对于Key排序

Properties:配置文件

  • 18
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值