删除文本文件中的ArrayList重复项 - Java

时间:2017-12-05 16:17:21

标签: java

我想删除ArrayList中的userNames重复项。我试图将ArrayList转换为HashSet,但由于某种原因它不起作用。我选择将ArrayList转换为HashSet的原因是因为它不允许重复的值,但是,当我在我的代码上使用它时,它只更改列表中的顺序:

我的代码输出:

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

输出应该如何:

Choreography - Imran Sullivan
Goodfella - Khalil Person
DarknianIdeal - Sophia Jeffery
Frolicwas - Taylor Vargas
Reliable - Aryan Hess
Deservedness - Eadie Jefferson

这是代码。我已将数据拆分为数组,因此我可以单独打印数据。

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 personName = personData[0];
                String userNameGenerator = personData[1];
                String[] userNameSplit = userNameGenerator.split(",");                
                String newUserNameSplit = userNameSplit[0];                
                Person personObj = new Person(personName, 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);           
        }           
    } 
}

/* Person Class */

public class Person {
    private String personName;
    private String userNameGenerator;

    public Person(String personName, String userNameGenerator) {
        this.personName = personName;
        this.userNameGenerator = userNameGenerator;
    }

    public String getPersonName() {
        return personName;
    }

    public String getUserNameGenerator() {
        return userNameGenerator;
    }

    @Override
    public String toString() {        
        return userNameGenerator + " - " + personName;
    }
}

1 个答案:

答案 0 :(得分:4)

您需要覆盖Person对象上的equals and hashCode方法,以便Set知道哪些对象被认为是相同的。

您似乎希望任何具有相同userNameGenerator字段的两个人被视为相等。在这种情况下,以下内容将满足您的需求:

public class Person {
    private String commonName;
    private String userNameGenerator;

    ...

    @Override
    public int hashCode() 
    {
        return userNameGenerator.hashCode();
    }

    @Override
    public boolean equals(Object o) 
    {
        if (this == o) return true;

        if (this.getClass() != o.getClass()) return false;

        final Person that = (Person) o;

        return this.userNameGenerator.equals(that.userNameGenerator);
    } 
}

需要注意的一些重要事项:

  • 如果是重复项,则Set仅允许在第一个中 所以插入顺序变得很重要

  • 集合不应包含可变元素,因为可变性会破坏 他们的内在一致性你的Person类是不可变的(没有 setters)这是好的,但你甚至可能想强制执行这种不变性 进一步宣布所有字段final