Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting object from an ArrayList<T> of a specific type in java

I've an ArrayList, for example b, and i want to get an ArrayList of Worker (Worker extend Person) from that ArrayList. b also contains other object that extends from Person.

How can i achive that? Thanks.

like image 424
Gabriel Avatar asked Apr 11 '26 21:04

Gabriel


1 Answers

If you use Guava, it is as easy as:

ArrayList<Person> b;
ArrayList<Worker> a = Lists.newArrayList(Iterables.filter(b, Worker.class));
like image 174
Mark Peters Avatar answered Apr 13 '26 11:04

Mark Peters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!