Some handy code for JVM8

How to map with casting in Java8

Original code:

Stream.of(objects)
 .filter(c -> c instanceof Client)
 .map(c -> (Client) c)
 .map(Client::getID)
 .forEach(System.out::println);

Better one:

Stream.of(objects)
 .filter(Client.class::isInstance)
 .map(Client.class::cast)
 .map(Client::getID)
 .forEach(System.out::println);

How to convert an array to Stream?

Stream<String> str = Arrays.stream(array);
 str.forEach(x -> System.out.println(x));

How to find duplicates in a list?

The following snippet will filter out null values from the given list and then leave only those elements which are duplicated:

List<String> list = 
 Lists.newArrayList("aa", "bb", "cc", null, "aa", "bb", "aa", null, null);
 list.stream()
  .filter(Objects::nonNull)
  .filter(tag -> Collections.frequency(list, tag) > 1)
  .collect(Collectors.toSet())
  .forEach(System.out::println);

The results of the above code is aa, bb.