1. 程式人生 > >java集合map體系思維導圖整理

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區別

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);
		
	}
}