Java 8 Streams
Tags: java
Published: 2020-02-11 12:06:00

Streams are very basic in java 8 features . Below is my example where I'm going to cover different API methods .


public class StreamBuilders {

     public static void main(String[] args){
         List<Integer> list = new ArrayList<Integer>();
         for(int i = 1; i< 10; i++){
             list.add(i);
         }
         Stream<Integer> stream = list.stream();
         // stream count 
            long count=list.stream().count();
        //limit 
            System.out.println("fileter value :"+   list.stream().filter(i->i>5).count());
            System.out.println("count is "+count);
         List<Integer> evenNumbersList = stream.filter(i -> i%2 == 0).collect(Collectors.toList());
         System.out.print(evenNumbersList);
        // filter operations 
        List<String> nameList = Arrays.asList("Ram", "Amit", "Ashok", "Manish", "Rajat");
        nameList.stream().filter(n->n.startsWith("A")).collect(Collectors.toList()).forEach(System.out::println);

        nameList.stream().filter(n -> !n.startsWith("A")).collect(Collectors.toList()).forEach(System.out::println);
	//map
	list.stream().map(n->n*2).filter(n->n>10).forEach(System.out::println);

    //complex operations
    List<Person> persons =
    Arrays.asList(
        new Person("Max", 18,"10000","computers"),
        new Person("Peter", 23,"15000","maths"),
        new Person("Pamela", 23,"17000","computers"),
        new Person("David", 12,"10000","physics"));

// grouping the objects with age 

    List<Person>p1= persons.stream().filter(n->n.name.startsWith("P")).collect(Collectors.toList());
    System.out.println(p1);
    Map<Integer,List<Person>> personWithGroupBy=persons.stream().collect(Collectors.groupingBy(p->p.age));
    personWithGroupBy.forEach((age,p)->System.out.format("age %s: %s\n", age, p));
//grouping the persons with department 

    Map<String,List<Person>> personsWithGroupByDept=persons.stream().collect(Collectors.groupingBy(Person::getDept));
    personsWithGroupByDept.forEach((dept,p)->System.out.println(" dept : "+dept+"  "+p));
    
// grouping the persons with salary 
    Map<String,List<Person>> personWithSalary= persons.stream().collect(Collectors.groupingBy(p->p.salary));
    personWithSalary.forEach((salary,p)->System.out.println("salary :"+salary+"  "+p));

Map<String,List<Person>> personWithDept=persons.stream().collect(Collectors.groupingBy(p->p.dept));
personWithDept.forEach((dept,p)->System.out.println("dept : "+dept+ " "+p));

//sorting persons based on person name

	persons.sort(Comparator.comparing(p->p.name));
		System.out.println("sorted list "+persons);
	persons.sort(Comparator.comparing(p->p.dept));
	System.out.println("sorted with dept "+persons);

      }
}


class Person {
     public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSalary() {
        return salary;
    }
    public void setSalary(String salary) {
        this.salary = salary;
    }
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }
    String name;
        int age;
        String salary;
        String dept;

    Person(String name, int age,String salary,String dept) {
        this.name = name;
        this.age = age;
        this.salary=salary;
        this.dept=dept;
    }

    @Override
    public String toString() {
        return name +" "+salary+" " +dept+" "+age;
    }
}



/*

out put :
c:\rajesh\MicroServices\Java8Practice>java StreamBuilders
fileter value :4
count is 9
[2, 4, 6, 8]Amit
Ashok
Ram
Manish
Rajat
12
14
16
18
[Peter 15000 maths 23, Pamela 17000 computers 23]
age 18: [Max 10000 computers 18]
age 23: [Peter 15000 maths 23, Pamela 17000 computers 23]
age 12: [David 10000 physics 12]
 dept : maths  [Peter 15000 maths 23]
 dept : physics  [David 10000 physics 12]
 dept : computers  [Max 10000 computers 18, Pamela 17000 computers 23]
salary :17000  [Pamela 17000 computers 23]
salary :15000  [Peter 15000 maths 23]
salary :10000  [Max 10000 computers 18, David 10000 physics 12]
dept : maths [Peter 15000 maths 23]
dept : physics [David 10000 physics 12]
dept : computers [Max 10000 computers 18, Pamela 17000 computers 23]
sorted list [David 10000 physics 12, Max 10000 computers 18, Pamela 17000 computers 23, Peter 15000 maths 23]
sorted with dept [Max 10000 computers 18, Pamela 17000 computers 23, Peter 15000 maths 23, David 10000 physics 12]


*/