JAVA-HashMap,LinkedHashMap,TreeMap之间的区别和运用

package cn.project.demo;

public class People {
	
	public String name;
	public int no;
	public int age;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "People [name=" + name + ", no=" + no + ", age=" + age + "]";
	}
}
package cn.project.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class MapClass {

	//HashMap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快的访问速度。遍历时,取得数据的顺序是完全随机的。
	//LinkedHashMap保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的。也可以在构造时带参数,按照应用次数排序。在遍历的时候会比HashMap慢,不过有种情况例外:当HashMap容量很大,实际数据较少时,遍历起来可能会比LinkedHashMap慢。因为LinkedHashMap的遍历速度只和实际数据有关,和容量无关,而HashMap的遍历速度和他的容量有关
	//TreeMap实现SortMap接口,能够把它保存的记录根据键排序。默认是按键值的升序排序,也可以指定排序的比较器,当用Iterator 遍历TreeMap时,得到的记录是排过序的。
	public static void main(String[] args) {
		
		//-------------HashMap存储数据是无序的
		List<Map<String,Integer>> mapList=new ArrayList<>();
		Map<String,Integer> hashMapCollection=new HashMap<>();
		hashMapCollection.put("class", 4);
		hashMapCollection.put("add", 9);
		hashMapCollection.put("zoom", 18);
		
		Map<String,Integer> hashMapCollection1=new HashMap<>();
		hashMapCollection1.put("class1", 5);
		hashMapCollection1.put("zoom", 9);
		
		mapList.add(hashMapCollection);
		mapList.add(hashMapCollection1);

		List<Map<String,Integer>> newmapList=new ArrayList<>();
		newmapList=mapList.stream().filter(r-> r.containsKey("add")).collect(Collectors.toList());
		
		for (Map<String, Integer> map : newmapList) {
			 for (String key : map.keySet()) {
				System.out.println("Key:"+key+"---Value:"+map.get(key));
			}
		}
		System.out.println("---------------------------------------------------");
		
		//-------------TreeMap存储数据默认是升序排序的
		Map<String,Integer> treeMap=new TreeMap<String, Integer>();
		treeMap.put("boy", 2);
		treeMap.put("dad", 3);
		treeMap.put("apple", 1);
		for (Map.Entry<String, Integer> map : treeMap.entrySet()) {
			System.out.println("Tree Key:"+map.getKey()+"---Value:"+map.getValue().toString());
		}
		System.out.println("---------------------------------------------------");
		
		//-------------linkedMap存储数据会根据你put值的顺序来进行排序的,先进先出
		Map<String,Integer> linkedMap=new LinkedHashMap<String, Integer>();
		linkedMap.put("ddd", 3);
		linkedMap.put("zzz", 2);
		linkedMap.put("aaa", 1);
		Iterator<Entry<String, Integer>> Iterator=linkedMap.entrySet().iterator();
		while(Iterator.hasNext()){
			Entry<String, Integer> entry=Iterator.next();
			System.out.println("Linked Key:"+entry.getKey()+"---Value:"+entry.getValue());
		}
		System.out.println("---------------------------------------------------");
		
		//-------------实现将List里面的实体类People中的no和name凑成键值对
		List<People> pList=new ArrayList<People>();
		People people1=new People();
		people1.no=1;
		people1.name="张三";
		people1.age=21;
		pList.add(people1);
		
		People people2=new People();
		people2.no=2;
		people2.name="李四";
		people2.age=22;
		pList.add(people2);
		
		People people3=new People();
		people3.no=3;
		people3.name="王五";
		people3.age=23;
		pList.add(people3);
		//善于运用map转换数据库表的所有数据,以便后续通过key值获取Value值
		Map<Integer, String> mapStu=pList.stream().collect(Collectors.toMap(People::getNo, People::getName));
		System.out.println(mapStu.get(2));//李四
		System.out.println(mapStu.get(3));//王五
		System.out.println(mapStu.get(4));//null
	}
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值