java集合map体系思维导图整理

开始时间:2018年8月23日20:00:37 | 2018年8月22日14:30:12 | 2018年8月23日14:30:45

结束时间:2018年8月23日21:47:03 | 2018年8月22日16:47:20 | 2018年8月23日18:39:48

累计时间:8小时

知识点补充:

hashtable已经基本废弃没有出现在思维导图里面,但有公司面试会让你比较hashmap与hashtable区别

Hashtable 和 HashMap 两个类的区别: 
   (1)时间上: JDK1.0 JDK1.2
   (2)NULL 
   (3)同步和异步上回答。 :https://blog.csdn.net/wangxing233/article/details/79452946

Map :映射结构: 
 1:框架结构: 
  Map:接口:
   |---HashMap: 
   |---Hashtable
   |---TreeMap 
   |---Property
 
 2:map集合的特点: 
     Map集合当中存放的都是键和值:  
              键和值的映射关系称之为:键值对    Entry<k,v>
              键唯一: 不能重复: 
              一个键只能映射一个值: 


 2: Map接口:  
    put(key,value); null 键重复,返回旧值。 
    remove(key); 返回key 对用的value值。 
    clear(); 
    size(); 获得映射关系的个数: 
    containsKey(key); 
    containsValue(value); 
    isEmpty(); Entry个数为0, 返回true。 
    putAll();
 
 3:API方法: 
   put(key ,value); 存放键值对: 在map集合当中键不能重复。 
     返回值:当键不重复, 返回值为null 。 
                    当键重复, 返回旧值, 新值将原来的旧值替代。
   putAll(Map<? extends K,? extends V> m) 

put(key,value); null 键重复,返回旧值。 
    remove(key); 返回key 对用的value值。 
    clear(); 
    size(); 获得映射关系的个数: 
    containsKey(key); 
    containsValue(value); 
    isEmpty(); Entry个数为0, 返回true。 
    putAll();
      remove(key);根据key 将key 和value 的映射关系移除。
   返回值为: key对应的值: 
   
   clear(); 清空集合: 
   
   size();  获得集合当中映射关系的个数: 
   
   isEmpty(); 当集合当中映射关系个数0时,返回true、。 
   
   containsKey(key)判断是否包含指定的key
   
   containsValue(value) 判断是否包含指定的value。 

 Map与Collection的区别

  • 1.Map 存储的是键值对形式的元素,键唯一,值可以重复。
  • 2.Collection 存储的是单列元素,子接口Set元素唯一,子接口List元素可重复。
  • 3.Map集合的数据结构值针对键有效,跟值无关,Collection集合的数据结构是针对元素有效

HashMap

1 存放基本类型的元素和遍历

package 面向对象;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapDemo01 {
	public static void main(String[] args) {
		/*
		 * map 集合的遍历:
		 */
		
		Map<String ,String> map = new HashMap<String,String>();
		
		map.put("name", "wangda");
		map.put("age", "12");
		map.put("sex", "nan");
		map.put("birthday", "1990-01-01");
		map.put("birthday1", "1990-01-01");
		
		//获得Map集合当中的某个元素:  根据指定的key 获得 对应的value: 
		String age = map.get("age");
		System.out.println(age);
		
		
		//方式一: 
		/*	Set<String> keySet = map.keySet();
		//获得迭代器: 
		Iterator<String> it = keySet.iterator();
		while(it.hasNext()){
			String key = it.next();//获得每一个key。 
			//通过key 获得key对应的值: 
			String value = map.get(key);
			System.out.println("key:"+key+" value:"+value);
		}
		
		
		for(String key: map.keySet()){
			// key 获得的每一个键: 
			String value = map.get(key);
			System.out.println("key:"+key+" value:"+value);
		}
		*/
		
		
		//方式二: 
		//获得所有的键值对: Set集合当中: 
		/*Set<Map.Entry<String,String>> entrys = map.entrySet();
		
		Iterator<Entry<String, String>> it = entrys.iterator();
		
		while(it.hasNext()){
			//遍历 得到每一个entry。 
			Entry<String, String> entry = it.next();
			
			//使用Entry 对象的方法: 
			// 获得key
			String key = entry.getKey();
			//获得value
			String value = entry.getValue();
			
			System.out.println(key+"::"+value);
		}*/
		
		
		//使用for简化上述方式: 
		for(Map.Entry<String,String> entry:map.entrySet()){
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key+"::::"+value);
		}
		
		//遍历方式三: 
		System.out.println("====");
		Collection<String> values = map.values();
		Iterator<String> it = values.iterator();
		while(it.hasNext()){
			String value = it.next();
			System.out.println(value);
		}
	}
}

   

2 存放自定义对象和遍历

// hashMap 在存放的时候,根据 key 存放的: 
        /*
         * 先去计算key 的哈希值: 如果哈希值相等,哈希冲突。 
         * 会继续调用equals方法比较具体的值, 
         * 如果值相同, 不存。 
         * 值不相同, 会存放: 
         */

