0
Follow
0
View

How to check the weight of multiple attributes of a list entity class

adi6000 注册会员
2023-02-28 19:29


can be iterated through the list, composing the name and sex attributes of each entity class into a string, and then using a HashSet to see if it is duplicated. If there is a duplicate, the entity class is added to a new list.

import java.util.*;

public class Person {
    private int id;
    private String name;
    private int age;
    private String sex;

    public Person(int id, String name, int age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public static void main(String[] args) {
        List persons = new ArrayList<>();
        persons.add(new Person(1, "张三", 18, "男"));
        persons.add(new Person(2, "张三", 18, "男"));
        persons.add(new Person(3, "李四", 20, "男"));
        persons.add(new Person(4, "李四", 18, "男"));

        Set<String> uniquePerson = new HashSet<>();
        List duplicatePerson = new ArrayList<>();

        for (Person person : persons) {
            String personInfo = person.getName() + person.getSex();
            if (!uniquePerson.add(personInfo)) {
                duplicatePerson.add(person);
            }
        }

        System.out.println("重复的人员信息:");
        for (Person person : duplicatePerson) {
            System.out.println(person.getName() + " " + person.getSex());
        }
    }
}


About the Author

Question Info

Publish Time
2023-02-28 19:29
Update Time
2023-02-28 19:29

Related Question

在ScrollView中包装时,Top Tab Navigator高度问题

如何在颤振的新页面上创建showModelBottomSheet

单独打开Tomcat可以访问,但是运行代码时就不行了

如何将参数传递给#selector函数

在R中安装PDFTools的问题

pytorch hub 网站运行缓慢?

QuerySnapshot只返回添加/更新/删除的文档还是kotlin中的Firestore中的所有文档?

sizeof和strlen的区别

如何在Magento2.3.4中添加新页面

使用IndexOf结果引用一个单元格[副本]