Find max value in List [duplicate] - java

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?

Related

How to create a generic array in Java [duplicate]

This question already has answers here:
How to create a generic array in Java?
(32 answers)
Closed 1 year ago.
class car<T>{
car<T>[] name;
name=(car<T>[]) new Object[5];
}
I tried to create an array of the class but the declare seem wrong. how should I set length of the array to 5 here?
Use java.lang.reflect.Array#newInstance:
car<T>[] name = (car<T>[]) java.lang.reflect.Array.newInstance(car.class, 5);

collection framework : operation on two collection object [duplicate]

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.

How to remove element from List after getting it by random()? [duplicate]

This question already has answers here:
Java Arraylist remove multiple element by index
(8 answers)
Closed 2 years ago.
I have a List with some elements. My method get random element from List like
ListName.get(num)
How can I remove element from List with that num I've got?
Call List::remove and pass the same number.
myList.remove( num ) ;
listname.remove(elementnumber)

Java streams statement: find the error [duplicate]

This question already has answers here:
Why does this java 8 stream operation evaluate to Object instead of List<Object> or just List?
(2 answers)
Why do we have to cast the List returned by Collectors.toList() to List<Integer> even though the elements of the Stream are already mapped to Integer? [duplicate]
(1 answer)
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 4 years ago.
This works as expected
Collection<String> items = combo.getItems();
items.stream().filter(item -> item.startsWith("New")).findFirst()...
But this fails to compile. Why?
Collection items = combo.getItems();
items.stream().map(Object::toString).filter(item -> item.startsWith("New")).findFirst()...
^^^^^^^^^^

Java - getting type of derivated class [duplicate]

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

Categories