package com.java8tutorial;

import java.util.Arrays;

import java.util.Collection;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

import java.util.stream.Stream;

public class TestStreaming {

       public static void main(String[] args) {

              // Print the stream

              Stream<String> stream = Stream.of(“a”, “b”, “c”, “d”);

              stream.forEach(System.out::println);

              //Output

              /*a

          b

          c

          d*/

              //Print from source

              Collection<String> collection = Arrays.asList(“John”, “Doe”, “Mark”, “Johnson”);

              Stream<String> stream1 = collection.stream();

              stream1.forEach(System.out::println);

              /*

               * John

                     Doe

                     Mark

                     Johnson */

              //Print from list

              List<String> list = Arrays.asList(“John”, “Doe”, “Mark”, “Johnson”);

              Stream<String> stream2 = collection.stream();

              stream2.forEach(System.out::println);

              //Print from source

              Set<String> set = new HashSet<>(list);

              Stream<String> stream3 = collection.stream();

              stream3.forEach(System.out::println);

              String[] strArray = {“a”, “b”, “c”, “d”};

              Stream<String> stream4 = Arrays.stream(strArray);

              stream4.forEach(System.out::println);

              String[] strrArray = {“John”, “Doe”, “Mark”, “Johnson”};

              Stream<String> stream5 = Arrays.stream(strrArray);

              stream5.forEach(System.out::println);

       }

}

//Output

a

b

c

d

John

Doe

Mark

Johnson

John

Doe

Mark

Johnson

John

Doe

Mark

Johnson

a

b

c

d

John

Doe

Mark

Johnson

Stream Api

package com.java8tutorial;

import java.util.ArrayList;

import java.util.List;

import java.util.stream.Collectors;

public class StreamingExample {

       public static void main(String[] args) {

              //Java 7 method

              List<Product> list = new ArrayList<Product>();

              for(Product product : getProducts()) {

                     if(product.getPrice()>0) {

                           list.add(product);

                     }

              }

                     for(Product product : list) {

                           System.out.println(product);

                     }

                     /*

                      * Output:

                                  Product [id=1, name=Zinc, price=200.0]

                                  Product [id=2, name=Bed, price=120.0]

                                  Product [id=3, name=Robber, price=65.0]

                                  Product [id=4, name=Table, price=95.0]

                                  Product [id=5, name=Television, price=400.0]*/

                     //Java 8

                     List<Product> list1 =list.stream().filter((product)->product.getPrice()

                                  >0).collect(Collectors.toList());

                     list1.forEach(System.out::println);

                     /*

                      * Output:

                                  Product [id=1, name=Zinc, price=200.0]

                                  Product [id=2, name=Bed, price=120.0]

                                  Product [id=3, name=Robber, price=65.0]

                                  Product [id=4, name=Table, price=95.0]

                                  Product [id=5, name=Television, price=400.0]*/

                     getProducts()

                     .stream().filter((product)->product.getPrice()>0)

                     .forEach(System.out::println);

                     /*

                      * Output:

                                  Product [id=1, name=Zinc, price=200.0]

                                  Product [id=2, name=Bed, price=120.0]

                                  Product [id=3, name=Robber, price=65.0]

                                  Product [id=4, name=Table, price=95.0]

                                  Product [id=5, name=Television, price=400.0]*/

              }

       private static List<Product> getProducts(){

              List<Product> list = new ArrayList<Product>();

              list.add(new Product(1, “Zinc”, 200));

              list.add(new Product(2, “Bed”, 120));

              list.add(new Product(3, “Robber”, 65));

              list.add(new Product(4, “Table”, 95));

              list.add(new Product(5, “Television”, 400));

              return list;

       }

}

package com.java8tutorial;

public class Product {

       private int id;

       private String name;

       private float price;

       public Product(int id, String name, float price) {

              super();

              this.id = id;

              this.name = name;

              this.price = price;

       }

       public int getId() {

              return id;

       }

       public void setId(int id) {

              this.id = id;

       }

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       public float getPrice() {

              return price;

       }

       public void setPrice(float price) {

              this.price = price;

       }

       @Override

       public String toString() {

              return “Product [id=” + id + “, name=” + name + “, price=” + price + “]”;

       }

}

package com.sorting.stream;

import java.util.ArrayList;

import java.util.Comparator;

import java.util.List;

import java.util.stream.Collectors;

public class SortTutorial {

       public static void main(String[] args) {

              List<String> list = new ArrayList<String>();

              list.add(“Mango”);

              list.add(“Gova”);

              list.add(“Pinapple”);

              list.add(“Pear”);

              List<String> sortedList=list.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());

           System.out.println(sortedList);

           //Lambad expression

