.stream() java code example

Example 1: java streams

the Stream API is used to process collections
of objects. A stream is a sequence of objects
that supports various methods which can be
pipelined to produce the desired result.
  It is very similar to build().perform() 
  method in Actions class. It is chaining
  some operations in an order.

The features of Java stream are:
- A stream is not a data structure instead
it takes input from the Collections, 
Arrays or I/O channels.
- Streams don’t change the original data
structure, they only provide the result
as per the pipelined methods.
- Each intermediate operation is lazily
executed and returns a stream as a result,
hence various intermediate operations
can be pipelined. Terminal operations mark
the end of the stream and return the result.

Different Operations On Streams:

map ==> The map method is used to return a
stream consisting of the results of applying
the given function to the elements of this stream.
List number = Arrays.asList(2,3,4,5);
List square = number.stream().map(x->x*x).collect(Collectors.toList());

filter ==> The filter method is used to
select elements as per the Predicate passed as argument.
List names = Arrays.asList("Reflection","Collection","Stream");
List result = names.stream().filter(s->s.startsWith("S")).collect(Collectors.toList());

sorted ==> The sorted method is used to sort the stream.
List names = Arrays.asList("Reflection","Collection","Stream");
List result = names.stream().sorted().collect(Collectors.toList());

ArrayList<String> list3 = new ArrayList(Arrays.asList("monkey", "donkey","onion"));
List<String> list4 = list3.stream().filter(each->!"onion".equals(each)).collect(Collectors.toList());
System.out.println(list4);

static String reverse(String word){
    return Arrays.stream(word.split("")).reduce("", (x,y) -> y+x );
}

Example 2: stream java example

public class StreamBuilders 
{
     public static void main(String[] args)
     {
         Stream<Integer> stream = Stream.of( new Integer[]{1,2,3,4,5,6,7,8,9} );
         stream.forEach(p -> System.out.println(p));
     }
}

Example 3: java streams

Arrays.asList("a1", "a2", "a3")
    .stream()
    .findFirst()
    .ifPresent(System.out::println);  // a1

Example 4: using java 8 stream to process data in java

public class Employee {

	private int employeeID;
	private String employeeName;
	private String employeeGender;
	private String employeeCountry;
	private String employeeState;
	private String employeeCity;
	
	public Employee() {
		// TODO Auto-generated constructor stub
	}

	public Employee(int employeeID, String employeeName, String employeeGender, String employeeCountry,
			String employeeState, String employeeCity) {
		super();
		this.employeeID = employeeID;
		this.employeeName = employeeName;
		this.employeeGender = employeeGender;
		this.employeeCountry = employeeCountry;
		this.employeeState = employeeState;
		this.employeeCity = employeeCity;
	}

	public int getEmployeeID() {
		return employeeID;
	}

	public void setEmployeeID(int employeeID) {
		this.employeeID = employeeID;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public String getEmployeeGender() {
		return employeeGender;
	}

	public void setEmployeeGender(String employeeGender) {
		this.employeeGender = employeeGender;
	}

	public String getEmployeeCountry() {
		return employeeCountry;
	}

	public void setEmployeeCountry(String employeeCountry) {
		this.employeeCountry = employeeCountry;
	}

	public String getEmployeeState() {
		return employeeState;
	}

	public void setEmployeeState(String employeeState) {
		this.employeeState = employeeState;
	}

	public String getEmployeeCity() {
		return employeeCity;
	}

	public void setEmployeeCity(String employeeCity) {
		this.employeeCity = employeeCity;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "[Employee ID : " + employeeID + ", Employee Name : " + employeeName + ", Employee Gender : "
				+ employeeGender + ", Employee Country : " + employeeCountry + ", Employee State : " + employeeState
				+ ", Employee City : " + employeeCity + "]";
	}
	
	public static void main(String[] args) {
		ArrayList<Employee> employees=new ArrayList<Employee>();
		
		employees.add(new Employee(101, "John", "M", "United States", "California", "Los Angeles"));
		employees.add(new Employee(91, "Jacob", "M", "United States", "California", "Los Angeles"));
		employees.add(new Employee(111, "Lisa", "F", "United States", "California", "Los Angeles"));
		employees.add(new Employee(97, "Mary", "F", "United States", "California", "Sacramento"));
		employees.add(new Employee(76, "Christine", "F", "United States", "California", "Sacramento"));
		employees.add(new Employee(114, "David", "M", "United States", "California", "San Jose"));
		employees.add(new Employee(103, "Kevin", "M", "United States", "California", "Oakland"));
		employees.add(new Employee(109, "Joe", "M", "United States", "California", "Oakland"));
		employees.add(new Employee(119, "Mathew", "M", "United States", "California", "San Jose"));
		employees.add(new Employee(99, "Angelina", "F", "United States", "California", "San Diego"));
		employees.add(new Employee(98, "Tom", "M", "United States", "California", "San Diego"));
		employees.add(new Employee(116, "Curl", "M", "United States", "California", "Los Angeles"));
		employees.add(new Employee(66, "Christopher", "M", "United States", "California", "Oakland"));
		employees.add(new Employee(56, "Chelse", "F", "United States", "California", "Oakland"));
		employees.add(new Employee(88, "Murali", "M", "United States", "California", "San Jose"));
		employees.add(new Employee(87, "Daisy", "F", "United States", "California", "Sacramento"));
		employees.add(new Employee(85, "Niza", "F", "United States", "Virginia", "Richmond"));
		employees.add(new Employee(86, "Chris", "M", "United States", "Virginia", "Fairfax"));
		employees.add(new Employee(90, "Andrew", "M", "United States", "Virginia", "Reston"));
		
	}

}

Operations:

1. Get list of all the employees from "California"; Return a List
2. Count the number of Females; Return a Count
3. Add 10 to the ID of each Employee; Return the updated List
4. Sort in the Descending order by employee name (z-a); Return the List
5. Get the details of the second highest employee ID; Return the employee

Solution:

System.out.println(employees.stream().filter(employee->employee.getEmployeeState().equals("California")).collect(Collectors.toList()));
System.out.println(employees.stream().map(emp->emp.getEmployeeID()+10).collect(Collectors.toList()));
System.out.println(employees.stream().filter(employee->employee.getEmployeeGender().equalsIgnoreCase("f")).count());
System.out.println(employees.stream().sorted((e1,e2)->e2.getEmployeeName().compareTo(e1.getEmployeeName())).collect(Collectors.toList()));
Collections.sort(employees, (s1,s2)-> s2.getEmployeeID() - s1.getEmployeeID());
System.out.println(employees.get(1));

Tags:

Cpp Example