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 ;//同一个对象放两次,直接返回true if(!(obj instanceof Studenti)) throw new ClassCastException("类型错误"); Studenti p = (Studenti)obj; return this .name.equals(p.name) && this.age == p.age; } }
黑马程序员——java——Map集合的子类HashMap的简单操作方法
原文地址:http://blog.csdn.net/zl18603543572/article/details/46561591