Reputation:
I want the output to list the userNameGenerator in alphabetical order. I converted the HashSet into an ArrayList because sets cannot be sorted, so I changed it to an ArrayList to sort out the list in alphabetical order. I used Collections.sort to sort the list of strings in alphabetical order, however, the output is still the same.
Code I used for alphabetical order:
List sortedList = new ArrayList(personFile);
Collections.sort(sortedList);
System.out.println();
Output:
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,
Full Code:
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();
}
}
}
Person Class:
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);
}
}
Reference: Simple way to sort strings in the (case sensitive) alphabetical order
Upvotes: -1
Views: 1846
Reputation: 2104
As @phatfingers commented, a TreeSet will respect the natural order of the entries in the set.
Since Codes2 already implements Comparable, you could simply replace your original HashSet with a TreeSet and call it a day.
Note this from the TreeSet documentation, however:
Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface. (emphasis added)
What this means is that, while simply using a TreeSet probably work by itself, you may still want to consider overriding equals() and hashCode() in your Codes2 class so that the ordering behavior is consistent with equals.
Upvotes: 0