Map集合的子类HashMap的简单操作方法

Map集合的子类HashMap的简单操作方法

import java.util.HashMap;import java.util.Iterator;public class HashMapDemos {public static void%r0main(String[] args) {// HashMap<Studenti,String> hm = new HashMap<Studenti,String>();hm.put(new Studenti("kk",22), "山西");hm.put(new Studenti("jj",20), "山东");hm.put(new Studenti("hh",25), "上海");Iterator<Studenti> it = hm.keySet().iterator();while(it.hasNext()){Studenti key = it.next();String value = hm.get(key);System.out.println(key.toString()+" –"+value);}}}class Studenti{private String name;private int age;Studenti(String name ,int age){this.name=name;this.age = age;}public int getAge(){return age;}public String getName(){return name;}public String toString(){return "the student name :" +name +" and age is : "+ age;}public int hashCode(){return name.hashCode()+age*10;}public boolean equals(Object obj){ if(this == obj)return true ;//同一个对象放两次,,直接返回trueif(!(obj instanceof Studenti))throw new ClassCastException("类型错误");Studenti p = (Studenti)obj;return this .name.equals(p.name) && this.age == p.age;}}运行程序:

巨龟千岁,却也平淡无奇;昙花瞬间,却能绚丽无比。

Map集合的子类HashMap的简单操作方法

相关文章:

你感兴趣的文章:

标签云: