简体   繁体   English

从另一个对象列表中提取对象列表

[英]Extract List of object from another List of objects

I have a List<User> users , where class User has one property username . 我有一个List<User> users ,其中class User有一个属性username I also have another List<User> activeUsers . 我还有另一个List<User> activeUsers Let users = activeUsers + inactiveUsers . users = activeUsers + inactiveUsers Now I want to extract inactiveUsers from users based on username property. 现在我想根据username属性从users提取inactiveUsers I solved this problem using two for-loops. 我使用两个for循环解决了这个问题。 I think this is not efficient way. 我认为这不是有效的方法。 so if anyone know how it can be done efficiently please let me know. 所以如果有人知道如何有效地完成,请告诉我。

eg I have activeUsers[1,3] and users[1,2,3,4] and want to build inactiveUsers[2,4]. 例如,我有activeUsers [1,3]和用户[1,2,3,4],并希望构建inactiveUsers [2,4]。

In Java, you can use the Collection interface's removeAll method. 在Java中,您可以使用Collection接口的removeAll方法。

// Create a couple ArrayList objects and populate them
// with some delicious fruits.
Collection firstList = new ArrayList() {{
    add("user1");
    add("user2");
}};

Collection secondList = new ArrayList() {{
    add("user1");
    add("user1");
    add("user3");
    add("user4");
}};

// Show the "before" lists
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);

// Remove all elements in firstList from secondList
secondList.removeAll(firstList);

// Show the "after" list
System.out.println("Result: " + secondList);

The above code will produce the following output: 上面的代码将产生以下输出:

First List: [user1, user2]
Second List: [user1, user2, user3, user4]
Result: [user3, user4]

Well, based on the restriction of modifying the User class and nature of your question there is no way of doing it lesser than o(n^2) with two lists (ie two loops). 好吧,基于修改User类的限制和问题的性质,没有办法比使用两个列表(即两个循环)小于o(n^2) )。 Of course if you could have two lists of each type your problem is done. 当然,如果你可以有两个每种类型的列表,你的问题就完成了。

But since you cannot than the logic is : (assuming your structures are lists only) 但是因为你不能逻辑:(假设你的结构只是列表)

Iterate users list (o(n)) :
   - Search the active users list for the current user (o(n)) 

No matter how you look at it you get o(n^2) 无论你如何看待它,你都得到o(n^2)

If you could modify User to have activation property you can easily reduce the problem to o(n) by a single search 如果您可以修改User以具有激活属性,则可以通过单次搜索轻松将问题减少为o(n)

You could store users in a Set based on if they're active or not, I would prefer a property on User but I guess you don't want that in this assignment of yours. 您可以将用户存储在一个Set中,如果他们是否处于活动状态,我会更喜欢User上的一个属性,但我想你不希望在你的这个分配中。 Using this code your problem is solved in O(n) time. 使用此代码,您的问题将在O(n)时间内得到解决。 Assuming you haven't messed up equals/hashCode in User. 假设你没有弄乱用户中的equals / hashCode。

package com.stackoverflow.q15999468;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Users
{
    public static void main(String[] args)
    {
        List<User> users = Arrays.asList(u1,u2,u3,u4,u5);
        List<User> activeUsers = Arrays.asList(u3,u5);

        Set<User> activeUsersSet = new HashSet<User>(activeUsers);

        List<User> inactiveUsers = new ArrayList<User>();

        for(User user : users)
        {
            if(!activeUsersSet.contains(user))
            {
                inactiveUsers.add(user);
            }
        }
        System.out.println("Inactive users: " + inactiveUsers);
    }
}

This should work 这应该工作

List<User> inactiveUsers = new ArrayList<User>(users);
inactiveUsers.removeAll(activeUsers) ;

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

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