map集合練習

import java.util.*;


/*練習: 每一個學生都有對應的歸屬地。
 * 學生Student,地址String。
 * 學生屬性:姓名,年齡。
 * 注意:姓名和年齡相同的視爲同一個學生。
 * 保證學生的唯一性。
 * Map集合練習:
 * 1.描述學生。
 * 2.定義map容器,將學生作爲鍵,地址作爲值,存入。
 * 3.後去map集合中的元素。
 * 
 */
class Student implements Comparable<Student>
{
private String name;
private int age;
public Student(String name,int age) {
this.name=name;
this.age=age;
}
//元素有可能會存入二叉樹中,所以要對元素進行自然排序。
public int compareTo(Student s)
{
int num=new Integer(this.age).compareTo(s.age);
if(num==0)
{
return this.name.compareTo(s.name);
}
return num;
}
public String getName()
{
return name;
}
public int getAge(){
return age;
}
public String toString()
{
return name+":"+age;
}
//元素可能回存入hash表中,所以要判斷元素的唯一性,添加元素就會調用的兩個方法。
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 (this.name.equals(s.name)) && (this.age==s.age);
}
}
public class MapTest {
public static void main(String[] args) {
HashMap<Student,String> hm=new HashMap<Student,String>();
hm.put(new Student("lis1",21),"beijing");
hm.put(new Student("lis3",23),"shanghai");
hm.put(new Student("lis4",24),"changsha");
hm.put(new Student("lis2",22),"nanjing");
//第一種存取方式。
Set<Student> keySet=hm.keySet();
Iterator<Student> it=keySet.iterator();
while(it.hasNext())
{
Student stu=it.next();
String addr=hm.get(stu);
sop(stu+"---"+addr);
}
//第二種取出方式entrySet
Set<Map.Entry<Student,String>> entrySet =hm.entrySet();
Iterator<Map.Entry<Student,String>> it1=entrySet.iterator();
while(it1.hasNext())
{
Map.Entry<Student,String> me= it1.next();
Student tu=me.getKey();
String addr=me.getValue();
sop(tu+"----"+addr);
}
}
public static <T> void sop(T t){
System.out.println(t);
}

}



import java.util.*;


/*
 * TreeSet練習:
 * 因爲數據時以鍵值對形式存在的。所以要使用可以排序的map集合,TreeMap。
 * 
 */
public class MapTest2 {
public static void main(String[] args) {
//以比較器的比較爲主。
TreeMap<Student,String> tm=new TreeMap<Student,String>(new MyCompare());
tm.put(new Student("blis1",21),"beijing");
tm.put(new Student("lis3",23),"shanghai");
tm.put(new Student("alis4",24),"changsha");
tm.put(new Student("alis4",24),"chang");
tm.put(new Student("slis2",22),"nanjing");

Set<Map.Entry<Student,String>> entrySet =tm.entrySet();
Iterator<Map.Entry<Student,String>> it1=entrySet.iterator();
while(it1.hasNext())
{
Map.Entry<Student,String> me= it1.next();
Student tu=me.getKey();
String addr=me.getValue();
sop(tu+"----"+addr);
}

}
public static <T> void sop(T t){
System.out.println(t);
}
}
//定義姓名比較器
class MyCompare 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;
}
}


import java.util.*;
/*
 * 練習:
 * "sdfgzxcvasdfxcvdf"獲取該字符串中字母出現的次數。
 * 希望打印結果:a(1),c(2)....
 * 因爲字母和次數之間有映射關係,所以使用map集合。
 * 思路:
 * 1.將字符串轉換成數組,因爲要對每一個字母進行操作。
 * 2.定義一個map集合,因爲打印結果的字母有順序,所以使用treemap集合。
 * 3.遍歷字符數組。
 * 將每一個字母作爲鍵去查map集合。
 * 如果返回null,將字母和1存入到map集合中。
 * 如果返回不是null,說明該map集合已經存在並有對應次數。
 * 那麼就獲取該次數並進行自增,然後將字母和自增後的次數存入到map集合。
 * 4.將map集合中的數據編程指定的字符串形式返回。
 */
public class MapTest3 {
public static void main(String[] args) {
String str="sdfgzxcvasdfxcvdf";
sop(charCount(str));
}
public static String charCount(String str)
{
int count=0;
// 1.調用toCharArray將字符串轉換成數組,因爲要對每一個字母進行操作。
char[] chs=str.toCharArray();
TreeMap<Character,Integer> tm=new TreeMap<Character,Integer>();
for(int x=0;x<chs.length;x++)
{

//用get方法判斷是否包含此鍵,包含返回
Integer value=tm.get(chs[x]);
if(value!=null)
{
count=value;
}
count++;
tm.put(chs[x], count);
count=0;

/*if(value==null)
{
tm.put(chs[x],1);
}
else 
{
value+=1;
tm.put(chs[x], value);
}
*/
}
//sop(tm);
StringBuilder sb=new StringBuilder();
Set<Map.Entry<Character,Integer>> entrySet=tm.entrySet();
Iterator<Map.Entry<Character,Integer>> it=entrySet.iterator();
while (it.hasNext())
{
Map.Entry<Character,Integer> me=it.next();
Character ch=me.getKey();
Integer value =me.getValue();
sb.append(ch+"("+value+")");
}
return sb.toString();

}
public static<T> void sop(T t)
{
System.out.println(t);
}

}



/*
 * map擴展知識。
 * map集合被使用是因爲具有映射關係。
 * 集合嵌套集合
 */
import java.util.*;
class Student0{
private String name;
private String num;
Student0(String num,String name)
{
this.name=name;
this.num=num;
}
public String toString()
{
return num+":"+name;
}
}
public class MapDemo4 {
public static void demo()
{
HashMap<String,List<Student0>> czbk=new HashMap<String,List<Student0>>();
List<Student0> yure=new ArrayList<Student0>();
List<Student0> jiuye=new ArrayList<Student0>();
czbk.put("yureban", yure);
czbk.put("jiuyeban",jiuye);
yure.add(new Student0("01","zhangsan"));
yure.add(new Student0("02","lisi"));
jiuye.add(new Student0("01","zhaoliu"));
jiuye.add(new Student0("02","wangwu"));

Iterator<String> it=czbk.keySet().iterator();
while(it.hasNext())
{
String roomname=it.next();
List room=czbk.get(roomname);
getInfo(room);
}
}
public static void getInfo(List<Student0> list)
{
Iterator<Student0> it=list.iterator();
while(it.hasNext())
{
Student0 s=it.next();
System.out.println(s.toString());
}
}
public static void main(String[] args) {


demo();





/*HashMap<String,HashMap<String,String>> czbk=new HashMap<String,HashMap<String,String>>();
HashMap<String,String> yure=new HashMap<String,String>();
HashMap<String,String> jiuye=new HashMap<String,String>();

czbk.put("yureban", yure);
czbk.put("jiuyeban",jiuye);
yure.put("01","zhangsan");
yure.put("02","lisi");
jiuye.put("01","zhaoliu");
jiuye.put("02","wangwu");

//遍歷czbk集合,獲取所有的教室。
Iterator<String> it=czbk.keySet().iterator();
while(it.hasNext())
{
String roomname=it.next();
HashMap<String,String> room=czbk.get(roomname);
getStudentInfo(room);
}
*/
}
public static void getStudentInfo(HashMap<String,String> room)
{
Iterator<String> it=room.keySet().iterator();
while(it.hasNext())
{
String id=it.next();
String name=room.get(id);
System.out.println(id+":"+name);
}
}
}

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