第二题: public class Person { private String name; public Person(){ } public Person(String name){ super(); this.name = name; } public String getName(){ return this.name; } } public class Dog { private String name; public Dog(){ } public Dog(String name){ super(); this.name = name; } public String getName(){ return this.name; } } import java.util.Map; import java.util.HashMap; import java.util.Map.Entry; import java.util.List; import java.util.ArrayList; public class HashDemo { public static void main(String[] args) { Map<Person,Dog> hashMap = new HashMap<Person,Dog>(); List<Person> list = new ArrayList<Person>(); Person person_obj = null; for(int i=0;i<100;i++){ person_obj = new Person("person_name" + i); hashMap.put(person_obj, new Dog("dog_name" + i)); list.add(person_obj); } //Keyset for(Person p : hashMap.keySet()){ System.out.println(p.getName() + "---" + hashMap.get(p).getName()); } //EntrySet for (Entry<Person, Dog> entry : hashMap.entrySet()) { System.out.println(entry.getKey().getName() + "---" + entry.getValue().getName()); } for(Person pl : list){ hashMap.remove(pl); } System.out.println("------------"); System.out.println(hashMap.size()); } }
时间: 2024-10-04 18:43:40