![](/img/trans.png)
[英]Sort contents of file in alphabetical order into a new file using methods (Java)
[英]Sort data in a text file in alphabetical order - Java
我希望輸出按字母順序列出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);
}
}
正如@phatfingers所說,TreeSet將尊重集合中條目的自然順序。
由於Codes2已經實現了Comparable ,因此您可以簡單地用TreeSet替換原始的HashSet並將其命名為day。
但是,請從TreeSet文檔中注意這一點:
請注意, 如果要 正確實現Set接口,則由集合(無論是否提供顯式比較器)維護的順序必須等於equals 。 (有關與equals一致的精確定義,請參見Comparable或Comparator。)之所以這樣,是因為Set接口是根據equals操作定義的,但是TreeSet實例使用其compareTo(或compare)方法執行所有元素比較,因此兩個從集合的角度來看,此方法認為相等的元素是相等的。 即使集合的順序與equals不一致,它的行為也是明確定義的; 它只是不遵守Set接口的一般約定。 (添加了重點)
這意味着,盡管僅使用TreeSet可能本身即可工作,但您可能仍要考慮在Codes2類中重寫equals()和hashCode(),以便排序行為與equals保持一致。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.