Collectors
class provides following methods to convert a stream to collections like list,
set etc.,
Method
|
Description
|
public
static <T, C extends Collection<T>> Collector<T, ?, C>
toCollection(Supplier<C> collectionFactory)
|
Returns a
Collector that accumulates the input elements into a new Collection, in
encounter order.
|
public
static <T, K, U> Collector<T, ?, ConcurrentMap<K,U>>
toConcurrentMap(Function<? super T, ? extends K>
keyMapper,Function<? super T, ? extends U> valueMapper)
|
Returns a
concurrent Collector that accumulates elements into a ConcurrentMap whose
keys and values are the result of applying the provided mapping functions to
the input elements.
|
public
static <T, K, U> Collector<T, ?, ConcurrentMap<K,U>>
toConcurrentMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper, BinaryOperator<U>
mergeFunction)
|
Returns a
concurrent Collector that accumulates elements into a ConcurrentMap whose
keys and values are the result of applying the provided mapping functions to
the input elements.
|
public
static <T, K, U, M extends ConcurrentMap<K, U>> Collector<T,
?, M> toConcurrentMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,BinaryOperator<U>
mergeFunction, Supplier<M> mapSupplier)
|
Returns a
concurrent Collector that accumulates elements into a ConcurrentMap whose
keys and values are the result of applying the provided mapping functions to
the input elements.
|
public
static <T> Collector<T, ?, List<T>> toList()
|
Returns a
Collector that accumulates the input elements into a new List.
|
public
static <T, K, U> Collector<T, ?, Map<K,U>>
toMap(Function<? super T, ? extends K> keyMapper, Function<? super
T, ? extends U> valueMapper)
|
Returns a
Collector that accumulates elements into a Map whose keys and values are the
result of applying the provided mapping functions to the input elements.
|
public
static <T, K, U> Collector<T, ?, Map<K,U>>
toMap(Function<? super T, ? extends K> keyMapper, Function<? super
T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)
|
Returns a
Collector that accumulates elements into a Map whose keys and values are the
result of applying the provided mapping functions to the input elements.
|
public static
<T, K, U, M extends Map<K, U>> Collector<T, ?, M>
toMap(Function<? super T, ? extends K> keyMapper, Function<? super
T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction,
Supplier<M> mapSupplier)
|
Returns a
Collector that accumulates elements into a Map whose keys and values are the
result of applying the provided mapping functions to the input elements.
|
public
static <T> Collector<T, ?, Set<T>> toSet()
|
Returns a
Collector that accumulates the input elements into a new Set.
|
1. Get all employee staying in Hyderabad and salary
> 60000
employees.stream().filter((emp)
-> emp.getSalary()>60000 &&
emp.getCity().equals("Hyderabad")).collect(Collectors.toList());
2. Get employees id and firstName
employees.stream().collect(Collectors.toMap(Employee::getId, Employee::getFirstName))
employees.stream().collect(Collectors.toMap(Employee::getId, Employee::getFirstName))
Find
complete application below.
import java.util.Objects; public class Employee { private int id; private String firstName; private String lastName; private int age; private String city; private double salary; public Employee(int id, String firstName, String lastName, int age, String city, double salary) { super(); this.id = id; this.firstName = firstName; this.lastName = lastName; this.age = age; this.city = city; this.salary = salary; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public boolean equals(Object employee) { if (Objects.isNull(employee)) return false; if (!(employee instanceof Employee)) return false; Employee emp = (Employee) employee; return id == emp.id; } @Override public int hashCode() { return Objects.hash(id, firstName, lastName, age); } @Override public String toString() { return String.format("%s(%s,%d,%f)", firstName, city, age, salary); } }
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Test { public static List<Employee> getEmployees() { Employee emp1 = new Employee(1, "Hari Krishna", "Gurram", 26, "Bangalore", 40000); Employee emp2 = new Employee(2, "Joel", "Chelli", 27, "Hyderabad", 50000); Employee emp3 = new Employee(3, "Shanmukh", "Kummary", 28, "Chennai", 35000); Employee emp4 = new Employee(4, "Harika", "Raghuram", 27, "Chennai", 76000); Employee emp5 = new Employee(5, "Sudheer", "Ganji", 27, "Bangalore", 90000); Employee emp6 = new Employee(6, "Rama Krishna", "Gurram", 27, "Bangalore", 56700); Employee emp7 = new Employee(7, "PTR", "PTR", 27, "Hyderabad", 123456); Employee emp8 = new Employee(8, "Siva krishna", "Ponnam", 28, "Hyderabad", 98765); Employee emp9 = new Employee(9, "Raju", "Antony", 40, "Trivendram", 198765); Employee emp10 = new Employee(10, "Brijesh", "Krishnan", 34, "Trivendram", 100000); List<Employee> employees = new ArrayList<>(); employees.add(emp1); employees.add(emp2); employees.add(emp3); employees.add(emp4); employees.add(emp5); employees.add(emp6); employees.add(emp7); employees.add(emp8); employees.add(emp9); employees.add(emp10); return employees; } public static void main(String args[]) { List<Employee> employees = getEmployees(); /* Get all employee staying in Hyderabad and salary > 60000 */ List<Employee> empHyd = employees .stream() .filter((emp) -> emp.getSalary() > 60000 && emp.getCity().equals("Hyderabad")) .collect(Collectors.toList()); System.out.println(empHyd); /* get employees as id and firstName */ Map<Integer, String> empMap = employees.stream().collect( Collectors.toMap(Employee::getId, Employee::getFirstName)); System.out.println(empMap); } }
Output
[PTR(Hyderabad,27,123456.000000), Siva krishna(Hyderabad,28,98765.000000)] {1=Hari Krishna, 2=Joel, 3=Shanmukh, 4=Harika, 5=Sudheer, 6=Rama Krishna, 7=PTR, 8=Siva krishna, 9=Raju, 10=Brijesh}
No comments:
Post a Comment