ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HashMap存储自定义类型键值

2022-07-08 10:03:22  阅读:79  来源: 互联网

标签:map HashMap 自定义 age 键值 perpon new public name


HashMap存储自定义类型键值

Map集合保证key是唯一的:作为key的元素 必须重写hashCode方法和equals方法 以保证key唯一

代码:

自定义的类:

package demo18.Student;

public class perpon {
private String name;
private int age;

public perpon(String name, int age) {
this.name = name;
this.age = age;
}

public perpon() {
}

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 String toString() {
return "perpon{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

测试类(使用String作为key 而自己定义的类作为value):

private static void method() {
//创建HashMap集合
HashMap<String, perpon> map = new HashMap<>();
//向HashMap集合中添加数据
map.put("上海",new perpon("张三",18));
map.put("广州",new perpon("李四",18));
map.put("东北",new perpon("王五",18));
map.put("北京",new perpon("赵六",18));
//使用Map集合中的方法entrySet() 把Map集合中多个Entry对象取出来 存储到一个Set集合中
Set<Map.Entry<String, perpon>> set = map.entrySet();
for (Map.Entry<String, perpon> entry : set) {
String key = entry.getKey();
perpon value = entry.getValue();
System.out.println(key+"------->"+value);
}

运行结果:

 

 测试类(使用自己定义的类作为key String作为value)

private static void method01() {
//创建HashMap集合 让perpon作为key值
HashMap<perpon, String> map = new HashMap<>();
map.put(new perpon("张三",18),"北京");
map.put(new perpon("李四",18),"北京");
map.put(new perpon("王五",18),"北京");
map.put(new perpon("赵六",18),"北京");
map.put(new perpon("张三",18),"北京");
//使用Map集合中的方法entrySet() 把Map集合中多个Entry对象取出来 存储到一个Set集合中
Set<Map.Entry<perpon, String>> set = map.entrySet();
//使用增强for遍历数组
for (Map.Entry<perpon, String> entry : set) {
String value = entry.getValue();
perpon key = entry.getKey();
System.out.println(key+"------>"+value);
}


}

运行结果:

 

 可以看到运行结果中是用两个张三 而且年龄也一样 因为我们没有重写equals方法和hashCode方法

添加equals方法和hashCode方法的perpon类:

public class perpon {
private String name;
private int age;

public perpon(String name, int age) {
this.name = name;
this.age = age;
}

public perpon() {
}

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 String toString() {
return "perpon{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
perpon perpon = (perpon) o;
return age == perpon.age &&
Objects.equals(name, perpon.name);
}

@Override
public int hashCode() {
return Objects.hash(name, age);
}
}

测试类:

    private static void method01() {
//创建HashMap集合 让perpon作为key值
HashMap<perpon, String> map = new HashMap<>();
map.put(new perpon("张三",18),"北京");
map.put(new perpon("李四",18),"北京");
map.put(new perpon("王五",18),"北京");
map.put(new perpon("赵六",18),"北京");
map.put(new perpon("张三",18),"北京");
//使用Map集合中的方法entrySet() 把Map集合中多个Entry对象取出来 存储到一个Set集合中
Set<Map.Entry<perpon, String>> set = map.entrySet();
//使用增强for遍历数组
for (Map.Entry<perpon, String> entry : set) {
String value = entry.getValue();
perpon key = entry.getKey();
System.out.println(key+"------>"+value);
}
}

}

运行结果:

 

 没有同名同年龄的人

标签:map,HashMap,自定义,age,键值,perpon,new,public,name
来源: https://www.cnblogs.com/aimz01/p/16457179.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有