为什么将自定义 Comparator 用于 treeSet 会破坏 String 对象的相等性?
Why using custom Comparator to treeSet is breaking the equality of String objects?
我写了一个比较器,它计算给定字符串中的辅音以比较 2 个字符串,但它以某种方式使 TreeSet 认为 2 个字符串相等(可能是当它们大小相同时)。
public class SortDiff {
public static void main(String[] s1) {
List<String> list = Arrays.asList("century", "army", "spit", "twin", "frog", "guideline", "impulse",
"distributor", "hallway", "appeal", "fitness", "pudding", "mild", "enter", "kitchen", "constitutional",
"die", "condition", "morsel", "jealous", "colorful", "myth", "belly", "rugby", "valid", "shot",
"locate", "preference", "representative", "chart", "prince", "think", "threshold", "health", "sweater",
"volume", "poison", "lease", "toast", "diplomat", "trait", "cower", "slime", "handy", "example",
"sanctuary", "board", "crash", "large", "attract", "censorship", "room", "license", "smoke", "roll",
"taste", "inflate", "continuation", "establish", "fault", "gown", "dirty", "width", "qualify",
"reference", "excitement", "vegetable", "wear", "confusion", "concept", "willpower", "snarl", "develop",
"integration", "pie", "respectable", "fast", "limit", "shaft", "acceptance", "insert", "brown", "death",
"effective", "ticket", "play", "highway", "lot", "tablet", "mother", "pier", "facility", "match",
"animal", "sport", "laundry", "negligence", "white", "vat", "nuclear");
System.out.println(list.size() + "=====");
TreeSet<String> tree = new TreeSet<String>(new sortByConsonants());//
int count = 0;
for (String s : list) {
count++;
System.out.println(s);
System.out.println(tree.add(s));
}
System.out.println("===>" + count);
System.out.println(tree.size() + "=====");
Iterator itr = tree.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
这是我传递给 TreeSet 的比较器 class。
class sortByConsonants implements Comparator<String>{
public int compare(String a, String b)
{
return count(a,2)-count(b,2);
}
public boolean equals(String a,String b) {
return a.equals(b);
}
public int count(String line,int type) {
int vowels = 0, consonants = 0, digits = 0, spaces = 0;
line = line.toLowerCase();
for(int i = 0; i < line.length(); ++i)
{
char ch = line.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u') {
++vowels;
}
else if((ch >= 'a'&& ch <= 'z')) {
++consonants;
}
else if( ch >= '0' && ch <= '9')
{
++digits;
}
else if (ch ==' ')
{
++spaces;
}
}
if(type==1) {
return vowels;
}else {
return consonants;
}
}
}
谁能调查一下,看看我犯了什么错误!!
目标:我想根据辅音的数量对字符串进行排序(不使用库 sort() 方法)
编辑:将比较器逻辑更改为
public int compare(String a, String b)
{
if(count(a,2)-count(b,2)==0) {
return 1;
}
return count(a,2)-count(b,2);
}
还是不行!!
编辑=对于这个问题TreeSet不是正确的数据结构,应该使用ArrayList。
将您的 SortByConsonants
更改为以下内容。 (注意 class 名称应以大写字母开头)
class SortByConsonants implements Comparator<String> {
public int compare(String a, String b) {
if (count(a) - count(b) == 0) {
return 1;
}
return count(a) - count(b);
}
public boolean equals(String a, String b) {
return a.equals(b);
}
public int count(String line) {
int consonants = 0;
line = line.toLowerCase();
for (int i = 0; i < line.length(); ++i) {
char ch = line.charAt(i);
if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u') {
consonants++;
}
}
return consonants;
}
}
此外,由于我们只是想根据 count 个辅音进行排序,因此无需计算元音、空格、数字等的个数。此外,还有也不需要将另一个参数 type 传递给方法 count
您编写代码时的计算器将 return 0
(意思是相等)
如果 2 个字符串具有 相同数量的辅音 .
您定义了:
TreeSet<String> tree = new TreeSet<String>(new sortByConsonants());
所以在tree
中不能存在2个辅音数量相同的项目(字符串)
我写了一个比较器,它计算给定字符串中的辅音以比较 2 个字符串,但它以某种方式使 TreeSet 认为 2 个字符串相等(可能是当它们大小相同时)。
public class SortDiff {
public static void main(String[] s1) {
List<String> list = Arrays.asList("century", "army", "spit", "twin", "frog", "guideline", "impulse",
"distributor", "hallway", "appeal", "fitness", "pudding", "mild", "enter", "kitchen", "constitutional",
"die", "condition", "morsel", "jealous", "colorful", "myth", "belly", "rugby", "valid", "shot",
"locate", "preference", "representative", "chart", "prince", "think", "threshold", "health", "sweater",
"volume", "poison", "lease", "toast", "diplomat", "trait", "cower", "slime", "handy", "example",
"sanctuary", "board", "crash", "large", "attract", "censorship", "room", "license", "smoke", "roll",
"taste", "inflate", "continuation", "establish", "fault", "gown", "dirty", "width", "qualify",
"reference", "excitement", "vegetable", "wear", "confusion", "concept", "willpower", "snarl", "develop",
"integration", "pie", "respectable", "fast", "limit", "shaft", "acceptance", "insert", "brown", "death",
"effective", "ticket", "play", "highway", "lot", "tablet", "mother", "pier", "facility", "match",
"animal", "sport", "laundry", "negligence", "white", "vat", "nuclear");
System.out.println(list.size() + "=====");
TreeSet<String> tree = new TreeSet<String>(new sortByConsonants());//
int count = 0;
for (String s : list) {
count++;
System.out.println(s);
System.out.println(tree.add(s));
}
System.out.println("===>" + count);
System.out.println(tree.size() + "=====");
Iterator itr = tree.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
这是我传递给 TreeSet 的比较器 class。
class sortByConsonants implements Comparator<String>{
public int compare(String a, String b)
{
return count(a,2)-count(b,2);
}
public boolean equals(String a,String b) {
return a.equals(b);
}
public int count(String line,int type) {
int vowels = 0, consonants = 0, digits = 0, spaces = 0;
line = line.toLowerCase();
for(int i = 0; i < line.length(); ++i)
{
char ch = line.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u') {
++vowels;
}
else if((ch >= 'a'&& ch <= 'z')) {
++consonants;
}
else if( ch >= '0' && ch <= '9')
{
++digits;
}
else if (ch ==' ')
{
++spaces;
}
}
if(type==1) {
return vowels;
}else {
return consonants;
}
}
}
谁能调查一下,看看我犯了什么错误!!
目标:我想根据辅音的数量对字符串进行排序(不使用库 sort() 方法)
编辑:将比较器逻辑更改为
public int compare(String a, String b)
{
if(count(a,2)-count(b,2)==0) {
return 1;
}
return count(a,2)-count(b,2);
}
还是不行!!
编辑=对于这个问题TreeSet不是正确的数据结构,应该使用ArrayList。
将您的 SortByConsonants
更改为以下内容。 (注意 class 名称应以大写字母开头)
class SortByConsonants implements Comparator<String> {
public int compare(String a, String b) {
if (count(a) - count(b) == 0) {
return 1;
}
return count(a) - count(b);
}
public boolean equals(String a, String b) {
return a.equals(b);
}
public int count(String line) {
int consonants = 0;
line = line.toLowerCase();
for (int i = 0; i < line.length(); ++i) {
char ch = line.charAt(i);
if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u') {
consonants++;
}
}
return consonants;
}
}
此外,由于我们只是想根据 count 个辅音进行排序,因此无需计算元音、空格、数字等的个数。此外,还有也不需要将另一个参数 type 传递给方法 count
您编写代码时的计算器将 return 0
(意思是相等)
如果 2 个字符串具有 相同数量的辅音 .
您定义了:
TreeSet<String> tree = new TreeSet<String>(new sortByConsonants());
所以在tree
中不能存在2个辅音数量相同的项目(字符串)