import java.util.*; public class MapTest { public static void main(String[] args) { Map map = new HashMap(); map.put(null,null); System.out.println(map); Map table = new Hashtable(); table.put("null",123); System.out.println(table); Map map1 = new LinkedHashMap(); map1.put(34,"AA"); map1.put("Te",123); map1.put("TOM",new Object()); System.out.println(map1); HashMap map2 = new HashMap(); map2.put("AA",123); map2.put(67,123); map2.put("A",123); map2.put("qA",new Person("aa",12)); map2.put(new Person("aaw",12),"qA"); System.out.println(map2); System.out.println(map2.size()); Object value = map2.remove("AA"); System.out.println(value); System.out.println(map2); Object oldValue = map2.put("A",99); System.out.println(oldValue); System.out.println(map2); Object value1 = map2.get("qA"); System.out.println(value1); System.out.println(map2.get(new Person("aaw",12))); Set keyset = map2.keySet(); Iterator iterator = keyset.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } for(Object o : keyset) { System.out.println(o); } //方式1 Collection collection = map2.values(); for(Object obj : collection) { System.out.println(obj); } //方式2 for(Object key : keyset) { System.out.println(map2.get(key)); } Set set = map2.entrySet(); Iterator iterator1 = set.iterator(); while(iterator1.hasNext()) { Map.Entry entry = (Map.Entry)iterator1.next(); System.out.println(entry.getKey()); System.out.println(entry.getValue()); } } } class Person { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Person person)) return false; return getAge() == person.getAge() && Objects.equals(getName(), person.getName()); } @Override public int hashCode() { return Objects.hash(getName(), getAge()); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
import java.util.*;
public class MapTest
{
public static void main(String[] args)
{
Map map = new HashMap();
map.put(null,null);
System.out.println(map);
Map table = new Hashtable();
table.put("null",123);
System.out.println(table);
Map map1 = new LinkedHashMap();
map1.put(34,"AA");
map1.put("Te",123);
map1.put("TOM",new Object());
System.out.println(map1);
HashMap map2 = new HashMap();
map2.put("AA",123);
map2.put(67,123);
map2.put("A",123);
map2.put("qA",new Person("aa",12));
map2.put(new Person("aaw",12),"qA");
System.out.println(map2);
System.out.println(map2.size());
Object value = map2.remove("AA");
System.out.println(value);
System.out.println(map2);
Object oldValue = map2.put("A",99);
System.out.println(oldValue);
System.out.println(map2);
Object value1 = map2.get("qA");
System.out.println(value1);
System.out.println(map2.get(new Person("aaw",12)));
Set keyset = map2.keySet();
Iterator iterator = keyset.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
for(Object o : keyset)
{
System.out.println(o);
}
//方式1
Collection collection = map2.values();
for(Object obj : collection)
{
System.out.println(obj);
}
//方式2
for(Object key : keyset)
{
System.out.println(map2.get(key));
}
Set set = map2.entrySet();
Iterator iterator1 = set.iterator();
while(iterator1.hasNext())
{
Map.Entry entry = (Map.Entry)iterator1.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}class Person
{
private String name;
private int age;public Person() {
}public Person(String name, int age) {
this.name = name;
this.age = age;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public int getAge() {
return age;
}public void setAge(int age) {
this.age = age;
}@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person person)) return false;
return getAge() == person.getAge() && Objects.equals(getName(), person.getName());
}@Override
public int hashCode() {
return Objects.hash(getName(), getAge());
}@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
JAVA的HashMap改造面向对象编程-----JAVA
于 2023-05-14 12:14:11 首次发布