TreeSet只添加一个值?

时间:2014-02-28 04:25:46

标签: java collections set treeset

我是Java Collections的新手。我有三个类,即 MyData,Initializer和SetBasics

我创建了一个HashSet和一个TreeSet。现在的问题是HashSet工作正常,但TreeSet只存储一个值。

这是我的课程

Class MyData

`package collectionsWorkUp.setDemos;

public class MyData实现Comparable {

MyData(){   
}

MyData(String n, String p, String eM, long mn, char s){
    name = n;
    profession = p;
    email = eM;
    mobileNo = mn;
    sex =  s;
}


private String name;
private String profession;
private String email;
private long mobileNo;
private char sex;


public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getProfession() {
    return profession;
}
public void setProfession(String profession) {
    this.profession = profession;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public long getMobileNo() {
    return mobileNo;
}
public void setMobileNo(long mobileNo) {
    this.mobileNo = mobileNo;
}
public char getSex() {
    return sex;
}
public void setSex(char sex) {
    this.sex = sex;
}

@Override
public int compareTo(MyData o) {
    // TODO Auto-generated method stub
    return 0;
}

} `

类初始值设定项

package collectionsWorkUp.setDemos;

public class Initializer {

/**
 * @param args
 */
public static void main(String[] args) {

    System.out.println("Initializing");

    SetBasics operate = new SetBasics();

    operate.DataInSet();
    operate.printDataInSet(operate.getUsersInHash());

    System.out.println("\n\nPrinting Data in Tree Set--------------------");
    operate.printDataInSet(operate.getUsersInTree());

    System.out.println("\n\n--------Program Terminated----------");

}

}

Class SetBasics

package collectionsWorkUp.setDemos;

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

public class SetBasics {

    private Set<MyData> usersInHash = new HashSet<MyData>();
    private Set<MyData> usersInTree =  new TreeSet<MyData>();

    MyData d1= new MyData(d1's data -data include name, #, profession etc check MyData Class);
    MyData d2=  new MyData(d2's data);
    MyData d3=  new MyData(d3's data);
    MyData d4= new MyData(d4's data);
    MyData d5= new MyData(d5's data);
    MyData d6= new MyData(d6's data);
    MyData d7= new MyData(d7's data);
    MyData nullUser = null;

    public Set<MyData> getUsersInHash() {
        return usersInHash;
    }

    public Set<MyData> getUsersInTree() {
        return usersInTree;
    }

public SetBasics(){ }

public void DataInSet(){

    {
        System.out.println("Adding Data in HashSet");
        System.out.println("Data in HashSet Before Adding Element:" + usersInHash.size());
        setDataInSet(nullUser);
        System.out.println("Data in HashSet After Adding Element:" + usersInHash.size());
        setDataInSet(d7);
        System.out.println("Data in HashSet After Adding Element:" + usersInHash.size());
        setDataInSet(d6);
        System.out.println("Data in HashSet After Adding Element:" + usersInHash.size());
        setDataInSet(d5);
        System.out.println("Data in HashSet After Adding Element:" + usersInHash.size());
        setDataInSet(d4);
        System.out.println("Data in HashSet After Adding Element:" + usersInHash.size());
        setDataInSet(d3);
        System.out.println("Data in HashSet After Adding Element:" + usersInHash.size());
        setDataInSet(d2);
        System.out.println("Data in HashSet After Adding Element:" + usersInHash.size());
        setDataInSet(d1);
        System.out.println("Data in HashSet After Adding Element:" + usersInHash.size());
        setDataInSet(null);
        System.out.println("Data in HashSet After Adding Element:" + usersInHash.size());
    }

    {
        System.out.println("Adding Data in TreeSet");
        System.out.println("Users in Tree after Before Data:"+usersInTree.size());
        setDataInTreeSet(d7);
        System.out.println("Users in Tree after Adding Data:"+usersInTree.size());
        setDataInTreeSet(d6);
        System.out.println("Users in Tree after Adding Data:"+usersInTree.size());
        setDataInTreeSet(d5);
        System.out.println("Users in Tree after Adding Data:"+usersInTree.size());
        setDataInTreeSet(d4);
        System.out.println("Users in Tree after Adding Data:"+usersInTree.size());
        setDataInTreeSet(d3);
        System.out.println("Users in Tree after Adding Data:"+usersInTree.size());
        setDataInTreeSet(d2);
        System.out.println("Users in Tree after Adding Data:"+usersInTree.size());
        setDataInTreeSet(d1);
        System.out.println("Users in Tree after Adding Data:"+usersInTree.size());

        printDataInSet(usersInTree);
    }

}

public void setDataInSet(MyData userDetails){
    System.out.println("Setting Data in HashSet");
    usersInHash.add(userDetails);       
}

public void setDataInTreeSet(MyData userDetails){
    System.out.println("Setting Data in TreeSet");
    usersInTree.add(userDetails);
}

public void printDataInSet(Set<MyData> s){
    System.out.println("Printing Data");
    System.out.println("Size of the Set:"+s.size());
    for(MyData itorator: s){
        try{
            System.out.println("User Details:______");
            System.out.println("Name:"+itorator.getName());
            System.out.println("Mobile Number:"+itorator.getMobileNo());
            System.out.println("eMail:"+itorator.getEmail());
            System.out.println("Profession:"+itorator.getProfession());
            System.out.println("Sex:"+itorator.getSex());

        }catch(NullPointerException npe){
            System.out.println(npe.getMessage());
            npe.getStackTrace();
        }finally{
            System.out.println("EOD_____\n");
        }

    }

}

}

现在问题出现在TreeSet中我只能看到d7的数据而没有其他东西被添加到TreeSet

2 个答案:

答案 0 :(得分:4)

TreeSet将使用您的MyData.compareTo实施来比较元素。但是你没有实现它 - 它总是返回0。因此,就TreeSet而言,所有MyData个对象都是相同的。有关详细信息,请参阅compareTo javadoc。

请注意,HashSet正在运行,因为它使用hashCodeequals。与compareTo不同,它们在Object类中具有默认实现,它们基于引用相等性。我建议您覆盖这两种方法,并将它们基于MyData字段。实际上,具有相同数据的两个不同MyData对象将不被视为相等。 (大多数IDE可以根据指定的字段自动生成良好的hashCodeequals覆盖。)

答案 1 :(得分:2)

问题出在compareTo方法上。 return 0视为等于MyData,这会在MyData添加一个TreeSet对象。

public int compareTo(MyData o) {
   //Write correct compareTo contract for MyData.
}