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> mapList=new ArrayList<>();
		Map hashMapCollection=new HashMap<>();
		hashMapCollection.put("class", 4);
		hashMapCollection.put("add", 9);
		hashMapCollection.put("zoom", 18);
		
		Map hashMapCollection1=new HashMap<>();
		hashMapCollection1.put("class1", 5);
		hashMapCollection1.put("zoom", 9);
		
		mapList.add(hashMapCollection);
		mapList.add(hashMapCollection1);

		List> newmapList=new ArrayList<>();
		newmapList=mapList.stream().filter(r-> r.containsKey("add")).collect(Collectors.toList());
		
		for (Map map : newmapList) {
			 for (String key : map.keySet()) {
				System.out.println("Key:"+key+"---Value:"+map.get(key));
			}
		}
		System.out.println("---------------------------------------------------");
		
		//-------------TreeMap存储数据默认是升序排序的
		Map treeMap=new TreeMap();
		treeMap.put("boy", 2);
		treeMap.put("dad", 3);
		treeMap.put("apple", 1);
		for (Map.Entry map : treeMap.entrySet()) {
			System.out.println("Tree Key:"+map.getKey()+"---Value:"+map.getValue().toString());
		}
		System.out.println("---------------------------------------------------");
		
		//-------------linkedMap存储数据会根据你put值的顺序来进行排序的,先进先出
		Map linkedMap=new LinkedHashMap();
		linkedMap.put("ddd", 3);
		linkedMap.put("zzz", 2);
		linkedMap.put("aaa", 1);
		Iterator> Iterator=linkedMap.entrySet().iterator();
		while(Iterator.hasNext()){
			Entry entry=Iterator.next();
			System.out.println("Linked Key:"+entry.getKey()+"---Value:"+entry.getValue());
		}
		System.out.println("---------------------------------------------------");
		
		//-------------实现将List里面的实体类People中的no和name凑成键值对
		List pList=new ArrayList();
		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 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
	}
}

 

你可能感兴趣的:(JAVA,hashmap)