package 面向对象;

import java.util.HashMap;



public class HashMapDemo02 {
	/*
	 * 存放自定义对象:以及对象的地址信息: 
	 * 分析: 对象和add地址 有一一对应的关系, 所以讲对象的信息存放在Map集合当中: 
	 * 么每个学生对应一个add信息: 
	 * 
	 *  存放的要求::自定义对象要求唯一: 
	 *  
	 *  
	 */
	
	public static void main(String[] args) {
		//创建一个HashMap对象: 
		HashMap<Student,String> map = new HashMap<Student,String>();
		
		
		map.put(new Student(1001,"王达"), "北京");
		System.out.println("====");
		map.put(new Student(1001,"王达"), "北京");
	/*	map.put(new Student(1002,"韩梅梅"), "山西");
		map.put(new Student(1003,"李明"), "安徽");
		map.put(new Student(1004,"李雷"), "河北");*/
		
		
		for(Student key:map.keySet()){
			String add = map.get(key);
			System.out.println(key+":"+add);
		}
	}
}

TreeMap

1 存放一般的数据类型以及遍历。

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapDemo01 {
	public static void main(String[] args) {
		/*
		 * 演示TreeMap集合: 
		 * 
		 * treeMap 当中存放的key String类型: String类型具备默认的比较规则: 字典顺序: 
		 * 
		 * 上述使用的String的默认的比较规则; 现在的需求: 使用字符串的降序排序:  
		 * 默认的排序规则不能满足需求: 
		 * 可以在定义treeMap 的时候,指定集合的存放规则: 
		 * 
		 */
		
		TreeMap<String,String> t = new TreeMap<String,String>();
		
		t.put("a", "ajax");
		t.put("j", "java"); 
		t.put("s", "spring");
		t.put("h", "hibernate"); 
		
		System.out.println(t);
		
		
		// 定义一个TreeMap集合: 指定具体的比较器; 
		TreeMap<String,String> t1 = new TreeMap<String,String>(new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
			    int num =  o2.compareTo(o1);
				return num;
			}
			
		});
		t1.put("a", "ajax");
		t1.put("j", "java"); 
		t1.put("s", "spring");
		t1.put("h", "hibernate");
		System.out.println(t1);
		
		
	}
}

2 存放自定义对象 

 因为自定义对象没有相应的比较规则,需要自己写,如果超过两次另外封装。

本例子中:

解决: 创建Student对象实现: Comparable接口: 

student.java

package com.yidongxueyuan.domain;

public class Student implements Comparable<Student>{
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Student(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
	
	@Override
	public int hashCode() {
		System.out.println("hashCode ....");
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		//
		System.out.println("equals方法...");
		
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	@Override
	public int compareTo(Student o) {
		/*
		 * 根据id的升序: id相同,name的降序:
		 */
		int num1= this.id-o.id;
		int num2= o.name.compareTo(this.name);
		return num1==0?num2:num1;
	} 
	

}


import java.util.Comparator;
import java.util.TreeMap;

import com.yidongxueyuan.domain.Student;

public class TreeMapDemo02 {
	public static void main(String[] args) {
		/*
		 * 往TreeMap集合当中存放自定义对象:
		 * 
		 * 
		 * 自定义的元素不具备任何的比较规则: 
		 * 直接存放: ClassCastException: 
		 * 
		 * 解决: 创建Student对象实现: Comparable接口: 
		 */
		
		//定义集合对象:
		TreeMap<Student,String> t= new TreeMap<Student,String>();
		
		t.put(new Student(1003,"pengpeng"), "beijing");
		t.put(new Student(1002,"zichen"), "beijing");
		t.put(new Student(1001,"songkun"), "beijing");
		t.put(new Student(1004,"chenzhang"), "beijing");
		t.put(new Student(1004,"xxx"), "beijing");
		t.put(new Student(1004,"xxx"), "tianjin");
		
		System.out.println(t);
		
		
		//创建TreeMap 指定自己的比较器: 
		TreeMap<Student,String> t1= new TreeMap<Student,String>(new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				
				/*
				 * name 的升序:  id 升序:
				 */
				int num1 = o1.getName().compareTo(o2.getName()); 
				int num2 = o1.getId() - o2.getId(); 
				return num1==0?num2:num1;
			}
		});
		t1.put(new Student(1003,"pengpeng"), "beijing");
		t1.put(new Student(1002,"zichen"), "beijing");
		t1.put(new Student(1001,"songkun"), "beijing");
		t1.put(new Student(1004,"chenzhang"), "beijing");
		t1.put(new Student(1004,"xxx"), "beijing");
		t1.put(new Student(1005,"xxx"), "beijing");
		
		System.out.println(t1);
		
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41517071/article/details/81986943