I have a list:
我有一个清单:
List<UserItem> userList = new ArrayList<>();
Where I add the following:
我在哪里添加以下内容:
User father = new User();
father.setName("Peter");
UserItem parent = new UserItem(father, null);
userList.add(parent);
I then create another user:
然后我创建另一个用户:
User daughter = new User();
daughter.setName("Emma");
UserItem child = new UserItem(daughter, <OBJECT IN LIST WHERE NAME IS "PETER">);
userList.add(child);
However, I need to change the text wrapped in <>
above to the parent object I added before (the father), specified by the name ("Peter" in this case).
但是,我需要将上面包含的文本更改为我之前添加的父对象(父对象),由名称指定(在本例中为“Peter”)。
How can I find an object in a List by a specific attribute? In my case, how can I find the object in the List that has the name "Peter"?
如何通过特定属性在List中找到对象?在我的例子中,如何在List中找到名为“Peter”的对象?
Please note that I add hundreds, sometimes thousands, of different users like this to the list. Each "parent" has a unique name.
请注意,我在列表中添加了数百个,有时是数千个这样的不同用户。每个“父母”都有一个唯一的名称。
3 个解决方案
#1
4
The obvious solution would be iterating on the list and when the condition is met, return the object:
显而易见的解决方案是在列表上进行迭代,当条件满足时,返回对象:
for (User user : userList) {
if ("peter".equals(user.getName()) {
return user;
}
}
And you can use filter
(Java 8):
你可以使用过滤器(Java 8):
List<User> l = list.stream()
.filter(s -> "peter".equals(s.getUser()))
.collect(Collectors.toList());
to get a list with all "peter" users.
获取所有“彼得”用户的列表。
As suggested in comments, I think using Map
is a better option here.
正如评论中所建议的,我认为在这里使用Map是一个更好的选择。
#2
1
Answer to your question is here: https://*.com/a/1385698/2068880
您的问题的答案如下:https://*.com/a/1385698/2068880
Stream peters = userList.stream().filter(p -> p.user.name.equals("Peter"))
However, as ruakh suggested, it's more reasonable to use Map<String, UserItem>
to make it faster. Otherwise, it will iterate all the objects in the list to find users with name "Peter".
但是,正如Ruakh建议的那样,使用Map
#3
0
Other way with parallelStream with findAny
使用findAny的parallelStream的其他方法
Optional<UserItem> optional = userList.parallelStream().findAny(p -> p.user.getName().equalsIgnoreCase("Peter"));
UserItem user = optional.isPresent() ? optional.get() : null;
#1
4
The obvious solution would be iterating on the list and when the condition is met, return the object:
显而易见的解决方案是在列表上进行迭代,当条件满足时,返回对象:
for (User user : userList) {
if ("peter".equals(user.getName()) {
return user;
}
}
And you can use filter
(Java 8):
你可以使用过滤器(Java 8):
List<User> l = list.stream()
.filter(s -> "peter".equals(s.getUser()))
.collect(Collectors.toList());
to get a list with all "peter" users.
获取所有“彼得”用户的列表。
As suggested in comments, I think using Map
is a better option here.
正如评论中所建议的,我认为在这里使用Map是一个更好的选择。
#2
1
Answer to your question is here: https://*.com/a/1385698/2068880
您的问题的答案如下:https://*.com/a/1385698/2068880
Stream peters = userList.stream().filter(p -> p.user.name.equals("Peter"))
However, as ruakh suggested, it's more reasonable to use Map<String, UserItem>
to make it faster. Otherwise, it will iterate all the objects in the list to find users with name "Peter".
但是,正如Ruakh建议的那样,使用Map
#3
0
Other way with parallelStream with findAny
使用findAny的parallelStream的其他方法
Optional<UserItem> optional = userList.parallelStream().findAny(p -> p.user.getName().equalsIgnoreCase("Peter"));
UserItem user = optional.isPresent() ? optional.get() : null;