JAVA New Features – Stream – Part 3

This blog explains about Stream part 3 in java is given below : 

map(Function)

map(List of Objects -> List of Objects)

ArrayList<Integer> al = new ArrayList<Integer>(); 
	al.add(10); 
	al.add(20);
	al.add(30); 
//	System.out.println(al);
	al
	.stream()
	.map(no -> no/2)
	.forEach(System.out::println);
	ArrayList<String> al = new ArrayList<String>(); 
	al.add("Kavin"); 
			 
	al.add("Viyan");
	al.add("Arul"); 
//	System.out.println(al);
	al
	.stream()
	.map(name -> name.toUpperCase())
	.forEach(System.out::println);

Example 2:

Stream<Employee> empStream = empList.stream(); 
	

	Stream<Employee> expEmpStream = empStream
	.filter(emp -> emp.getSalary()>40000);
	
	Stream<String> empName2Stream = expEmpStream.map(emp -> emp.getName()); 
	empName2Stream.forEach(System.out::println);

It should be rewritten as below:

empList
	.stream() //Stream of Employees 
	.filter(emp -> emp.getSalary()>40000) //>40000 Salaried
	.map(emp -> emp.getName()) //Names 
	.forEach(System.out::println);

limit()

	HashSet<Integer> al = new HashSet<Integer>(); 
		al.add(10); 
		al.add(20);
		al.add(30);
		al.add(40);
		
		al
		.stream()
		.limit(2)
		.forEach(System.out::println);

skip()

		ArrayList<Integer> al = new ArrayList<Integer>(); 
		al.add(10); 
		al.add(20);
		al.add(30);
		al.add(40);
		
		al
		.stream()
		.limit(3)
		.skip(2)
		.forEach(System.out::println);

total

int[] ar = {10,20,30,40}; 
int total = 0; 
total = total + ar[0]; 
total = total + ar[1]; 

reduce()

ArrayList<Integer> al = new ArrayList<Integer>(); 
		al.add(10); 
		al.add(20);
		al.add(30);
		al.add(40);
		
		Optional<Integer> result = al
		.stream()
		.reduce((no1, no2) -> no1+no2);
		
		System.out.println(result.get());

//Converting ArrayList to Array

ArrayList<Integer> al = new ArrayList<Integer>(); 
		al.add(10); 
		al.add(20);
		al.add(30);
		al.add(40);
		
		Object[] ob = al
		.stream()
		.toArray();
		
		for(Object o: ob)
			System.out.println(o);

//Get max from an ArrayList

	ArrayList<Integer> al = new ArrayList<Integer>(); 
		al.add(10); 
		al.add(200);
		al.add(30);
		al.add(40);
			
		
		Optional<Integer> result = al
		.stream()
		.max((elem1, elem2) -> elem1.compareTo(elem2));
		
		System.out.println(result.get());https://payilagam.com/java/java-features-stream-api-part-2/

Parallel Stream

ArrayList<Integer> al = new ArrayList<Integer>(); 
		al.add(10); 
		al.add(200);
		al.add(30);
		al.add(40);
			
		
		al
		.parallelStream()
		.forEachOrdered(System.out::println);

//ParallelStream

	ArrayList<Integer> al = new ArrayList<Integer>(); 
		al.add(10); 
		al.add(200);
		al.add(30);
		al.add(40);
			
		
		al
		.stream()
		.parallel()
		.forEachOrdered(System.out::println);
HashMap<String, Integer> hm = new HashMap<String,Integer>();
hm.put("Viyan", 100);
hm.put("Kavin", 200);
hm.put("Iyal", 300);
hm.put("Pari", 123);

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

//Sorting Based on Key: Alphabetical Order

	HashMap<String, Integer> hm = new HashMap<String,Integer>();
	hm.put("C Viyan", 100);
	hm.put("A Kavin", 200);
	hm.put("B Iyal", 300);
	hm.put("D Pari", 123);
	
	hm
	.keySet()
	.stream()
	.sorted()
	.forEach(System.out::println);
	
HashMap<String, Integer> hm = new HashMap<String,Integer>();
	hm.put("C Viyan", 100);
	hm.put("A Kavin", 200);
	hm.put("B Iyal", 300);
	hm.put("D Pari", 123);
	
	hm
	.entrySet()
	.stream()
	.sorted(Map.Entry.comparingByKey())
	.forEach(System.out::println);
	HashMap<String, Integer> hm = new HashMap<String,Integer>();
	hm.put("C Viyan", 100);
	hm.put("A Kavin", 200);
	hm.put("B Iyal", 300);
	hm.put("D Pari", 123);
	
	hm
	.entrySet()
	.stream()
	.sorted(Map.Entry.comparingByValue())
	.forEach(System.out::println);

//anyMatch()

HashMap<String, Integer> hm = new HashMap<String,Integer>();
	hm.put("C Viyan", 100);
	hm.put("A Kavin", 200);
	hm.put("B Iyal", 300);
	hm.put("D Pari", 123);
	
	Set<String> l = hm
	.keySet()
	.stream()
	.collect(Collectors.toSet()); 
	System.out.println(l);

anyMatch()

	HashMap<String, Integer> hm = new HashMap<String,Integer>();
	hm.put("Viyan", 100);
	hm.put("Kavin", 200);
	hm.put("Iyal", 300);
	hm.put("Pari", 123);
	
	Set<String> l = hm
	.keySet()
	.stream()
	.collect(Collectors.toSet()); 
	System.out.println(l);
	
boolean result = l
	.stream()
	.anyMatch(name -> name.endsWith("n"));
System.out.println(result);

//allMatch()

HashMap<String, Integer> hm = new HashMap<String,Integer>();
	hm.put("Viyan", 100);
	hm.put("Kavin", 200);
	hm.put("Iyal", 300);
	hm.put("Pari", 123);
	
	Set<String> l = hm
	.keySet()
	.stream()
	.collect(Collectors.toSet()); 
	System.out.println(l);
	
boolean result = l
	.stream()
	.allMatch(name -> name.endsWith("n"));
System.out.println(result);

//noneMatch()


	HashMap<String, Integer> hm = new HashMap<String,Integer>();
	hm.put("Viyan", 100);
	hm.put("Kavin", 200);
	hm.put("Iyal", 300);
	hm.put("Pari", 123);	HashMap<String, Integer> hm = new HashMap<String,Integer>();
	hm.put("Viyan", 100);
	hm.put("Kavin", 200);
	hm.put("Iyal", 300);
	hm.put("Pari", 123);
	
	Set<String> l = hm
	.keySet()
	.stream()
	.collect(Collectors.toSet()); 
	System.out.println(l);
	
boolean result = l
	.stream()
	.anyMatch(name -> name.endsWith("n"));
System.out.println(result);

	
	Set<String> l = hm
	.keySet()
	.stream()
	.collect(Collectors.toSet()); 
	System.out.println(l);
	
boolean result = l
	.stream()
	.noneMatch(name -> name.endsWith("n"));
System.out.println(result);