首页 > 编程语言 >Java集合-Map家族

Java集合-Map家族

时间:2024-06-04 20:29:43浏览次数:17  
标签:Map Java map System put key 集合 println out

集合-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:配置文件

标签:Map,Java,map,System,put,key,集合,println,out
From: https://blog.csdn.net/weixin_47234283/article/details/139427063

相关文章

  • Java面试八股文day02
    系列文章目录文章目录前言跟着我的节奏拿下Java面试八股文二、容器1.java容器都有哪些?2.Collection和Collections有什么区别?java.util.Collection是一个集合接口(集合类的一个顶级接口)。它提供了对集合对象进行基本操作的通用接口方法。Collection接口在Java......
  • 【Java基础】线程的概念、特点及创建线程的三种方式
    线程概念程序:是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念。进程:是执行程序的一次执行过程,她是一个动态的概念,是系统资源分配的单位。正在运行的程序在内存中开辟相应的空间。线程:负责程序执行的一条执行路径,是CPU调度和执行的单位。进程和线程......
  • 重学java 60.IO流 ① File类
    明年此日青云去,却笑人间举子忙                            ——24.6.4知识回顾1.HashMap        a.特点:无序,无索引,key唯一,线程不安全,可以存null键null值        b.数据结构:哈希表      ......
  • 2024最新拼多多Java面试题(现场五面),全面涵盖Java高级到高并发,字节跳动java面试算法没
    总结其他的内容都可以按照路线图里面整理出来的知识点逐一去熟悉,学习,消化,不建议你去看书学习,最好是多看一些视频,把不懂地方反复看,学习了一节视频内容第二天一定要去复习,并总结成思维导图,形成树状知识网络结构,方便日后复习。这里还有一份很不错的《Java基础核心总结笔记》,......
  • Java毕业设计 基于springboot vue大学新生报到系统
    Java毕业设计基于springbootvue大学新生报到系统SpringBoot大学新生报到系统功能介绍首页图片轮播报道流程流程详情校园公告公告详情登录注册个人中心更新信息学生后台登录个人中心修改密码个人信息学生报到班级分配宿舍分配缴费信息管理员登录个......
  • java中JDBC的实际使用注意事项
    连接被自动关闭:connectionisclose  如果你的jdbc写了这样的代码,就会出现第一次执行完次方法后自动关闭连接: 我的项目中使用连接池,所以连接不必关闭,有连接池做缓存。可以更改为try{Connectionconn=this.connection;xxxxxxxx}catch{xxxxxxxx}......
  • java项目部署脚本
    一、java项目部署脚本示例在Java项目中,部署脚本通常依赖于项目的构建工具(如Maven或Gradle)以及部署环境(如Docker、Tomcat、Kubernetes等)。以下是一个基于Maven和Shell脚本的Java项目部署示例,假设我们正在将应用部署到Linux服务器上的Tomcat容器中。1.前提条件Linux服务器已安......
  • java项目部署脚本
    一、java项目部署脚本示例在Java项目中,部署脚本通常依赖于项目的构建工具(如Maven或Gradle)以及部署环境(如Docker、Tomcat、Kubernetes等)。以下是一个基于Maven和Shell脚本的Java项目部署示例,假设我们正在将应用部署到Linux服务器上的Tomcat容器中。1.前提条件Linux服务器已......
  • Java读写xml文件
    前言使用dom4j库java读xml文件示例代码:SAXReadersaxReader=newSAXReader();Documentdoc=null;try{ FileInputStreamfin=newFileInputStream("D://doc_f/student.xml"); document=saxReader.read(fin); Elementroot=document.getRootElement();//获取......
  • Docker---java.sql.SQLNonTransientConnectionException: Could not create connectio
    文章目录一、问题场景二、问题分析及解决2.1问题分析2.2问题解决2.2.1有改动未重启容器2.2.2数据库配置不对三、结束一、问题场景使用docker容器控制数据库时,启动服务报错:java.sql.SQLNonTransientConnectionException:Couldnotcreateconnectiontodat......