Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较java中的两个数组列表_Java_Arrays_Loops - Fatal编程技术网

比较java中的两个数组列表

比较java中的两个数组列表,java,arrays,loops,Java,Arrays,Loops,我有两个不同的数组oldUsers和newUsers,其中包含类User的实例。用户包含firstname、lastname和age属性。我想知道newUsers数组中具有相同属性的oldUsers对象的数量。我应该使用两个for循环并逐个比较数组,还是有一个函数可以做相同的工作?假设您的用户对象正确实现等于()和hashCode(),我将使用列表中的一个“保留(集合其他)方法来创建两个列表的交集,然后返回其大小。假设您的用户对象正确地实现了equals()和hashCode(),我将使用其中一

我有两个不同的数组oldUsers和newUsers,其中包含类User的实例。用户包含firstname、lastname和age属性。我想知道newUsers数组中具有相同属性的oldUsers对象的数量。我应该使用两个for循环并逐个比较数组,还是有一个函数可以做相同的工作?

假设您的
用户
对象正确实现
等于()
hashCode()
,我将使用列表中的一个“
保留(集合其他)
方法来创建两个列表的交集,然后返回其大小。

假设您的
用户
对象正确地实现了
equals()
hashCode()
,我将使用其中一个列表的
retainal(集合其他)
方法来创建两个列表的交集,然后返回其大小。

如果正确实现了
equals()
hashCode()
,则可以将
oldUsers
数组中的所有输入放置到
Set
中,然后检查来自
新用户的数据,如果他们在
Set
中。这将适用于
O(max(n,m))
(将数据放入
Set
is
O(n)
,检查
newUsers
,如果他们在Set is
O(m)
,那么你有
O(n)+O(m)=O(max(n,m))
,其中
n
旧用户的大小
列表,
m
新用户的大小

例如:

    private int numberOfSameUsers(ArrayList<User> oldUsers, ArrayList<User> newUsers) {
        Set<User> oldUsersSet = new HashSet<>(oldUsers);
        int counter = 0;
        for (int i = 0; i < newUsers.size(); i++) if (oldUsersSet.contains(newUsers.get(i))) counter++;
        return counter;
    }
private int numberOfSameUsers(ArrayList oldUsers,ArrayList newUsers){
Set oldUsersSet=新哈希集(oldUsers);
int计数器=0;
对于(int i=0;i
如果正确实现了
equals()
hashCode()
,则可以将
oldUsers
数组中的所有输入放置到
Set
中,然后检查
newUsers
中的数据是否在该
集合中。这将适用于
O(max(n,m))
(将数据放入
Set
is
O(n)
,检查
newUsers
,如果他们在Set is
O(m)
,那么你有
O(n)+O(m)=O(max(n,m))
,其中
n
旧用户的大小
列表,
m
新用户的大小

例如:

    private int numberOfSameUsers(ArrayList<User> oldUsers, ArrayList<User> newUsers) {
        Set<User> oldUsersSet = new HashSet<>(oldUsers);
        int counter = 0;
        for (int i = 0; i < newUsers.size(); i++) if (oldUsersSet.contains(newUsers.get(i))) counter++;
        return counter;
    }
private int numberOfSameUsers(ArrayList oldUsers,ArrayList newUsers){
Set oldUsersSet=新哈希集(oldUsers);
int计数器=0;
对于(int i=0;i
您首先需要覆盖
equals()
hashCode()
。然后可以实现一个
intersection()
方法

Number of identical values: 2
------------------------------
- { 'firstname': 'Bob', 'lastname': 'Smith', 'age': 30 }
- { 'firstname': 'Robert', 'lastname': 'Brown', 'age': 51 }
主要
替代方法 集合::保留所有
公共静态列表交叉点(列表1、列表2){
Set Set=新哈希集(列表1);
restainall(新HashSet(list2));
返回新的ArrayList(集合);
}
列表::Java8过滤器流
公共静态列表交叉点(集合列表1、集合列表2){
返回list1.stream().filter(item->list2.contains(item)).collect(Collectors.toList());
}

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

Number of identical values: 2
------------------------------
- { 'firstname': 'Bob', 'lastname': 'Smith', 'age': 30 }
- { 'firstname': 'Robert', 'lastname': 'Brown', 'age': 51 }
主要
替代方法 集合::保留所有
公共静态列表交叉点(列表1、列表2){
Set Set=新哈希集(列表1);
restainall(新HashSet(list2));
返回新的ArrayList(集合);
}
列表::Java8过滤器流
公共静态列表交叉点(集合列表1、集合列表2){
返回list1.stream().filter(item->list2.contains(item)).collect(Collectors.toList());
}

首先,重写
User
类中的
equals()
hashCode()
方法。是的,可以使用两个for循环。我不确定一个已经存在的函数。如果使用两个for循环“足够好”,请使用它。首先,重写
User
类中的
equals()
hashCode()
方法。是的,可以使用两个for循环。我不确定一个已经存在的函数。如果使用两个for循环“足够好”,请使用它。谢谢,你能告诉我最后的int prime=31?我不明白。@user3235881它是EclipseIDE提供的主乘数的默认值。关于这背后的原因,请查看:@user3235881在我上面的示例中,除非您更改了某些内容,否则在控制台中始终会得到2。平等者没有错。它使用的是
String.equals()
,按字典顺序比较名称。谢谢,您能告诉我最后的int prime=31?我不明白。@user3235881它是EclipseIDE提供的主乘数的默认值。关于这背后的原因,请查看:@user3235881在我上面的示例中,除非您更改了某些内容,否则在控制台中始终会得到2。平等者没有错。它使用的是
String.equals()
,它按字典顺序比较名称。
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);
}
public static <T> List<T> intersection(Collection<T> list1, Collection<T> list2) {
    return list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList()); 
}