Java 8 streams - how to filter a list and get a value

Bick

Lets say I have Animal object and successors. One of them is Dog and only he implements

public interface Barkable{
    Bark getBark();
}

Now, I have

List<Animal> animals 

Which holds all animal types and I want to collect all instances of Bark
(which only exists in Dog).
I can do it Java 7 style

 ArrayList<Bark> barks = new ArrayList<>();
 for (Animal animal : AnimalsList) {
     if (animal instanceof Barkable){
         barks.add((Barkablle) dog).getBark();
     }
}

How do I do it in Java 8 streams?
Will it be as fast as the java-8 version?

Eran

You can use filter() to get the Barkable instances and then use map to get the Barks :

List<Bark> barks = AnimalsList.stream()
                              .filter (a -> a instanceof Barkable) 
                              .map (a -> ((Barkable) a).getBark())
                              .collect (Collectors.toList());

As for the running time, you'll have to test for yourself. It will depend on the size of the input list, and you may be able to make it run faster by using parallelStream() instead of stream() (if your input is large enough for a parallel stream to make a difference).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Converting enhanced loop into Java 8 Streams, filter

From Java

How to compare two ArrayList and get list1 with filter using java streams

From Dev

How to map elements of the list to their indices using Java 8 streams?

From Dev

How to filter list of lists using streams on the basis of a value in a list?

From Dev

How to get the first value for each distinct keys using Java 8 streams?

From Dev

How to do filter a list in Java8?

From Dev

How to filter nested streams in Java 8 with lambda productions

From Dev

Java 8 - Filter list inside map value

From Dev

Java 8 Streams Filter Intention of Lazy Evaluation

From Dev

How to multiply values in a list using java 8 streams

From Dev

How to sort Map<YearMonth, List<LocalDate>> with java 8 lambda and streams

From Dev

Modify property value of the objects in list using Java 8 streams

From Dev

How to collect into a Map forming a List in value when duplicate keys in streams, in Java 8

From Dev

How to using stream in java 8 filter two list object and set value to new List

From Dev

How to compare two ArrayList and get list1 with filter using java streams

From Dev

Filter on a value of inner Map using Java 8 streams

From Dev

Filter and modify list object using java 8 streams

From Dev

How to use java 8 streams to make a new list by using another's list objects values with filter?

From Dev

How to get List of List of Objects with Java Streams

From Dev

How to separate a List by a condition using Java 8 streams

From Dev

How do I sort a List of TreeSets with java8 streams

From Dev

How to get a list output from forEach loop in Java 8 Streams

From Dev

filter Map in Java 8 Streams

From Dev

Java 8 streams - How do I manipulate value in a group by result?

From Dev

How to multiply values in a list using java 8 streams

From Dev

Java8 : How to filter a map of List value via stream

From Dev

Java 8 streams - how to filter a list and get a value

From Dev

How to filter nested loops using Java 8 streams and filters?

From Dev

How does one Convert a List to Map with Java-8 Streams where Key is the ListValue and the Value is the amount of values of that specific list

Related Related

  1. 1

    Converting enhanced loop into Java 8 Streams, filter

  2. 2

    How to compare two ArrayList and get list1 with filter using java streams

  3. 3

    How to map elements of the list to their indices using Java 8 streams?

  4. 4

    How to filter list of lists using streams on the basis of a value in a list?

  5. 5

    How to get the first value for each distinct keys using Java 8 streams?

  6. 6

    How to do filter a list in Java8?

  7. 7

    How to filter nested streams in Java 8 with lambda productions

  8. 8

    Java 8 - Filter list inside map value

  9. 9

    Java 8 Streams Filter Intention of Lazy Evaluation

  10. 10

    How to multiply values in a list using java 8 streams

  11. 11

    How to sort Map<YearMonth, List<LocalDate>> with java 8 lambda and streams

  12. 12

    Modify property value of the objects in list using Java 8 streams

  13. 13

    How to collect into a Map forming a List in value when duplicate keys in streams, in Java 8

  14. 14

    How to using stream in java 8 filter two list object and set value to new List

  15. 15

    How to compare two ArrayList and get list1 with filter using java streams

  16. 16

    Filter on a value of inner Map using Java 8 streams

  17. 17

    Filter and modify list object using java 8 streams

  18. 18

    How to use java 8 streams to make a new list by using another's list objects values with filter?

  19. 19

    How to get List of List of Objects with Java Streams

  20. 20

    How to separate a List by a condition using Java 8 streams

  21. 21

    How do I sort a List of TreeSets with java8 streams

  22. 22

    How to get a list output from forEach loop in Java 8 Streams

  23. 23

    filter Map in Java 8 Streams

  24. 24

    Java 8 streams - How do I manipulate value in a group by result?

  25. 25

    How to multiply values in a list using java 8 streams

  26. 26

    Java8 : How to filter a map of List value via stream

  27. 27

    Java 8 streams - how to filter a list and get a value

  28. 28

    How to filter nested loops using Java 8 streams and filters?

  29. 29

    How does one Convert a List to Map with Java-8 Streams where Key is the ListValue and the Value is the amount of values of that specific list

HotTag

Archive