我希望输出按字母顺序列出userNameGenerator。我将HashSet转换为ArrayList,因为无法对集合进行排序,因此我将其更改为ArrayList以按字母顺序对列表进行排序。我使用Collections.sort按字母顺序对字符串列表进行排序,但输出仍然是 同样的。
我用于字母顺序的代码:
List sortedList = new ArrayList(personFile);
Collections.sort(sortedList);
System.out.println();
输出:
Dompteuse -> Imran Sullivan,
Deservedness -> Eadie Jefferson,
Ostracize -> Eadie Jefferson,
Abattoir -> Angel Whitehouse,
Choreography -> Imran Sullivan,Taylor Vargas,Priya Oliver,
Goodfella -> Khalil Person,Eadie Jefferson,
Frolicwas -> Taylor Vargas,
DarknianIdeal -> Sophia Jeffery,Clyde Calderon,Taylor Vargas,Liyah Navarro,
Cremaster -> Aryan Hess,
Reliable -> Imran Sullivan,Aryan Hess,Angel Whitehouse,Priya Oliver,
Hootenany -> Clyde Calderon,
Acrimony -> Taylor Vargas,
完整代码:
import java.util.*;
import java.io.*;
public class Codes {
public static void main(String[] args) {
List<Codes2> personFile = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader("person-data.txt"));
String fileRead = br.readLine();
while (fileRead != null) {
String[] personData = fileRead.split(":");
String personName = personData[0];
String userNameGenerator = personData[1];
Codes2 personObj = new Codes2(personName, userNameGenerator);
personFile.add(personObj);
fileRead = br.readLine();
}
br.close();
}
catch (FileNotFoundException ex) {
System.out.println("File not found!");
}
catch (IOException ex) {
System.out.println("An error has occured: " + ex.getMessage());
}
Set<String> newStrSet = new HashSet<>();
for(int i = 0; i < personFile.size(); i++){
String[] regionSplit = personFile.get(i).getUserNameGenerator().split(", ");
for(int j = 0; j < regionSplit.length; j++){
newStrSet.add(regionSplit[j]);
}
}
for (String s: newStrSet) {
System.out.printf("%s -> ", s);
for (Codes2 p: personFile) {
if (p.getUserNameGenerator().contains(s)) {
System.out.printf("%s, ", p.getPersonName());
}
}
List sortedList = new ArrayList(personFile);
Collections.sort(sortedList);
System.out.println();
}
}
}
人类:
public class Codes2 implements Comparable<Codes2> {
private String personName;
private String userNameGenerator;
public Codes2(String personName, String userNameGenerator) {
this.personName = personName;
this.userNameGenerator = userNameGenerator;
}
public String getPersonName() {
return personName;
}
public String getUserNameGenerator() {
return userNameGenerator;
}
@Override
public int compareTo(Codes2 o) {
return getUserNameGenerator().compareTo(o.getUserNameGenerator());
}
public int compare(Object lOCR1, Object lOCR2) {
return ((Codes2)lOCR1).userNameGenerator
.compareTo(((Codes2)lOCR2).userNameGenerator);
}
}
参考:Simple way to sort strings in the (case sensitive) alphabetical order
答案 0 :(得分:1)
正如@phatfingers评论的那样,TreeSet将遵循集合中条目的自然顺序。
由于Codes2已经实现了Comparable,你可以简单地用TreeSet替换你的原始HashSet并将它调用一天。
注意由一组维护的排序(无论是否显式 提供比较器)必须与equals一致,如果它是 正确实现Set接口。(请参阅Comparable或Comparator 对于与equals一致的精确定义。)就是这样 因为Set接口是根据equals操作定义的, 但是TreeSet实例使用它执行所有元素比较 compareTo(或compare)方法,因此两个被认为相等的元素 从该集合的角度来看,通过这种方法是相等的。该 集合的行为即使其排序不一致也是明确定义的 与...平等它只是没有遵守集合的一般合同 接口。 (重点补充)
这意味着,虽然简单地使用TreeSet可能单独工作,但您可能仍需要考虑在Codes2类中重写equals()和hashCode(),以便排序行为与equals一致。