java集合:在HashSet中添加自定义类的对象

定义宠物猫类:

package java_set;

public class Cat {
    private String name;
    private int month;
    private String species;
    
    //构造方法
    public Cat(String name, int month, String species) {
        this.name = name;
        this.month = month;
        this.species = species;
    }

    //getter、setter方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public String getSpecies() {
        return species;
    }

    public void setSpecies(String species) {
        this.species = species;
    }
}

测试类:
定义宠物猫对象,将宠物猫对象放入HashSet中

package java_set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class CatTest {
    public static void main(String[] args) {
        //定义宠物猫对象
        Cat huahua = new Cat("花花", 5, "英国短毛猫");
        Cat fanfan = new Cat("凡凡", 10, "中华田园猫");
        //将宠物猫对象放入HashSet中
        Set set = new HashSet();
        set.add(huahua);
        set.add(fanfan);
    }
}

显示宠物猫信息

//显示宠物猫信息
Iterator it = set.iterator();
while(it.hasNext()){
    System.out.println(it.next());  
}

输出:

java_set.Cat@2f92e0f4
java_set.Cat@28a418fc

可看到输出的结果是宠物猫对象的地址信息。
若想显示宠物猫信息,需要在Cat类中重写toString方法:

@Override
public String toString() {
    return "[姓名:" + name + ", 年龄:" + month + ", 品种:" + species + "]";
}   

此时再次显示宠物猫信息,会自动调用重写的toString方法:

[姓名:花花, 年龄:5, 品种:英国短毛猫]
[姓名:凡凡, 年龄:10, 品种:中华田园猫]

接着在测试类中添加与huahua属性一样的猫

//添加与huahua属性一样的猫
Cat huahua1 = new Cat("花花", 5, "英国短毛猫");
set.add(huahua1);
System.out.println("**************************************");
System.out.println("添加重复数据后的宠物猫信息:");
it = set.iterator();
while(it.hasNext()){
    System.out.println(it.next()); 
}

输出:

**************************************
添加重复数据后的宠物猫信息:
[姓名:花花, 年龄:5, 品种:英国短毛猫]
[姓名:凡凡, 年龄:10, 品种:中华田园猫]
[姓名:花花, 年龄:5, 品种:英国短毛猫]

why? 该结果与之前所说的(集合中不允许有重复元素)好像矛盾了

其实是需要在Cat类中再重写hashCode和equals方法的:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + month;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + ((species == null) ? 0 : species.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    //判断对象是否相等,相等则返回true,不用继续比较属性了
    if(this == obj)
        return true;
    //判断obj是否是Cat类的对象
    if(obj.getClass() == Cat.class){  //判断obj这个对象对应的类是不是Cat
        Cat cat = (Cat)obj;   //强制类型转换
        return (cat.getName().equals(name)) && (cat.getMonth() == month) && (cat.getSpecies().equals(species));
    }
    return false;
}    

输出,此时就不会再出现重复的信息了:

**************************************
添加重复数据后的宠物猫信息:
[姓名:凡凡, 年龄:10, 品种:中华田园猫]
[姓名:花花, 年龄:5, 品种:英国短毛猫]
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值