hashset中的重复元素(在正确实现比较器/比较方法之后)

时间:2015-08-21 13:39:21

标签: java compare comparator duplicate-removal treeset

我正在编写一个程序,它从文件中读取行并创建令牌。然后将令牌保存在树集中。我不希望集合中有任何重复,所以我用正确的比较方法实现了Comparator。但是我仍然无法弄清楚为什么在树集中重复了一个元素。

输入文件:(13行)

VEG,SMALL,Corn,MOZZARELLA 
VEG,LARGE,Capsicum,CHEDDAR 
VEG,MEDIUM,Olives,COTTAGE 
VEG,MEDIUM,Garlic,MOZZARELLA 
VEG,LARGE,Peppers,CHEDDAR     
NONVEG,MEDIUM,Beef,CHEDDAR  
NONVEG,LARGE,Chicken,COTTAGE  
NONVEG,SMALL,Pork,MOZZARELLA  
NONVEG,LARGE,Mutton,CHEDDAR    
NONVEG,SMALL,Mutton,MOZZARELLA 
VEG,SMALL,Corn,MOZZARELLA
NONVEG,MEDIUM,Beef,CHEDDAR 
NONVEG,SMALL,Mutton,MOZZARELLA  

我的代码:

public Set<Pizza> populateData(String FileName) {

    Set<Pizza> set = new TreeSet(new Comparator<Pizza>(){

        @Override
        public int compare(Pizza o1, Pizza o2) {

            if(o1.getPizzType().compareTo(o2.getPizzType())==0)
            {
                if(o1.getCheeseType().compareTo(o2.getCheeseType())==0)
                {
                    if(o1.getSize().compareTo(o2.getSize())==0)
                    {
                        if(o1.getTopping().compareTo(o2.getTopping())==0)
                        {
                            return 0;
                        }
                    }
                }
            }
            return 54;
        }


    });


    File file = new File(FileName);
    Scanner scanner=null;

    try {
        scanner=new Scanner(new FileInputStream(file));
        Pizza pizza=null;

        while(scanner.hasNext())
        {
            String line=scanner.nextLine();
            String words[]=new String[4];
            words=line.split(",");

            pizza=new Pizza();

            if(words[0].trim().length()>0)
            {
                pt=PizzaType.valueOf(words[0].trim());
                pizza.setPizzType(PizzaType.valueOf(words[0].trim()));
            }
            if(words[1].trim().length()>0)
            {
                sz=(Size.valueOf(words[1].trim()));
                pizza.setSize(Size.valueOf(words[1].trim()));
            }
            if(words[2].trim().length()>0)
            {
                tp=(words[2].trim());
                pizza.setTopping(words[2].trim());
            }
            if(words[3].trim().length()>0)
            {
                ct=(CheeseType.valueOf(words[3].trim()));
                pizza.setCheeseType(CheeseType.valueOf(words[3].trim()));
            }


            set.add(pizza);

        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return set;
}

(与字符串长度和哈希码一起打印的输出)

输出:(11行){第一行和最后一行是重复:P}

VEG SMALL Corn MOZZARELLA 22  589431969
VEG LARGE Capsicum CHEDDAR 23  1252169911
VEG MEDIUM Olives COTTAGE 22  2101973421
VEG MEDIUM Garlic MOZZARELLA 25  685325104
VEG LARGE Peppers CHEDDAR 22  460141958
NONVEG MEDIUM Beef CHEDDAR 23  1163157884
NONVEG LARGE Chicken COTTAGE 25  1956725890
NONVEG SMALL Pork MOZZARELLA 25  356573597
NONVEG LARGE Mutton CHEDDAR 24  1735600054
NONVEG SMALL Mutton MOZZARELLA 27  21685669
VEG SMALL Corn MOZZARELLA 22  2133927002

预期产出:(10行)

VEG SMALL Corn MOZZARELLA  
VEG LARGE Capsicum CHEDDAR
VEG MEDIUM Olives COTTAGE 
VEG MEDIUM Garlic MOZZARELLA
VEG LARGE Peppers CHEDDAR 
NONVEG MEDIUM Beef CHEDDAR 
NONVEG LARGE Chicken COTTAGE
NONVEG SMALL Pork MOZZARELLA
NONVEG LARGE Mutton CHEDDAR 
NONVEG SMALL Mutton MOZZARELLA

1 个答案:

答案 0 :(得分:2)

Comparator contract要求在o1 < o2时返回否定,如果o1 > o2则返回肯定,如果o1 == o2则返回0。

这意味着您的代码需要看起来像

public int compare(Pizza o1, Pizza o2) {

    int compvalue = o1.getPizzType().compareTo(o2.getPizzType());
    if(compvalue == 0)
    {
        compvalue = o1.getCheeseType().compareTo(o2.getCheeseType());
        ...etc..
        if(compvalue == 0)
        ...
    }
    return compvalue;
}