           List<String> sortedList0=list.stream().sorted((o1,o2)->o1.compareTo(o2)).collect(Collectors.toList());

           System.out.println(sortedList0);

         //output: [Gova, Mango, Pear, Pinapple]

         //output: [Gova, Mango, Pear, Pinapple]

           List<String> sortedList1=list.stream().sorted().collect(Collectors.toList());

           System.out.println(sortedList1);

         //output: [Gova, Mango, Pear, Pinapple]

           //Descending order

           List<String> sortedList2=list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

           System.out.println(sortedList2);

           //Lambad expression

           List<String> sortedList3=list.stream().sorted((o1,o2)->o2.compareTo(o1)).collect(Collectors.toList());

           System.out.println(sortedList3);

           //Output:

           //[Pinapple, Pear, Mango, Gova]

           //[Pinapple, Pear, Mango, Gova]

       }

}

public class Product {

       private int id;

       private String name;

       private float price;

       public Product(int id, String name, float price) {

              super();

              this.id = id;

              this.name = name;

              this.price = price;

       }

       public int getId() {

              return id;

       }

       public void setId(int id) {

              this.id = id;

       }

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       public float getPrice() {

              return price;

       }

       public void setPrice(float price) {

              this.price = price;

       }

       @Override

       public String toString() {

              return “Product [id=” + id + “, name=” + name + “, price=” + price + “]”;

       }

}

package com.java8tutorial;

import java.util.ArrayList;

import java.util.List;

import java.util.stream.Collectors;

public class StreamingExample {

       public static void main(String[] args) {

              //Java 7 method

              List<Product> list = new ArrayList<Product>();

              for(Product product : getProducts()) {

                     if(product.getPrice()>0) {

                           list.add(product);

                     }

              }

                     for(Product product : list) {

                           System.out.println(product);

                     }

                     /*

                      * Output:

                                  Product [id=1, name=Zinc, price=200.0]

                                  Product [id=2, name=Bed, price=120.0]

                                  Product [id=3, name=Robber, price=65.0]

                                  Product [id=4, name=Table, price=95.0]

                                  Product [id=5, name=Television, price=400.0]*/

                     //Java 8

                     List<Product> list1 =list.stream().filter((product)->product.getPrice()

                                  >0).collect(Collectors.toList());

                     list1.forEach(System.out::println);

                     /*

                      * Output:

                                  Product [id=1, name=Zinc, price=200.0]

                                  Product [id=2, name=Bed, price=120.0]

                                  Product [id=3, name=Robber, price=65.0]

                                  Product [id=4, name=Table, price=95.0]

                                  Product [id=5, name=Television, price=400.0]*/

                     getProducts()

                     .stream().filter((product)->product.getPrice()>0)

                     .forEach(System.out::println);

                     /*

                      * Output:

                                  Product [id=1, name=Zinc, price=200.0]

                                  Product [id=2, name=Bed, price=120.0]

                                  Product [id=3, name=Robber, price=65.0]

                                  Product [id=4, name=Table, price=95.0]

                                  Product [id=5, name=Television, price=400.0]*/

              }

       private static List<Product> getProducts(){

              List<Product> list = new ArrayList<Product>();

              list.add(new Product(1, “Zinc”, 200));

              list.add(new Product(2, “Bed”, 120));

              list.add(new Product(3, “Robber”, 65));

              list.add(new Product(4, “Table”, 95));

              list.add(new Product(5, “Television”, 400));

              return list;

       }

}

import java.util.ArrayList;

import java.util.Comparator;

import java.util.List;

import java.util.stream.Collectors;

public class SortTutorial {

       public static void main(String[] args) {

              List<String> list = new ArrayList<String>();

              list.add(“Mango”);

              list.add(“Gova”);

              list.add(“Pinapple”);

              list.add(“Pear”);

              List<String> sortedList=list.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());

           System.out.println(sortedList);

           //Lambad expression

           List<String> sortedList0=list.stream().sorted((o1,o2)->o1.compareTo(o2)).collect(Collectors.toList());

           System.out.println(sortedList0);

           List<String> sortedList1=list.stream().sorted().collect(Collectors.toList());

           System.out.println(sortedList1);

           //Descending order

           List<String> sortedList2=list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

           System.out.println(sortedList2);

           //Lambad expression

           List<String> sortedList3=list.stream().sorted((o1,o2)->o2.compareTo(o1)).collect(Collectors.toList());

           System.out.println(sortedList3);

       }

}

//output: [Gova, Mango, Pear, Pinapple]

//output: [Gova, Mango, Pear, Pinapple]

package adding;

//Program to find the missing number

import java.util.HashSet;

