简体   繁体   English

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

[英]Remove ArrayList duplicates in a text file - Java

I want to remove the duplicates of userNames in the ArrayList. 我想删除ArrayList中的userNames的重复项。 I've tried to convert the ArrayList to an HashSet, but for some reason it doesn't work. 我试图将ArrayList转换为HashSet,但是由于某种原因它不起作用。 The reason why I chose to convert the ArrayList into a HashSet is because it does not allow duplicated values, however, when I use it on my code, it only changes the order in the list: 之所以选择将ArrayList转换为HashSet,是因为它不允许重复的值,但是,当我在代码中使用它时,它只会更改列表中的顺序:

My code 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

This is the code. 这是代码。 I've splitted the data into an Array so I can print out the data individually. 我已将数据拆分为一个数组,以便可以单独打印出数据。

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;
    }
}

You need to override the equals and hashCode methods on your Person object in order for Set to know which objects are considered the same. 您需要在Person对象上覆盖equalshashCode方法,以便Set知道哪些对象被视为相同。

It seems you want any two people with the same userNameGenerator field to be considered equal. 似乎您希望将具有相同userNameGenerator字段的任何两个人视为相等。 In that case, the following will suit your needs: 在这种情况下,以下将满足您的需求:

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);
    } 
}

Some important things to note: 需要注意的一些重要事项:

  • In the case of duplicates, the Set will only allow in the first one so insertion order becomes important . 如果重复,则Set仅在第一个中允许,因此插入顺序变得很重要

  • Sets should not contain mutable elements because mutability destroys their internal consistency. 集不应该包含可变元素,因为可变性会破坏其内部一致性。 Your Person class is immutable (has no setters) which is good but you may want to enforce this immutability even further by declaring all of it's fields final . 您的Person类是不可变的(没有设置器),这很好,但是您可能想要通过声明其所有字段final进一步加强这种不可变性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM