哈希集中的重复元素(在正确实施比较器/比较方法之后)
duplicate element in hashset ( after implementing comparator /compare method correctly )
我目前正在编写一个从文件中读取行并创建标记的程序。然后将令牌保存在树集中。我不希望集合中有任何重复项,因此我使用正确的比较方法实现了 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
Comparator contract 要求 return 如果 o1 < o2
为负,如果 o1 > o2
为正,如果 o1 == o2
则为零。
这意味着您的代码需要看起来像
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;
}
我目前正在编写一个从文件中读取行并创建标记的程序。然后将令牌保存在树集中。我不希望集合中有任何重复项,因此我使用正确的比较方法实现了 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
Comparator contract 要求 return 如果 o1 < o2
为负,如果 o1 > o2
为正,如果 o1 == o2
则为零。
这意味着您的代码需要看起来像
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;
}