public class MissingNumber {

public int solution(int[] A) {
    HashSet<Integer> x = new HashSet<Integer>();
    for(int i=1; i<=A.length+1; i++) {
        x.add(i);
    }
    for(int a : A) {
        x.remove(a);
    }
    return x.iterator().next();
}


public static void main(String[] args) {
    MissingNumber mn = new MissingNumber();
    System.out.println(mn.solution(new int[] {-1, -3}));

}

}

Output: 1

package learningPractise;

//Printing strings

public class Duplicates {

       static void findDuplicates(String str) {

              String[] x = str.split(” “);

              for(String temp : x) {

                     System.out.println(temp);

              }

       }

       public static void main(String[] args) {

              findDuplicates(“The Lord is is is my my shephard”);

       }

}

/*Output: The

Lord

is

is

is

my

my

shephard*/

//Finding words

package learningPractise;

import java.util.HashMap;

public class Duplicates {

       static void findDuplicates(String str) {

              HashMap<String, Integer> h = new HashMap<>();

              String[] x = str.split(” “);

              for(String temp : x) {

                     h.put(temp, 1);

              }

              System.out.println(h);

       }

       public static void main(String[] args) {

              findDuplicates(“The Lord is is is my my shephard”);

       }

}

/*Output: {The=1, Lord=1, shephard=1, is=1, my=1}*/

//Find number of Occurance of a word

package learningPractise;

import java.util.HashMap;

public class Duplicates {

       static void findDuplicates(String str) {

              HashMap<String, Integer> h = new HashMap<>();

              String[] x = str.split(” “);

              for(String temp : x) {

                     if(h.get(temp)!=null) {

                           h.put(temp, h.get(temp) + 1);

                     }

                     else {

                     h.put(temp, 1);

                     }

              }

              System.out.println(h);

       }

       public static void main(String[] args) {

              findDuplicates(“The Lord is is is my my shephard”);

       }

}

/*Output: {The=1, Lord=1, shephard=1, is=3, my=2}

*/

//Find duplicate words

import java.util.HashMap;

import java.util.Iterator;

public class Duplicates {

       static void findDuplicates(String str) {

              HashMap<String, Integer> h = new HashMap<>();

              String[] x = str.split(” “);

              for(String temp : x) {

                     if(h.get(temp)!=null) {

                           h.put(temp, h.get(temp) + 1);

                     }

                     else {

                     h.put(temp, 1);

                     }

              }

              Iterator<String> temp = h.keySet().iterator();

              while(temp.hasNext()) {

                     String n = temp.next();

                     if(h.get(n)> 1) {

                           System.out.println(“Words : ” + n + ” appeared ” + h.get(n) + ” times”);

                     }

              }

       }

       public static void main(String[] args) {

              findDuplicates(“The Lord is is is my my shephard”);

       }

}

/*Output: Words : is appeared 3 times

Words : my appeared 2 times

*/

import java.util.HashMap;

import java.util.Iterator;

public class Duplicates {

       static void findDuplicates(String str) {

              HashMap<String, Integer> h = new HashMap<>();

              String[] x = str.split(” “);

              for(String temp : x) {

                     if(h.get(temp)!=null) {

                           h.put(temp, h.get(temp) + 1);

                     }

                     else {

                     h.put(temp, 1);

                     }

              }

              Iterator<String> temp = h.keySet().iterator();

              while(temp.hasNext()) {

                     String n = temp.next();

                     if(h.get(n)> 1) {

                           System.out.println(“Words : ” + n + ” appeared ” + h.get(n) + ” times”);

                     }

              }

       }

       static void findDuplicateCharacters(String str) {

              HashMap<Character, Integer> c = new HashMap<>();

              for(int i=0;i<str.length();i++) {

                     char a = str.charAt(i);

                     if(c.get(a)!=null) {

                           c.put(a, c.get(a)+1);

                     }

                     c.put(a, 1);

              }

              System.out.println(c);

       }

       public static void main(String[] args) {

              findDuplicates(“The Lord is is is my my shephard”);

              findDuplicateCharacters(“abacdeceffghddij”);

       }

}

