Map接口的实现类(HashMap,LinkedLinkedHashMap,Hashtable,ConcurrentHashMap,TreeMap,Properties)

Map集合:

       Map: 存两个值(Key-Value),不可以获取迭代器,不能遍历(Map可以间接遍历)

 
 

HashMap:

HashMap的使用:

public class Test01 {
	
	public static void main(String[] args) {
		
		HashMap<String, Integer> map = new HashMap<>();
		
		//添加元素
		map.put("小明", 22);
		map.put("小白", 23);
		map.put("小红", 26);
		map.put("小玉", 18);
		
		
		//将newMap中所有的元素添加到map集合中
		HashMap<String, Integer> newMap = new HashMap<>();
		newMap.put("a", 10);
		newMap.put("b", 20);
		newMap.put("c", 30);
		newMap.put("d", 40);
		map.putAll(newMap);
		
		
		
		//如果key存在就获取value值,如果不存在就添加
		Integer p = map.putIfAbsent("小雨", 21);
		System.out.println("putIfAbsent:" + p);
		
		
		//通过Key获取到对应的Value
		Integer i1 = map.get("小玉");
		System.out.println("通过Key获取对应的value:" + i1);
		
		
		//通过Key获取对应的value,如果key不存在则返回默认值
		Integer i2 = map.getOrDefault("麻生希111", 666);
		System.out.println("通过Key获取对应的value:" + i2);
		
		//清空集合中的元素
		//map.clear();
		
		
		System.out.println("判断集合中是否有指定的key:" + map.containsKey("小雨"));
		
		System.out.println("判断集合中是否有指定的value:" + map.containsValue(21));
		
		System.out.println("判断集合中是否没有元素:" + map.isEmpty());
		
		
		
		//通过key删除映射关系(key+value)
		map.remove("a");
		
		//通过key+value删除映射关系(key+value),key和value要正确。
		map.remove("c", 30);
		
		//通过key替换value
		map.replace("小白", 20);
		
		//通过key+value替换value
		map.replace("小红", 26, 25);
		
		//获取映射关系的个数(映射关系内包含了key和value)
		int size = map.size();
		System.out.println("获取映射关系的个数:" + size);
		
		//获取map中所有的value
		Collection<Integer> values = map.values();
		//将集合转换为数组,再将数组转换为字符串
		System.out.println(Arrays.toString(values.toArray()));
		
		System.out.println("-----------------------");
		
		
		
	}
}

 

遍历:
		//遍历:
		//思路:获取map集合中所有的key放在一个Set集合中,
		//遍历Set集合获取出key,再通过key获取到Map集合中对应的value
		
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			
			Integer value = map.get(key);
			System.out.println(key + " -- " + value);
			
		}
		
		System.out.println("-----------------------");
		
		//遍历:
		//思路:获取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的注意事项:

put方法即使添加也是替换。

public static void main(String[] args) {
		
		HashMap<String, Integer> map = new HashMap<>();		
		
		//put第一次添加数据,返回为null
		Integer p1 = map.put("小明", 25);
		Integer p2 = map.put("小红", 23);
		Integer p3 = map.put("小白", 22);
		
		System.out.println("p1:" + p1);//null
		System.out.println("p2:" + p2);//null
		System.out.println("p3:" + p3);//null
		
		//使用put添加数据,如果map中有key,就替换value值,返回被替换的值
		Integer put4 = map.put("小明", 23);
		System.out.println("put4:" + put4);//25
	
	
		}

 
 

面试题:针对于HashMap的value排序
public static void main(String[] args) {
		
		HashMap<String, Integer> map = new HashMap<>();		
		
		map.put("小明", 22);
		map.put("小白", 23);
		map.put("小红", 26);
		map.put("小玉", 18);
		
		//将map的映射关系对象取出,返回Set集合
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		
		//将Set集合转换为ArrayList集合
		ArrayList<Entry<String,Integer>> list = new ArrayList<>(entrySet);
		
		//利用ArrayList的sort方法去排序
		list.sort(new Comparator<Entry<String,Integer>>() {

			@Override
			public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
				Integer v1 = o1.getValue();
				Integer v2 = o2.getValue();
				return Integer.compare(v1, v2);
			}
		});
		
		//遍历ArrayList
		for (Entry<String, Integer> entry : list) {
			System.out.println(entry);
		}
		
	
	}

 

HashMap的特点:

HashMap的特点:无序 + key去重
 
       1.HashMap的key不允许重复,Key是唯一的

       2.HashMap的value允许重复

  
 public static void main(String[] args) {
		
		HashMap<String, Integer> map = new HashMap<>();		
		
		map.put("小明", 22);
		map.put("小白", 23);
		map.put("小红", 26);
		map.put("小玉", 18);
		map.put("小明", 24);
		map.put("小明", 28);
		
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			System.out.println(entry);
		}
		
	
	}

 

 

 

LinkedHashMap:

LinkedHashMap的使用:

       LinkedHashMap是HashMap的子类,HashMap如何使用,LinkedHashMap就怎么使用!!!

 

LinkedLinkedHashMap的特点:

LinkedLinkedHashMap 的特点: 有序+key去重

 public static void main(String[] args) {
		
		HashMap<String, Integer> map = new HashMap<>();		
		
		map.put("小明", 22);
		map.put("小白", 23);
		map.put("小红", 26);
		map.put("小玉", 18);
		map.put("小明", 24);
		map.put("小明", 28);
		
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		for (Entry<String, Integer> entry : entrySet) {
			System.out.println(entry);
		}
		
	
	}

 
 

Hashtable:

Hashtable的使用:

Hashtable和HashMap使用一样!!!

Hashtable的特点:

Hashtable的特点:无序+key去重+线程安全

 
 

ConcurrentHashMap:

ConcurrentHashMap的使用:

ConcurrentHashMap和HashMap使用一样!!!

ConcurrentHashMap的特点:

ConcurrentHashMap的特点:无序+key去重+线程安全

 
 

前四者的区别与联系:

HashMap vs LinkedHashMap vs Hashtable vs ConcurrentHashMap

区别:

  1. 存储null 键的情况:

    ​ HashMap,LinkedHashMap允许存储null键。

    ​ Hashtable,ConcurrentHashMap 不允许存储null键。
     

  2. 应用场景的区别:

    ​ HashMap:无序+key去重。

    ​ LinkedHashMap:有序+key去重

    ​ Hashtable:无序+key去重+线程安全(在方法上加锁,效率低,已弃用)

    ​ ConcurrentHashMap:无序+key去重+线程安全(局部加锁+CAS实现线程安全,效率高,多线程下使用ConcurrentHashMap)

 

 

TreeMap:

TreeMap的使用:

TreeMap和HashMap使用一样!!!

 

TreeMap的特点:

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

//研究内置比较器的使用 -- Comparable

public class Student implements Comparable<Student>{
    
	//排序规则:按照年龄排序
	@Override
	public int compareTo(Student o) {
		//this表示添加到TreeSet中的学生对象
		//o表示TreeSet中的学生对象
		return this.age - o.age;
	}
}


//研究外置比较器的使用 -- Comparator
	public static void main(String[] args) {
		
    	//按照名字长度排序,长度一致按照年龄排序
		//扩展:new Comparator 匿名内部类的使用场景。
		TreeMap<Student, String> map=new TreeMap<>(new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				if(o1.equals(o2)){
					return 0;
				}
				
				int nameLen1 = o1.getName().length();
				int nameLen2 = o2.getName().length();
				if(nameLen1 != nameLen2){
					//return nameLen1 - nameLen2;
					return Integer.compare(nameLen1, nameLen2);
				}
				
				int age1 = o1.getAge();
				int age2 = o2.getAge();
				if(age1 != age2){
					return Integer.compare(age1, age2);
				}
				return 1;
			}
		});
    

比较器的优先级别:外置比较器 > 内置比较器

 
 

Properties:

应用场景:配置文件

Properties的配置文件中的内容都是以键值对的形式存在。

public static void main(String[] args) throws IOException {
		
		//创建配置文件对象
		Properties p = new Properties();
		
		/*
		 * 将配置文件加载到配置文件对象中
		 * 
		 * 底层实现:
		 * 		1.将配置文件中的数据读取出
		 * 		2.将键值对写入父类对象(Hashtable) -- super.put(key,value)
		 */
		p.load(Test01.class.getClassLoader().getResourceAsStream("dbconfig.properties"));
		
		//获取配置文件里的数据
		//底层实现:利用父类对象(Hashtable).get(key) 
		String username = p.getProperty("username");
		String password = p.getProperty("password");
		System.out.println(username + " -- " + password);
		
		//注意1:如果key不存在,返回的为null
		String name = p.getProperty("name");
		System.out.println(name);
		
		//注意2:如果key不存在,就返回默认值
		String url = p.getProperty("url", "默认值");
		System.out.println(url);
		
		//将键值对写入到父类对象中(Hashtable),并不是写入配置文件中
		p.setProperty("author", "OYO");
		String author = p.getProperty("author");
		System.out.println(author);
	}
  • 44
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值