java基礎<集合框架——Map集合>


Map概述

一、概述

此接口是 Java Collections Framework 的成員。

將鍵映射到值的對象。一個映射不能包含重複的鍵;每個鍵最多隻能映射到一個值。

Map又被叫做——雙列集合

二、Map集合共性功能

1.添加

pub(K key,V value)

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

2.刪除

clear()

remove(Object key)

3.判斷

containsValue(Object value)

containsKey(Object key)

isEmpty()

4.獲取

get(Object key)

size()

values()

 

entrySet()

keySet()

三、Map集合分類

1.Hashtable

底層是哈希表數據結構,不可以存入null鍵和null值,該集合是線程同步的。(用作鍵的對象必須實現 hashCode 方法和 equals 方法。)

2.HashMap

底層是哈希表數據結構,允許使用null鍵和null值,該集合是不同步的。(除了非同步和允許使用 null 之外,HashMap 類與 Hashtable 大致相同。)

3.TreeMap

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

 

*****Map和Set很像,因爲Set集合底層就是使用了Map集合*****

Map集合共性方法

一、代碼
PS:上一節中提到的Map集合衆多共性方法中,只對 keySet( )和  entrySet() 方法做代碼解釋
1.keySet() ——返回該Map集合的鍵值Set集合
import java.util.*;
class MapDemo 
{
	public static void main(String[] args) 
	{
		Map<String,String> map=new HashMap<String,String>();
		map.put("1","zhangsan");
		map.put("2","lisi");
		map.put("3","wangzu");
		
		Set<String> keySet=map.keySet();

		Iterator<String> it=keySet.iterator();
		while(it.hasNext())
		{
			System.out.println(map.get(it.next()));
		}
	}
}
2.entrySet()
import java.util.*;
class MapDemo2
{
	public static void main(String[] args) 
	{
		Map<String,String> map=new HashMap<String,String>();
		map.put("1","zhangsan");
		map.put("2","lisi");
		map.put("3","wangzu");

		Set<Map.Entry<String,String>> entrySet=map.entrySet();

		Iterator<Map.Entry<String,String>> it=entrySet.iterator();
		while(it.hasNext())
		{
			Map.Entry<String,String> entry=it.next();
			System.out.println(entry.getKey()+"..."+entry.getValue());
		}
	}
}

Map練習(HashMap和TreeMap)

一、練習
每一個學生都有對應的歸屬地。
學生student,地址String
學生屬性:姓名,年齡。
注意:姓名和年齡相同的視爲同一個學生。
保證學生的唯一性。

1.描述學生。
2.定義map容器,將學生作爲鍵,地址作爲值,存入。
3.獲取map集合中的元素。
二、代碼
import java.util.*;

class Student implements Comparable<Student>
{
	private String name;
	private int age;


	public int compareTo(Student s)
	{
		if(s.age==this.age)
			return this.name.compareTo(s.name);
		return new Integer(this.age).compareTo(new Integer(s.age));
		
	}
	Student(String name,int age)
	{
		this.name=name;
		this.age=age;
	}

	public int hashCode()
	{
		return name.hashCode()+age*34;
	}

	public boolean equals(Object obj)
	{
		if(!(obj instanceof Student))
			throw new ClassCastException("類型不匹配");

		Student s=(Student)obj;
		return s.name.equals(this.name)&&s.age==this.age;

	}
	
	public String getName()
	{
		return name;
	}
	public int getAge()
	{
		return age;
	}
	public String toString()
	{
		return name+":"+age;
	}
}


class TreeMapPractiseDemo
{
	public static void main(String[] args) 
	{
		TreeMap<Student,String> hm=new TreeMap<Student,String>();
		hm.put(new Student("zhangsan",12),"山東");
		hm.put(new Student("lisi",14),"天津");
		hm.put(new Student("wangwu",15),"上海");
		hm.put(new Student("zhaoliu",11),"遼寧");
		hm.put(new Student("zhaoliu",11),"北京");//將原來相同Student對象取代
		Set<Student> set=hm.keySet();
		Iterator<Student> it=set.iterator();
		while(it.hasNext())
		{
			Student s=it.next();
			System.out.println(s.getName()+":"+s.getAge()+"___"+hm.get(s));
		}

		Set<Map.Entry<Student,String>> set1=hm.entrySet();
		Iterator<Map.Entry<Student,String>> it1=set1.iterator();
		while(it1.hasNext())
		{
			Map.Entry<Student,String> me=it1.next();
			System.out.println(me.getKey().getName()+":"+me.getKey().getAge()+"————"+me.getValue());
		}

	}
}

