黑馬程序員------集合(No.1)(Map集合、Map共性方法、keySet、entrySet)

---------------------- ASP.Net+Android+IO開發S.Net培訓、期待與您交流! ---------------------- 

 

微笑Map集合

 

Map集合:該集合存儲鍵值對。一對一往裏存。而且要保證鍵 的唯一性。

1.添加:

put(K key ,V value)

putAlll(Map<? extends K,?extends V> m)

 

2.刪除:

clear();

remove(Object key);

 

3.判斷:

containsValue(Object value)

containKey(Object key)

isEmpty()

 

4.獲取:

get(Object key)

size()

values();

entrySet();

keySet();

 

Map

  |----Hashtable:底層是哈希表數據結構,不可以存入null鍵和null值。該集合是線程同步的。JDK1.0,效率低。

  |----HashMap:底層是哈希表數據結構,允許使用null鍵和null值,該集合是不同步的。JDK1.2,效率高。

  |----TreeMap:底層是二叉樹數據結構。線程不同步,可以用於給map集合中的鍵進行排序。

 

和Set很像,其實,Set的底層就是使用了Map集合。

Map共性方法

import java.util.*;
class MapDemo{
	public static void main(String[] args){
		Map<String,String> map = new HashMap<String,String>();
		//添加元素,如果出現添加時,相同的鍵,後添加的值會覆蓋原有的值,並且該方法會返回被覆蓋的值
		map.put("01","zhangsan1");
		map.put("02","zhangsan2");
		map.put("03","zhangsan3");
		map.put("04","zhangsan4");
		System.out.println("containsKey:"+map.containsKey("002"));
		//System.out.println("remove:"+map.remove("02"));
		System.out.println("get:"+map.get("023"));
		//HashTable可以通過get方法的返回值來判斷一個鍵是否存在。通過返回null來判定
		//特殊情況。
		map.put("05",null);
		System.out.println("get:"+map.get("05"));
		//獲取map集合中所有的值
		Collection<String> coll = map.values();
		System.out.println(coll);
		System.out.println(map);
	}
}


 通過迭代方法得到元素。

import java.util.*;
class MapDemo2{
	public static void main(String[] args){
		Map<String,String> map = new HashMap<String,String>();
		map.put("01","zhangsan1");
		map.put("02","zhangsan2");
		map.put("03","zhangsan3");
		map.put("04","zhangsan4");
		System.out.println(map);
		//Map.Entry其實Entry也是一個接口,它是Map接口的一個內部接口。
		Set<Map.Entry<String,String>> entrySet = map.entrySet();
		Iterator<Map.Entry<String,String>> it = entrySet.iterator();
		while(it.hasNext()){
			Map.Entry<String,String> me = it.next();
			System.out.println(me.getKey()+":"+me.getValue());
		}
	}
}


 需求:對學生對象的姓名進行自然排序。

因爲數據是以鍵值對的形式存在的。

所以要使用可以排序的Map集合。TreeMap。

 

 有兩種方式,均在下面的程序中體現。

 

import java.util.*;
/*
 需求:對學生對象的姓名進行自然排序。
因爲數據是以鍵值對的形式存在的。所以要使用可以排序的Map集合。TreeMap。
*/
class Student implements Comparable<Student>{
	private String name;
	private int age;
	Student(String name,int age){
		this.name = name;
		this.age = age;
	}
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}
	public void setAge(int age){
		this.age=age;
	}
	public int getAge(){
		return age;
	}
	//重寫hashCode方法
	public int hashCode(){
		return name.hashCode()+age*15;
	}
	//重寫equals方法
	public boolean equals(Object obj){
		if(!(obj instanceof Student))
			throw new ClassCastException("類型不匹配");
		Student s = (Student)obj;
		return this.getName().equals(s.getName())&&this.getAge()==s.getAge();
	}
	//重寫compareTo方法
	public int compareTo(Student s){
		int num = this.getName().compareTo(s.getName());
		if (num==0)
		{
			return new Integer(this.getAge()).compareTo(new Integer(s.getAge()));	
		}
		return num;
	}
	//重寫toString方法
	public String toString(){
		return "["+this.getName()+":"+this.getAge()+"]";
	}
}
class MapDemo3{
	public static void main(String[] args){
		//使用TreeMap要按順序,方法一需要傳入實現Comparator接口的子類
		//TreeMap<Student,String> hm = new TreeMap<Student,String>(new MyComp());
		//使用TreeMap要按順序,方法二需要傳入Student實現Comparable接口。
		TreeMap<Student,String> hm = new TreeMap<Student,String>();
		hm.put(new Student("xiaozi5",12),"beijng1");
		hm.put(new Student("xiaozi7",13),"beijng3");
		hm.put(new Student("xiaozi2",15),"beijng2");
		hm.put(new Student("xiaozi3",16),"beijng4");
		hm.put(new Student("xiaozi2",11),"beijng5");
		
	//	System.out.println(hm);
		Set<Map.Entry<Student,String>> hs = hm.entrySet();
		Iterator<Map.Entry<Student,String>> it = hs.iterator();
		while(it.hasNext()){
			Map.Entry<Student,String> me = it.next();
			System.out.println(me);
		}
	}
}
class MyComp implements Comparator<Student>{
	public int compare(Student s1,Student s2){
		int num = s1.getName().compareTo(s2.getName());
		if (num==0)
		{
			return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
		}
		return num;
	}
}


---------------------- ASP.Net+Android+IOS開發.Net培訓、期待與您交流! ---------------------- 

詳情請查看:http://edu.csdn.net

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章