package com.xiaodong;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class Text1
{
public static void main(String[] args)
{
run(); //打印Map集合
run1(); //将对象作为值打印集合,以及重写equals方法判断是否包含对象的值
run2(); //不重写equals方法判断是否包含这个对象的值的方法
run3(); //判断这个集合是否为空
run4(); //遍历map集合
}
public static void run()
{
//创建一个以字符串作为键,Peek对象作为值的集合
Map<String,Peek> map = new HashMap<String,Peek>();
map.put("key1", new Peek(1,"张三",21));
map.put("key2", new Peek(2,"李四",23));
map.put("key3", new Peek(3,"王五",22));
map.put("key4", new Peek(4,"刘六",25));
System.out.println(map);
}
public static void run1()
{
//创建一个以字符串作为键,Peek对象作为值的集合
Map<String,Peek> map = new HashMap<String,Peek>();
map.put("key1", new Peek(1,"张三",21));
map.put("key2", new Peek(2,"李四",23));
map.put("key3", new Peek(3,"王五",22));
map.put("key4", new Peek(4,"刘六",25));
//是否包含指定的键
boolean is = map.containsKey("key1");
System.out.println(is);
//是否包含指定的值
boolean is1 = map.containsValue(new Peek(2,"李四",23));
System.out.println(is1);
}
public static void run2()
{
//不重写equals方法的判断方法
Peek p = new Peek(1,"张三",21);
Map<String,Peek> map = new HashMap<String,Peek>();
map.put("key1", new Peek(1,"张三",21));
map.put("key2", new Peek(2,"李四",23));
map.put("key3", new Peek(3,"王五",22));
map.put("key4", new Peek(4,"刘六",25));
boolean is = map.containsValue(p);
System.out.println(is);
}
public static void run3()
{
Map<String,Peek> map = new HashMap<String,Peek>();
map.put("key1", new Peek(1,"张三",21));
map.put("key2", new Peek(2,"李四",23));
map.put("key3", new Peek(3,"王五",22));
map.put("key4", new Peek(4,"刘六",25));
map.clear();
//判断这个集合是否为空
boolean is = map.isEmpty();
System.out.println(is);
}
public static void run4()
{
Map<String,Peek> map = new HashMap<String,Peek>();
map.put("key1", new Peek(1,"张三",21));
map.put("key2", new Peek(2,"李四",23));
map.put("key3", new Peek(3,"王五",22));
map.put("key4", new Peek(4,"刘六",25));
Set<Entry<String,Peek>> ti = map.entrySet();
for(Entry<String,Peek> s : ti)
{
//获得键
String t = s.getKey();
System.out.print("键:"+t);
//获得值
Peek t1 = s.getValue();
System.out.println("值:"+t1);
}
}
}
class Peek
{
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public Peek(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Peek [age=" + age + ", id=" + id + ", name=" + name + "]";
}
//判断包含指定的值需要重写equals方法
public boolean equals(Object obj)
{
if(!(obj instanceof Peek))
{
return false;
}
Peek pe = (Peek)obj;
if(this.id == pe.id&&this.age == pe.age&&this.name == pe.name)
{
return true;
}
return false;
}
//重写hashCode方法
public int hashCode()
{
return 1;
}
}
本文通过具体示例展示了如何使用 Java 中的 HashMap 进行增删改查操作,包括添加元素、检查键与值的存在状态、遍历集合等。此外,还讨论了如何重写 equals 方法以实现更精确的对象匹配。
2426

被折叠的 条评论
为什么被折叠?



