This question already has answers here:
How to calculate the intersection of two sets? [duplicate]
(2 answers)
Closed 2 years ago.
There are two collections :-
c1 = which include all female employee
c2= all employee whose age greater than 40(age>40).
How can i find all female employee whose age greater than 40?
c1.stream().filter(c2::contains).collect(Collectors.toList());//Last step is optional. Only use it, if you want a list of it.
stream(): Create a stream out of the first collection. (Java 8+)
Then filter() by those that are in c2,too.
And then optionally make a list from it.
Related
This question already has answers here:
Getting max value from an arraylist of objects?
(5 answers)
Java Stream: find an element with a min/max value of an attribute
(9 answers)
Closed 3 years ago.
I failed to solve this task. 'I have a List<Car> where a car is an object with carNumber and kmPassed props. Return a Car with max kmPassed value.'
I know how to find min/max in array but how to find it in Car object?
This question already has answers here:
Java List.contains(Object with field value equal to x)
(13 answers)
Is there a java equivalent to NSPredicate?
(1 answer)
Closed 5 years ago.
In the IOS, we need to search an item or name easily find using the NSPredicate on NSArray or collection. This NSPredicate is executing very faster and give the results exactly few seconds
In the Android or Java any NSPredicate type search available? Can you please suggest.
I have LiSt object, It contains "n" number of objects. So, I want search an item name or value is exist in this object or not. If the item is exist to get those items. In IOS it is easy to get the Items using the NSPredicate.
So using java is it possible to fetch the item in the List object. When we use the default for loop it takes lot of time to execute and find the elements.
This question already has answers here:
How to get the type of a value (Java)
(3 answers)
Closed 6 years ago.
So I have an abstract class named "User" and two derivated classes named "Seller" and "Buyer". I also have a vector of users in which I push sellers and buyers... My question is: if I get a random element from vector, how can I know if the element it's seller or buyer?
Thanks
use isInstanceOf(Object); on the random object with an if() condition to check if it' a a buyer or a seller
This question already has answers here:
Better way to find index of item in ArrayList?
(8 answers)
Closed 7 years ago.
In Java is there a method to find index of a particular string in ArrayList?
For Example if cities is an array list of 100 cities and I want to find the index of "New York".
You can use the indexOf(...) method for search in ArrayList. If the String exists within the ArrayList, it returns the index of the String, otherwise it returns -1.
This link might be helpful.
This question already has answers here:
Sorting Java objects using multiple keys
(7 answers)
Closed 9 years ago.
I have an arraylist of movies that holds the movie title and director. I need to create a comparator that will first sort the directors in alphabetical order, and will sor the movies made by each director in alphabetical order. I also want to sort this data in ascending OR descending order.
e.g.
James Cameron:
Steven Spielberg
after you've sorted the director list....
just take one director as an key value in outer loop and then start comparing each and every movie name in inner loop...
How can I sort a List alphabetically?
this would be helpful...
do some searching next time....it is easy to find such solutions... :) ;)