Select some objects from a list of objects based on list of one field
final ArrayList<MyNamedObject> namedObjectList = ...;
final List<String> nameList = ...;
Use
namedObjectList.stream()
.filter((final MyNamedObject o) -> nameList.stream().anyMatch((final String name) -> Objects.equals(name, o.getName())))
.collect(Collectors.toList());
If there are some duplicate values in the name list, either keep the list but use .distinct()
:
namedObjectList.stream()
.filter((final MyNamedObject o) -> nameList.stream().distinct().anyMatch((final String name) -> Objects.equals(name, o.getName())))
.collect(Collectors.toList());
or create a Set
:
final Set<String> nameSet = nameList.stream()
.distinct()
.collect(Collectors.toSet());
namedObjectList.stream()
.filter((final MyNamedObject o) -> nameSet.stream().anyMatch((final String name) -> Objects.equals(name, o.getName())))
.collect(Collectors.toList());
If nameList
or nameSet
can contain null
, use .filter(Objects::nonNull)
. If neither name
nor o.getName()
can be null
, replace Objects.equals(name, o.getName())
by name.equals(o.getName())
.
select some objects from object list through for loop based on a condition
You can use filter in computed property:
Vue.component('Child', {
template: `
<div>
<div v-for="(guest, g) in myGuests" :key="g">
{{ guest.name }} - {{ guest.age }}
</div>
</div>
`,
props: {
guests: {
type: Array,
default: null,
},
},
computed: {
myGuests() {
return this.guests.filter(g => g.age > 18)
}
}
})
new Vue({
el: '#demo',
data() {
return {
guests: [{'name': 'aa', 'age': 15}, {'name': 'bb', 'age': 21}, {'name': 'cc', 'age': 18}, {'name': 'dd', 'age': 19}, {'name': 'ee', 'age': 20}, {'name': 'ff', 'age': 24}]
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<child :guests="guests"> </child>
</div>
how to select an object from a list of objects by its attribute in python
Try
dave = next(person for person in a.pList if person.num == 123)
or
for person in a.pList:
if person.num == 123:
break
else:
print "Not found."
dave = person
How to find a single item in a list of objects?
FileCabinet fileCabinet = fileCabinets.SingleOrDefault(x => x.(propertyName) == (valueToMatch));
Like @Rainman said, if there is a probability that the name is not unique, use his solution. If you are sure that the Name property is unique, use this one.
Select a property from an array of objects based on a value : Javascript
You can use reduce
and check if the checked
property is true, then push
(As pointed out by assoron) the value to the accumulator - there is no need for 2 loops:
const arr = [ { "value": "abc", "checked": true }, { "value": "xyz", "checked": false }, { "value": "lmn", "checked": true }]
const filtered = arr.reduce((a, o) => (o.checked && a.push(o.value), a), []) console.log(filtered)
Selecting objects with list of other objects using LINQ
Your requirements essentially necessitate creating a new Team
object with a reduced set of players (or alternatively permanently removing the younger players from the original Team
object).
var teamsEnglandOnlyOver28Years =
teams
// Only teams from England
.Where(t => t.Country == "England")
// Create a new instance with only the older players
.Select(t => new Team()
{
TeamName = t.TeamName,
StadiumName = t.StadiumName,
Players = t.Players.Where(p => p.Age > 28).ToList(),
Country = t.Country
});
This may or may not be what you actually intend. It seems a bit awkward because, for example, the team name is the same as for the original team but the roster is reduced.
Related Topics
How to Get the Path of Src/Test/Resources Directory in Junit
Java Program to Find Palindrome Numbers in Given Range
How to Save a File Downloaded With Httpclient into a Specific Folder
Spring Boot @Autowired Environment Throws Nullpointerexception
Java Properties - How to Create Dynamic Properties
Springboot - How to Start Embedded Container
Saveastextfile() to Write the Final Rdd as Single Text File - Apache Spark
Map Json String Column of a JPA Entity to Java Object Automatically
How to Use Different Jsonproperty on Serialize & Deserialize Using Jackson API
How to Loop Through List of Webelements and Select One Webelement With a Condition
Unit Testing Private Functions in Junit With Mockito
How to Retrieve Element Value from Soap Response Using Java
Gson Expected Begin_Array But Was String At Line 1 Column 62