gpt4 book ai didi

java - java中比较两个数组列表

转载 作者:行者123 更新时间:2023-12-01 18:05:17 25 4
gpt4 key购买 nike

我有两个不同的数组 oldUsers 和 newUsers ,其中包含 User 类的实例。用户包含名字、姓氏和年龄属性。我想知道 newUsers 数组中具有相同属性的 oldUsers 对象的数量。我应该使用两个 for 循环并逐一比较数组吗?或者是否有一个函数可以完成相同的工作?

最佳答案

您首先需要重写equals()hashCode()。然后您可以实现一个 intersection() 方法。

Number of identical values: 2
------------------------------
- { 'firstname': 'Bob', 'lastname': 'Smith', 'age': 30 }
- { 'firstname': 'Robert', 'lastname': 'Brown', 'age': 51 }

主要

import java.util.*;

public class Main {
public static void main(String[] args) {
List<User> oldUsers = new ArrayList<User>();
List<User> newUsers = new ArrayList<User>();
List<User> intersect;

oldUsers.addAll(Arrays.asList(
new User("Bob", "Smith", 30),
new User("Tom", "Jones", 42),
new User("Robert", "Brown", 51),
new User("James", "Jones", 28)
));

newUsers.addAll(Arrays.asList(
new User("Robert", "Brown", 51), // Same
new User("Bob", "Smith", 30), // Same
new User("Tom", "Jones", 21),
new User("James", "Hendrix", 28)
));

intersect = intersection(oldUsers, newUsers);

System.out.printf("Number of identical values: %d%n%s%n",
intersect.size(), "------------------------------");
for (User user : intersect) {
System.out.printf("- %s%n", user);
}
}

// http://stackoverflow.com/a/5283123/1762224
public static <T> List<T> intersection(List<T> list1, List<T> list2) {
List<T> list = new ArrayList<T>();
for (T t : list1) {
if (list2.contains(t)) {
list.add(t);
}
}
return list;
}
}

用户

public class User {
private String firstname;
private String lastname;
private int age;

public String getFirstname() { return firstname; }
public void setFirstname(String firstname) { this.firstname = firstname; }

public String getLastname() { return lastname; }
public void setLastname(String lastname) { this.lastname = lastname; }

public int getAge() { return age; }
public void setAge(int age) { this.age = age; }

public User(String firstname, String lastname, int age) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
result = prime * result + ((lastname == null) ? 0 : lastname.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;

User other = (User) obj;

if (age != other.age) return false;

if (firstname == null) {
if (other.firstname != null) return false;
} else if (!firstname.equals(other.firstname)) return false;

if (lastname == null) {
if (other.lastname != null) return false;
} else if (!lastname.equals(other.lastname)) return false;

return true;
}

@Override
public String toString() {
return String.format("{ 'firstname': '%s', 'lastname': '%s', 'age': %d }",
firstname, lastname, age);
}
}
<小时/>

替代方法

设置::保留全部

public static <T> List<T> intersection(List<T> list1, List<T> list2) {
Set<T> set = new HashSet<T>(list1);
set.retainAll(new HashSet<T>(list2));
return new ArrayList<T>(set);
}

列表::Java 8 过滤器流

public static <T> List<T> intersection(Collection<T> list1, Collection<T> list2) {
return list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
}

关于java - java中比较两个数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36893372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com