TreeMap練習——字母出現的次數

一、練習
”isdjifsakdjsakdj"獲取該字符串中的字母的出現次數。
希望打印結果:a(1)c(2)
二、分析
通過結果發現,每一個字母都有對應的次數,說明字母和次數之間都有映射關係。
三、思路
1.將字符串轉換成字符數組,因爲要對每一個字母進行操作。
2.定義一個map集合,因爲打印結果的字母有順序,所以使用Treemap集合。
3.遍歷字符數組。
將每一個字母作爲鍵去查map集合。
如果返回null,將該字母和1存入到map集合中。
如果返回不是null,說明該字母在map集合已經存在並有對應次數,那麼就獲取這個次數並自增1,讓後將該字母和自增1後的數據繼續存入TreeMap
4.將map集合中的數據變成指定的字符串形式返回。
四、代碼
import java.util.*;
class  TreeMapNumberDemo
{
	public static void main(String[] args) 
	{
		String str="isdjifsakdjsakdj";
		char[] c=str.toCharArray();
		TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();
		for(int i=0;i<c.length;i++)
		{
			if(tm.get(c[i])==null)
			{
				tm.put(c[i],1);
			}
			else
			{
				int count=tm.get(c[i]);
				tm.put(c[i],++count);
			}
		}
		StringBuilder sb=new StringBuilder();
		Set<Map.Entry<Character,Integer>> set=tm.entrySet();
		Iterator<Map.Entry<Character,Integer>> it=set.iterator();
		while(it.hasNext())
		{
			Map.Entry<Character,Integer> me=it.next();
			sb.append(me.getKey()+"("+me.getValue()+")");		
		}

		System.out.println(sb.toString());


	}
}

Map擴展

一、練習
一個學校有四個班級——1班,2班,3班,4班
每個班級都有30名學生,他們有各自的年齡和學號
用Map集合來描述他們。
二、代碼
import java.util.*;

class SchoolMapDemo 
{
	public static void main(String[] args) 
	{
		TreeMap<String,TreeMap<Integer,String>> school=new TreeMap<String,TreeMap<Integer,String>>();
		TreeMap<Integer,String> class1=new TreeMap<Integer,String>();
		TreeMap<Integer,String> class2=new TreeMap<Integer,String>();
		TreeMap<Integer,String> class3=new TreeMap<Integer,String>();
		TreeMap<Integer,String> class4=new TreeMap<Integer,String>();
		school.put("1班",class1);
		school.put("2班",class2);
		school.put("3班",class3);
		school.put("4班",class4);
		putStudent(class1);
		putStudent(class2);
		putStudent(class3);
		putStudent(class4);
		
		//getStudentsOfClass("1班",school);
		getStudentsOfSchool(school);
		
	
	}

	public static void getStudentsOfSchool(TreeMap<String,TreeMap<Integer,String>> school)
	{
		Set<Map.Entry<String,TreeMap<Integer,String>>> set=school.entrySet();
		Iterator<Map.Entry<String,TreeMap<Integer,String>>> it=set.iterator();
		while(it.hasNext())
		{
			Map.Entry<String,TreeMap<Integer,String>> me=it.next();
			getStudentsOfClass(me.getKey(),school);
		}
	
	}


	public static void putStudent(TreeMap<Integer,String> class1)
	{
		class1.put(01,"zhangsan");
		class1.put(02,"lisi");
		class1.put(03,"wangwu");
		class1.put(04,"zhaoliu");	
	}



	public static void getStudentsOfClass(String name,TreeMap<String,TreeMap<Integer,String>> school)
	{
		if(school.get(name)==null)
		{
			System.out.println("您輸入的教室不存在");
		}
		else
		{
			TreeMap<Integer,String> tm=school.get(name);
			Set<Map.Entry<Integer,String>> set=tm.entrySet();
			Iterator<Map.Entry<Integer,String>> it=set.iterator();
			while(it.hasNext())
			{
				Map.Entry<Integer,String> ke=it.next();
				System.out.println(name+"——"+ke.getKey()+":"+ke.getValue());
			}
		}
	}
}





 

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