HashSet - 确保较早的对象持久性 [英] HashSet - ensuring the earlier object persistence

查看:169
本文介绍了HashSet - 确保较早的对象持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用 HashSet ,其中可能插入大量重复值。但是我想保留插入在哈希中的早期数据,当以后的插入做出重复。为了检查这我写了以下代码并插入许多重复的值,但它不满足我。请参阅下面的代码 -

I have to use a HashSet where a lot of duplicate value may be inserted. But I want to preserve the earlier data inserted in the hash when a later insertion makes the duplicate. To examine this I have write the following code and insert many duplicate value, but it doesn't satisfy me. Please see the code below -

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

public class SetTest {

    private static Set<Student> studentSet = new HashSet<Student>();
    private static Student s1, s2, s3, s4, s5, s6, s7, s8, s9;

    public static void main(String args[]){

        s1 = new Student(1, 1, "Syeful", "first boy");
        s2 = new Student(2, 2, "Razib", "no comments");
        s3 = new Student(3, 3, "Bulbul", "should remain");
        s4 = new Student(4, 3, "Bulbul", "should not remain");
        s5 = new Student(5, 4, "Bulbul", "should remain");
        s9 = new Student(9, 5, "Proshanto", "kaka - my favourite");

        studentSet.add(s1);
        studentSet.add(s2);
        studentSet.add(s3);
        studentSet.add(s4);
        studentSet.add(s5);
        studentSet.add(s9);

        for(Student each : studentSet){
            System.out.println("SrNo: " +each.getSrNo()+ " roleNo: " 
                    +each.getRoleNo()+ " name: " +each.getName()+ 
                    " coment: " +each.getComment());
        }
    }

}

class Student{

    private int srNo;
    private int roleNo;
    private String name;
    private String comment;

    public Student(int srNo, int role, String name, String comment) {
        super();
        this.srNo = srNo;
        this.roleNo = role;
        this.name = name;
        this.comment = comment;
    }

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

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Student)) {
            return false;
        }
        Student other = (Student) obj;
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        if (roleNo != other.roleNo) {
            return false;
        }
        return true;
    }

    public int getSrNo() {
        return srNo;
    }

    public int getRoleNo() {
        return roleNo;
    }

    public String getName() {
        return name;
    }

    public String getComment() {
        return comment;
    }
}

输出为:

Set Size: 5
SrNo: 9 roleNo: 5 name: Proshanto coment: kaka - my favourite
SrNo: 2 roleNo: 2 name: Razib coment: no comments
SrNo: 1 roleNo: 1 name: Syeful coment: first boy
SrNo: 5 roleNo: 4 name: Bulbul coment: should remain
SrNo: 3 roleNo: 3 name: Bulbul coment: should remain

似乎我在提出问题之前澄清一些观点

It seems me to clarify some points before asking the questions so that I can explain it properly and clarify my understanding as well.


  • 我想保持学生的唯一性 roleNo name 。这就是为什么hashCode()和equals()角色由这些属性组成。因此,根据这个实现,s3和s4彼此重复,即使认为它们的注释 srNo 彼此。

  • I want to maintain the uniqueness of 'Student' based on roleNo and name.That's why the hashCode() and equals() role is made up with these property. So according to this implementation s3 and s4 are duplicate of each other even thought the comment , srNo property of them differs from each other.

HashSet 是无序的。

当s4被添加到集合中时,我们可以从输出看到s4从集合中舍弃,s3仍然保留。

When s4 is added in the set, we can see from the out put that s4 is discarded from the set and s3 remains.

假设我要添加学生的另一个对象 s100 = new Student(3,3,Bulbul,更早的实例抑制); 这是s3的副本。让我们在插入s100之前插入了很多s3的副本。

Suppose I want to add another object of student s100 = new Student(3, 3, "Bulbul", "earlier instance suppressed"); which is the duplicate of s3. Let we have inserted a lot of duplicate of s3 before inserting s100.



由于set是未排序的,且重复剂量不存在于set中,s3是否可能被 s100 删除?我想坚持更早的对象丢弃后者。少量的数据不能澄清事实。我认为在这种情况下,可以使用排序 Set 。但是 HashSet 能否达到目的?

Question:
Since set is unsorted and the duplicate dose not exist in set, is there any possibility that the s3 is removed by s100? I want to persist the earlier object discarding the later one. The small amount of data is not clarifying the fact. I think in this case a sorted Set may used. But does the HashSet can serve the purpose?

提前感谢。

推荐答案

HashSet.add(E e) 如果已经包含指定的元素,则保持集合不变。因此,没有任何情况下 s3 将被 s100 删除。

这篇关于HashSet - 确保较早的对象持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