Skip to main content

Posts

Showing posts from May 12, 2017

Stream with filter and collection - Java 8

We can use stream with filters and collections:- a) Stream provide link with a collection. b) Filter can filter the data from linked filter c) Collector can collect the data based on used filter. Here is the simple demonstration:- package com.shadab.collections.stream; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class StreamDemo { public static void main(String[] args) { List<Integer> listIntegers = Arrays.asList(8, 9, 10, 7, 4,3,2,1,5,6,7,9); System.out.println("All numbers are:- "+listIntegers); Set<Integer> uniqueOddNumbers = listIntegers.stream().filter(number -> number % 2 != 0).collect(Collectors.toSet());   System.out.println("Unique odd numbers are:- "+uniqueOddNumbers); } } If you get surprised by seeing number -> number  in java code please click here . Output:- All numbers are:- [8, 9, 10, 7, 4, 3, 2, 1, 5, 6, 7, 9] Unique odd numbers are:- [1