java-Map練習

來源:互聯網
上載者:User

標籤:

/*每一個學生都有對應的歸屬地。學生Student,地址String。學生屬性:姓名,年齡。注意:姓名和年齡相同的視為同一個學生。保證學生的唯一性。1,描述學生。2,定義map容器。將學生作為鍵,地址作為值。存入。3,擷取map集合中的元素。*/import java.util.*;class Student implements Comparable<Student>{private String name;private int age;Student(String name,int age){this.name = name;this.age = age;}public int compareTo(Student s){int num = new Integer(this.age).compareTo(new Integer(s.age));if(num==0)return this.name.compareTo(s.name);return num;}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 String getName(){return name;}public int getAge(){return age;}public String toString(){return name+":"+age;}}class  MapTest{public static void main(String[] args) {HashMap<Student,String> hm = new HashMap<Student,String>();hm.put(new Student("lisi1",21),"beijing");hm.put(new Student("lisi1",21),"tianjin");hm.put(new Student("lisi2",22),"shanghai");hm.put(new Student("lisi3",23),"nanjing");hm.put(new Student("lisi4",24),"wuhan");//第一種取出方式 keySetSet<Student> keySet = hm.keySet();Iterator<Student> it = keySet.iterator();while(it.hasNext()){Student stu = it.next();String addr = hm.get(stu);System.out.println(stu+".."+addr);}//第二種取出方式 entrySetSet<Map.Entry<Student,String>> entrySet = hm.entrySet();Iterator<Map.Entry<Student,String>> iter = entrySet.iterator();while(iter.hasNext()){Map.Entry<Student,String> me = iter.next();Student stu = me.getKey();String addr = me.getValue();System.out.println(stu+"........."+addr);}}}

  

/*需求:對學生對象的年齡進行升序排序。因為資料是以索引值對形式存在的。所以要使用可以排序的Map集合。TreeMap。*/import java.util.*;class StuNameComparator 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;}}class  MapTest2{public static void main(String[] args) {TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());tm.put(new Student("blisi3",23),"nanjing");tm.put(new Student("lisi1",21),"beijing");tm.put(new Student("alisi4",24),"wuhan");tm.put(new Student("lisi1",21),"tianjin");tm.put(new Student("lisi2",22),"shanghai");Set<Map.Entry<Student,String>> entrySet = tm.entrySet();Iterator<Map.Entry<Student,String>> it = entrySet.iterator();while(it.hasNext()){Map.Entry<Student,String> me = it.next();Student stu = me.getKey();String addr = me.getValue();System.out.println(stu+":::"+addr);}}}

  

/*練習:"sdfgzxcvasdfxcvdf"擷取該字串中的字母出現的次數。希望列印結果:a(1)c(2).....通過結果發現,每一個字母都有對應的次數。說明字母和次數之間都有映射關係。注意了,當發現有映射關係時,可以選擇map集合。因為map集合中存放就是映射關係。什麼使用map集合呢?當資料之間存在這映射關係時,就要先想map集合。思路:1,將字串轉換成字元數組。因為要對每一個字母進行操作。2,定義一個map集合,因為列印結果的字母有順序,所以使用treemap集合。3,遍曆字元數組。將每一個字母作為鍵去查map集合。如果返回null,將該字母和1存入到map集合中。如果返回不是null,說明該字母在map集合已經存在並有對應次數。那麼就擷取該次數並進行自增。,然後將該字母和自增後的次數存入到map集合中。覆蓋調用原理鍵所對應的值。4,將map集合中的資料變成指定的字串形式返回。*/import java.util.*;class  MapTest3{public static void main(String[] args) {String s= charCount("ak+abAf1c,dCkaAbc-defa");System.out.println(s);}public static String charCount(String str){char[] chs = str.toCharArray();TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();int count = 0;for(int x=0; x<chs.length; x++){if(!(chs[x]>=‘a‘ && chs[x]<=‘z‘ || chs[x]>=‘A‘ && chs[x]<=‘Z‘))continue;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 = value + 1;tm.put(chs[x],value);}*/}//System.out.println(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();}}

  

java-Map練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.