Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Java
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:
Java
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 + "]";
    }
}
Posted
Updated 5-Dec-17 3:17am

1 solution

Quote:
DarknianIdeal -> [Sophia Jeffery]
DarknianIdeal -> [Clyde Calderon]
The above rows are not duplicates, you know.
If you need to discriminate using only the first string of each row then use a HashMap having the userNameGenerator as key and the corresponding Person object as value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900