Java 8 Streams: multiple filters vs. complex condition

Sometimes you want to filter a Stream with more than one condition: myList.stream().filter(x -> x.size() > 10).filter(x -> x.isCool()) … or you could do the same with a complex condition and a single filter: myList.stream().filter(x -> x.size() > 10 && x -> x.isCool()) … My guess is that the second approach has better performance characteristics, … Read more

Filter object properties by key in ES6

Let’s say I have an object: { item1: { key: ‘sdfd’, value:’sdfd’ }, item2: { key: ‘sdfd’, value:’sdfd’ }, item3: { key: ‘sdfd’, value:’sdfd’ } } I want to create another object by filtering the object above so I have something like. { item1: { key: ‘sdfd’, value:’sdfd’ }, item3: { key: ‘sdfd’, value:’sdfd’ } … Read more

How to filter empty or NULL names in a QuerySet?

I have first_name, last_name & alias (optional) which I need to search for. So, I need a query to give me all the names that have an alias set. Only if I could do: Name.objects.filter(alias!=””) So, what is the equivalent to the above? 9 s 9 You could do this: Name.objects.exclude(alias__isnull=True) If you need to … Read more

How to filter a Java Collection (based on predicate)?

I want to filter a java.util.Collection based on a predicate. 29 s 29 Java 8 (2014) solves this problem using streams and lambdas in one line of code: List<Person> beerDrinkers = persons.stream() .filter(p -> p.getAge() > 16).collect(Collectors.toList()); Here’s a tutorial. Use Collection#removeIf to modify the collection in place. (Notice: In this case, the predicate will … Read more