0
点赞
收藏
分享

微信扫一扫

HashMap 、LinkedHashMap 、 Hashtable 、ConcurrentHashMap的使用区别和Collections -- 集合工具类

LinkedHashMap

LinkedHashMap的使用

public static void main(String[] args) {

		LinkedHashMap<String,Integer> map = new LinkedHashMap<>();

		//添加元素
		Integer put1 = map.put("大文", 25);
		Integer put2 = map.put("小文", 26);
		Integer put3 = map.put("小王", 29);
		Integer put4 = map.put("小李", 28);
		Integer put5 = map.put("小博", 21);
		Integer put6 = map.put("小黄", 26);
		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
		System.out.println("put6:" + put6);//null


		//替换,返回被替换的值
		Integer put = map.put("小黄", 27);
		System.out.println("put:" + put);//26

		//替换,返回被替换的值
		Integer replace1 = map.replace("小黄", 28);
		System.out.println("replace1:" + replace1);//27

		//替换,返回是否替换成功的boolean值
		boolean replace2 = map.replace("小黄", 28, 29);//key - 被替换值 -替换值
		System.out.println("replace2:" + replace2);//true

		//将newMap1中所有的映射关系添加到map集合中
		LinkedHashMap<String, Integer> newMap1 = new LinkedHashMap<>();
		newMap1.put("aaa", 10);
		newMap1.put("bbb", 20);
		newMap1.put("ccc", 30);
		newMap1.put("ddd", 40);
		newMap1.put("ddd", 50);
		map.putAll(newMap1);

		//如果key存在则返回value,如果key不存在就添加
		Integer putIfAbsent = map.putIfAbsent("小王", 123);
		System.out.println("putIfAbsent:" + putIfAbsent);//29

		//通过key获取对应的value值
		Integer integer = map.get("小李");
		System.out.println("通过key获取对应的value值:" + integer);

		//通过key获取对应的value值,如果没有key就返回默认值666
		Integer orDefault = map.getOrDefault("小王111", 666);
		System.out.println("通过key获取对应的value值:" + orDefault);

		//清空所有的元素
		//map.clear();

		System.out.println("判断map集合中是否包含某个key:" + map.containsKey("小王"));//true
		System.out.println("判断map集合中是否包含某个value:" + map.containsValue(28));//true
		System.out.println("判断map集合是否有元素:" + map.isEmpty());//false
		//有元素就返回false

		//根据key删除映射关系
		map.remove("小博");
		//根据key+value删除映射关系
		map.remove("小李", 28);

		System.out.println("获取集合元素个数:" + map.size());//8

		//获取map集合中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));//集合->数组->字符串

		System.out.println("---------------------");

		//遍历 -- 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()
		//思路:获取map中所有的映射关系对象(Entry),存入Set集合,遍历Set集合依次把Entry取出来,获取Entry中的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);
		}
		
	}

继承关系:

classs LinkedHashMap extends HashMap

特点:

有序 + key去重

Hashtable

Hashtable的使用

public static void main(String[] args) {

		Hashtable<String,Integer> map = new Hashtable<>();

		//添加元素
		Integer put1 = map.put("大文", 25);
		Integer put2 = map.put("小文", 26);
		Integer put3 = map.put("小王", 29);
		Integer put4 = map.put("小李", 28);
		Integer put5 = map.put("小博", 21);
		Integer put6 = map.put("小黄", 26);
		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
		System.out.println("put6:" + put6);//null


		//替换,返回被替换的值
		Integer put = map.put("小黄", 27);
		System.out.println("put:" + put);//26

		//替换,返回被替换的值
		Integer replace1 = map.replace("小黄", 28);
		System.out.println("replace1:" + replace1);//27

		//替换,返回是否替换成功的boolean值
		boolean replace2 = map.replace("小黄", 28, 29);//key - 被替换值 -替换值
		System.out.println("replace2:" + replace2);//true

		//将newMap1中所有的映射关系添加到map集合中
		Hashtable<String, Integer> newMap1 = new Hashtable<>();
		newMap1.put("aaa", 10);
		newMap1.put("bbb", 20);
		newMap1.put("ccc", 30);
		newMap1.put("ddd", 40);
		newMap1.put("ddd", 50);
		map.putAll(newMap1);

		//如果key存在则返回value,如果key不存在就添加
		Integer putIfAbsent = map.putIfAbsent("小王", 123);
		System.out.println("putIfAbsent:" + putIfAbsent);//29

		//通过key获取对应的value值
		Integer integer = map.get("小李");
		System.out.println("通过key获取对应的value值:" + integer);

		//通过key获取对应的value值,如果没有key就返回默认值666
		Integer orDefault = map.getOrDefault("小王111", 666);
		System.out.println("通过key获取对应的value值:" + orDefault);

		//清空所有的元素
		//map.clear();

		System.out.println("判断map集合中是否包含某个key:" + map.containsKey("小王"));//true
		System.out.println("判断map集合中是否包含某个value:" + map.containsValue(28));//true
		System.out.println("判断map集合是否有元素:" + map.isEmpty());//false
		//有元素就返回false

		//根据key删除映射关系
		map.remove("小博");
		//根据key+value删除映射关系
		map.remove("小李", 28);

		System.out.println("获取集合元素个数:" + map.size());//8

		//获取map集合中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));//集合->数组->字符串

		System.out.println("---------------------");

		//遍历 -- 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()
		//思路:获取map中所有的映射关系对象(Entry),存入Set集合,遍历Set集合依次把Entry取出来,获取Entry中的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);
		}
		
	}

