Oops! Sorry!!


This site doesn't support Internet Explorer. Please use a modern browser like Chrome, Firefox or Edge.

Learn Java 8

10k+ Downloads

(2.5k Reviews)

Stream API

Inside this cheat sheet you will learn how to run profitable advertising campaigns. You will learn how to launch multiple campaigns and how to find the winning ones so you can scale them and squeeze all of the profit for your business.

+ Print list of employee name

Step1 :- Create Employee class

     public class Employee {

         private int id;

         private String name;

         private int age;

         public Employee(int id, String name, int age) {

            super(); this.id = id;

            this.name = name;

            this.age = age; }

        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 int getAge() { return age; }

        public void setAge(int age) {

          this.age = age;

        }

       @Override public String toString() {

         return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";

         }

        }

Step2 :- Create EmployeeNameList class

public class EmployeeNameList {

     public static void main(String[] args) {

     List empList = Arrays.asList(new Employee(1,"Mannu",30),

                           new Employee(2,"Atul",30),

                           new Employee(3,"Asif",32),

                           new Employee(4,"Heera",32),

                            new Employee(5,"Manish",31));

List empNameList = empList.stream().map(e->e.getName()).collect(Collectors.toList());

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

   }

}


Output:

Mannu

Atul

Asif

Heera

Manish

+ Find Duplicate employee name

Step1 :- Create Employee class

     public class Employee {

         private int id;

         private String name;

         private int age;

         public Employee(int id, String name, int age) {

            super(); this.id = id;

            this.name = name;

            this.age = age; }

        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 int getAge() { return age; }

        public void setAge(int age) {

          this.age = age;

        }

       @Override public String toString() {

         return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";

         }

        }

Step2 :- Create DuplicateName class

public class DuplicateName{

     public static void main(String[] args) {

     List empList = Arrays.asList(new Employee(1,"Mannu",30),

                           new Employee(2,"Atul",30),

                           new Employee(3,"Asif",32),

                           new Employee(4,"Heera",32),

                            new Employee(5,"Asif",31));

List empNameList = empList.stream().map(e->e.getName()).collect(Collectors.toList());

Set uniqeName = new HashSet<>();

Set duplicateName = empNameList.stream().filter(name ->!uniqeName.add(name)).collect(Collectors.toSet());

duplicateName.forEach(System.out::println)   }

}


or

List nameList = empList.stream().map(e->e.getName()).collect(Collectors.toList()); Map mapOfName = nameList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); Set duplicateName = mapOfName.entrySet().stream().filter(e->e.getValue() > 1).map(e->e.getKey()).collect(Collectors.toSet()); duplicateName.forEach(System.out::println);


// or Using 

Collections.frequency(list,i)

count the frequency of each element using Collections.frequency()method then for each element in the collection list, if the frequency of any element more than one, then this element is a duplicate element

stream().filter(i->Collections.frequency(list,i)>1).collect(Collectors.toSet());


Set<String> empDuplicateNameListFrequency = nameList.stream().filter(e->Collections.frequency(nameList, e)>1).collect(Collectors.toSet()); empDuplicateNameListFrequency.forEach(System.out::println);





Output:

Asif

+ Find Duplicate Employee Name and count ouccurence of each name

Step1 :- Create Employee class

     public class Employee {

         private int id;

         private String name;

         private int age;

         public Employee(int id, String name, int age) {

            super(); this.id = id;

            this.name = name;

            this.age = age; }

        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 int getAge() { return age; }

        public void setAge(int age) {

          this.age = age;

        }

       @Override public String toString() {

         return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";

         }

        }

Step2 :- Create EmployeeNameList class

public class EmployeeNameList {

     public static void main(String[] args) {

     List empList = Arrays.asList(new Employee(1,"Mannu",30),

                           new Employee(2,"Atul",30),

                           new Employee(3,"Asif",32),

                           new Employee(4,"Heera",32),

                            new Employee(5,"Manish",31));

List empNameList = empList.stream().map(name->name.getName()).collect(Collectors.toList());

Map<String,Long> mapOfNames = empNameList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

mapOfNames.entrySet().forEach(e->{ System.out.println(e.getKey()+" : "+e.getValue()); });

 }

}

Output:

Asif : 2

Heera : 1

Atul : 1

Mannu : 1

+ OccurenceOfCharacter


Create OccurenceOfCharacter class

public class EmployeeNameList {

     public static void main(String[] args) {

    String input = "india";Map output = Arrays.stream(input.split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

     System.out.println("Output : "+output);


   }

}

+ Print list of employee name

Step1 :- Create Employee class

     public class Employee {

         private int id;

         private String name;

         private int age;

         public Employee(int id, String name, int age) {

            super(); this.id = id;

            this.name = name;

            this.age = age; }

        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 int getAge() { return age; }

        public void setAge(int age) {

          this.age = age;

        }

       @Override public String toString() {

         return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";

         }

        }

Step2 :- Create EmployeeNameList class

public class EmployeeNameList {

     public static void main(String[] args) {

     List empList = Arrays.asList(new Employee(1,"Mannu",30),

                           new Employee(2,"Atul",30),

                           new Employee(3,"Asif",32),

                           new Employee(4,"Heera",32),

                            new Employee(5,"Manish",31));

List empNameList = empList.stream().map(e->e.getName()).collect(Collectors.toList()); empNameList.forEach(System.out::println);

   }

}

Built With GroovePages