I want to remove the duplicates in an ArrayList. I've tried to convert the ArrayList to an HashSet, but for some reason it doesn't work. It only changes the order in the list:
Output:
Choreography -> [Imran Sullivan]
Goodfella -> [Khalil Person]
DarknianIdeal -> [Sophia Jeffery]
DarknianIdeal -> [Clyde Calderon]
Frolicwas -> [Taylor Vargas]
Reliable -> [Aryan Hess]
DarknianIdeal -> [Liyah Navarro]
Deservedness -> [Eadie Jefferson]
Reliable -> [Angel Whitehouse]
Choreography -> [Priya Oliver]
How the output should be:
Choreography -> [Imran Sullivan]
Goodfella -> [Khalil Person]
DarknianIdeal -> [Sophia Jeffery]
Frolicwas -> [Taylor Vargas]
Reliable -> [Aryan Hess]
Deservedness -> [Eadie Jefferson]
What I have tried:
The codes that are in bold below are the codes that I have tried.
package task1;
import java.util.*;
import java.io.*;
public class Task1 {
public static void main(String[] args) {
List<Person> personFile = new ArrayList<>();
Set<Person> splitUserNameList = new HashSet<>(personFile);
try {
BufferedReader br = new BufferedReader(new FileReader("person-data.txt"));
String fileRead = br.readLine();
while (fileRead != null) {
String[] personData = fileRead.split(":");
String fullName = personData[0];
String userNameGenerator = personData[1];
String[] userNameSplit = userNameGenerator.split(",\\s+");
String newUserNameSplit = userNameSplit[0];
Person personObj = new Person(fullName, newUserNameSplit);
splitUserNameList.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());
}
for (Person userNames: splitUserNameList) {
System.out.println(userNames);
}
}
}
package task1;
public class Person {
private String fullName;
private String userNameGenerator;
public Shark2(String fullName, String userNameGenerator) {
this.commonName = fullName;
this.userNameGenerator = userNameGenerator;
}
public String getFullName() {
return fullName;
}
public String getUserNameGenerator() {
return userNameGenerator;
}
@Override
public String toString() {
return userNameGenerator + " -> " + "[" + fullName + "]";
}
}