特点:

无序 + key去重 + 线程安全(底层是方法上加锁了的 + 效率低)

ConcurrentHashMap

ConcurrentHashMap的使用\

public static void main(String[] args) {

		ConcurrentHashMap<String,Integer> map = new ConcurrentHashMap<>();

		//添加元素
		Integer put1 = map.put("大文", 25);
		Integer put2 = map.put("小文", 26);
		Integer put3 = map.put("小王", 29);
		Integer put4 = map.put("小李", 28);
		Integer put5 = map.put("小博", 21);
		Integer put6 = map.put("小黄", 26);
		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
		System.out.println("put6:" + put6);//null


		//替换,返回被替换的值
		Integer put = map.put("小黄", 27);
		System.out.println("put:" + put);//26

		//替换,返回被替换的值
		Integer replace1 = map.replace("小黄", 28);
		System.out.println("replace1:" + replace1);//27

		//替换,返回是否替换成功的boolean值
		boolean replace2 = map.replace("小黄", 28, 29);//key - 被替换值 -替换值
		System.out.println("replace2:" + replace2);//true

		//将newMap1中所有的映射关系添加到map集合中
		ConcurrentHashMap<String, Integer> newMap1 = new ConcurrentHashMap<>();
		newMap1.put("aaa", 10);
		newMap1.put("bbb", 20);
		newMap1.put("ccc", 30);
		newMap1.put("ddd", 40);
		newMap1.put("ddd", 50);
		map.putAll(newMap1);

		//如果key存在则返回value,如果key不存在就添加
		Integer putIfAbsent = map.putIfAbsent("小王", 123);
		System.out.println("putIfAbsent:" + putIfAbsent);//29

		//通过key获取对应的value值
		Integer integer = map.get("小李");
		System.out.println("通过key获取对应的value值:" + integer);

		//通过key获取对应的value值,如果没有key就返回默认值666
		Integer orDefault = map.getOrDefault("小王111", 666);
		System.out.println("通过key获取对应的value值:" + orDefault);

		//清空所有的元素
		//map.clear();

		System.out.println("判断map集合中是否包含某个key:" + map.containsKey("小王"));//true
		System.out.println("判断map集合中是否包含某个value:" + map.containsValue(28));//true
		System.out.println("判断map集合是否有元素:" + map.isEmpty());//false
		//有元素就返回false

		//根据key删除映射关系
		map.remove("小博");
		//根据key+value删除映射关系
		map.remove("小李", 28);

		System.out.println("获取集合元素个数:" + map.size());//8

		//获取map集合中所有的value
		Collection<Integer> values = map.values();
		System.out.println(Arrays.toString(values.toArray()));//集合->数组->字符串

		System.out.println("---------------------");

		//遍历 -- 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()
		//思路:获取map中所有的映射关系对象(Entry),存入Set集合,遍历Set集合依次把Entry取出来,获取Entry中的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去重 + 线程安全(底层局部加锁 + CAS,效率更高)

Collections -- 集合工具类

public static void main(String[] args) {

		ArrayList<Integer> list = new ArrayList<>();

		//批量添加数据
		Collections.addAll(list, 5,2,7,9,1,6,3,8,4);

		//排序 -- 外置比较
		list.sort(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return Integer.compare(o1, o2);
			}
		});

		//查询
		int binarySearch = Collections.binarySearch(list, 2);
		System.out.println("查询元素在数组中的下标:" + binarySearch);

		Integer max = Collections.max(list);
		System.out.println("最大值为:" + max);

		Integer min = Collections.min(list);
		System.out.println("最小值为:" + min);

		//替换
		Collections.fill(list, 888);//全部替换

		//遍历
		System.out.println(Arrays.toString(list.toArray()));

	}

HashMap 、LinkedHashMap 、 Hashtable 、ConcurrentHashMap的区别


特点的区别

HashMap

无序

key去重

线程不安全


LinkedHashMap

有序

key去重

线程不安全


Hashtable

无序

key去重

线程安全

方法上加锁,效率低,在多线程上已弃用

ConcurrentHashMap

无序

key去重

线程安全

局部加锁+CAS,效率高 ,在多线程上直接用

能否存储空键、空值的区别:

*/

能否存储空键、空值的区别:



HashMap

ok


LinkedHashMap

ok


Hashtable

no

会报空指针异常

ConcurrentHashMap

no

会报空指针异常,线程安全的不会允许有空指针存在

举报

相关推荐

0 条评论