/*Words : is appeared 3 times

Words : my appeared 2 times

{a=1, b=1, c=1, d=1, e=1, f=1, g=1, h=1, i=1, j=1}

import java.util.HashMap;

import java.util.Iterator;

public class Duplicates {

       static void findDuplicates(String str) {

              HashMap<String, Integer> h = new HashMap<>();

              String[] x = str.split(” “);

              for(String temp : x) {

                     if(h.get(temp)!=null) {

                           h.put(temp, h.get(temp) + 1);

                     }

                     else {

                     h.put(temp, 1);

                     }

              }

              Iterator<String> temp = h.keySet().iterator();

              while(temp.hasNext()) {

                     String n = temp.next();

                     if(h.get(n)> 1) {

                           System.out.println(“Words : ” + n + ” appeared ” + h.get(n) + ” times”);

                     }

              }

       }

       static void findDuplicateCharacters(String str) {

              HashMap<Character, Integer> c = new HashMap<>();

              for(int i=0;i<str.length();i++) {

                     char a = str.charAt(i);

                     if(c.get(a)!=null) {

                           c.put(a, c.get(a)+1);

                     }

                     else {

                     c.put(a, 1);

                     }

              }

              System.out.println(c);

       }

       public static void main(String[] args) {

              findDuplicates(“The Lord is is is my my shephard”);

              findDuplicateCharacters(“abacdeceffghddij”);

       }

}

/*Words : is appeared 3 times

Words : my appeared 2 times

{a=2, b=1, c=2, d=3, e=2, f=2, g=1, h=1, i=1, j=1}

*/

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

public class EmployeeProgram {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    List<Employee> list = new ArrayList<>();

    list.add(new Employee(1, "John Doe", 111, "inactive", 25000));
    list.add(new Employee(2, "Mark Bruce", 221, "active", 55000));
    list.add(new Employee(3, "Gary King", 221, "active", 85000));
    list.add(new Employee(4, "Jerry Khan", 111, "active", 80000));
    list.add(new Employee(5, "Congo Box", 111, "active", 66000));

    //List all employees by department
    list.stream().collect(Collectors.groupingBy(Employee::getDeptId, Collectors.toList()));
  Map<Integer, List<Employee>>listBasedOnDept=list.stream().collect(Collectors.groupingBy(Employee::getDeptId, Collectors.toList()));   
     listBasedOnDept.entrySet().forEach(x -> {
         System.out.println(x.getKey() + "--" + x.getValue()
         );
     });
       /*System.out.println("List all employees by department ....");
     221--[Employee [empid=2, empName=Mark Bruce, deptId=221, status=active, salary=55000], Employee [empid=3, empName=Gary King, deptId=221, status=active, salary=85000]]
     111--[Employee [empid=1, empName=John Doe, deptId=111, status=inactive, salary=25000], Employee [empid=4, empName=Jerry Khan, deptId=111, status=active, salary=80000], Employee [empid=5, empName=Congo Box, deptId=111, status=active, salary=66000]]*/


     //Counting no. of departments and employees in each department
    Map<Integer, Long> empCountDept = list.stream().collect(Collectors.groupingBy(Employee::getDeptId, Collectors.counting()));
    empCountDept.entrySet().forEach(x-> {
        System.out.println(x.getKey() + "--" + x.getValue());
    });
    /*System.out.println("//Counting no. of departments and employees in each department ....");
    221--2
    111--3*/

    //Count active and inactive employees
    Long activeEmpCount = list.stream().filter(x -> "active".equals(x.getStatus())).count(); 
    Long inactiveEmpCount = list.stream().filter(x -> "inactive".equals(x.getStatus())).count(); 
    System.out.println("active emp count -- " + activeEmpCount);
    System.out.println("inactive emp count -- " + inactiveEmpCount);

    /*System.out.println("Count active and inactive employees ....");
    active emp count -- 4
    inactive emp count -- 1*/

    System.out.println("Print maximum and minimum salaries ....");
    //Print maximum and minimum salaries
    Optional<Employee> emp1 =list.stream().max(Comparator.comparing(Employee::getSalary));
    Optional<Employee> emp2 =list.stream().min(Comparator.comparing(Employee::getSalary));
    System.out.println(emp1);
    System.out.println(emp2);

   /* Print maximum and minimum salaries ....
    Optional[Employee [empid=3, empName=Gary King, deptId=221, status=active, salary=85000]]
    Optional[Employee [empid=1, empName=John Doe, deptId=111, status=inactive, salary=25000]]*/

    System.out.println("List department and top salary earners ....");
    //List department and top salary earners
    Map<Integer, Optional<Employee>> topSalaryEmpDept = list.stream().collect(Collectors.groupingBy(Employee::getDeptId,
            Collectors.reducing(BinaryOperator.maxBy(Comparator.comparing(Employee::getSalary)))));
            topSalaryEmpDept.entrySet().forEach(x-> {
                System.out.println("Dept " + x.getKey() + " topEmp " + x.getValue());
    }); 

        /*  List department and top salary earners ....
            Dept 221 topEmp Optional[Employee [empid=3, empName=Gary King, deptId=221, status=active, salary=85000]]
            Dept 111 topEmp Optional[Employee [empid=4, empName=Jerry Khan, deptId=111, status=active, salary=80000]]*/

}

}