Java 8 List Example with forEach(), removeIf(), replaceAll() and sort()

By Arvind Rai, May 04, 2016
On this page we will provide java 8 List example with forEach(), removeIf(), replaceAll() and sort(). forEach() method in the List has been inherited from java.lang.Iterable and removeIf() method has been inherited from java.util.Collection. replaceAll() and sort() methods are from java.util.List. All these methods have been added in Java 8. Find the examples of each method.

forEach()

Find the syntax of forEach() method.
forEach(Consumer<? super T> action)
It accepts java 8 Consumer and iterates the list for each element.
ForEachDemo.java
package com.concretepage;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class ForEachDemo {
    public static void main(String[] args) {
    	List<Person> list = new ArrayList<>();    	
	list.add(new Person(1, "Mahesh"));
	list.add(new Person(2, "Ram"));
	list.add(new Person(3, "Krishna"));  
	Consumer<Person> style = (Person p) -> System.out.println("id:"+p.getPid() +", Name:"+p.getName());
	list.forEach(style);
    }
} 
Person.java
package com.concretepage;
public class Person {
	private int pid;
	private String name;
	public Person() {}
	public Person(int pid, String name){
		this.pid = pid;
		this.name = name;
	}
	public int getPid() {
		return pid;
	}
	public void setPid(int pid) {
		this.pid = pid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
} 
Find the output.
id:1, Name:Mahesh
id:2, Name:Ram
id:3, Name:Krishna 

removeIf()

Find the syntax of removeIf() method.
removeIf(Predicate<? super E> filter)
It removes all the elements from the List which satisfies the given Predicate.
RemoveIfDemo.java
package com.concretepage;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class RemoveIfDemo {
    public static void main(String[] args) {
    	        List<Person> list = new ArrayList<>();    	
		list.add(new Person(1, "Mahesh"));
		list.add(new Person(2, "Ram"));
		list.add(new Person(3, "Krishna"));  
		Consumer<Person> style = (Person p) -> System.out.println("id:"+p.getPid() +", Name:"+p.getName());		
		System.out.println("---Before delete---");
		list.forEach(style);
		int pid = 2;
    	        Predicate<Person> personPredicate = p-> p.getPid() == pid;
	        list.removeIf(personPredicate);
		System.out.println("---After delete---");	    
	        list.forEach(style);
	}
} 
Find the output.
---Before delete---
id:1, Name:Mahesh
id:2, Name:Ram
id:3, Name:Krishna
---After delete---
id:1, Name:Mahesh
id:3, Name:Krishna 

replaceAll()

Find the syntax of replaceAll() method.
replaceAll(UnaryOperator operator)
It replaces each element of the List by the result obtained by applying UnaryOperator.
ReplaceAllDemo.java
package com.concretepage;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
public class ReplaceAllDemo {
    public static void main(String[] args) {
    	List<Person> list = new ArrayList<>();    	
		list.add(new Person(1, "Mahesh"));
		list.add(new Person(2, "Ram"));
		list.add(new Person(3, "Krishna"));  
		Consumer<Person> style = (Person p) -> System.out.println("id:"+p.getPid() +", Name:"+p.getName());
		System.out.println("---Before replaceAll---");
		list.forEach(style);
		UnaryOperator<Person> unaryOpt = pn -> modifyName(pn); 
		list.replaceAll(unaryOpt);
		System.out.println("---After replaceAll---");		
		list.forEach(style);
	}
        private static Person modifyName(Person p){
    	        p.setName(p.getName().concat(" -God"));
		return p;
        }
} 
Find the output.
---Before replaceAll---
id:1, Name:Mahesh
id:2, Name:Ram
id:3, Name:Krishna
---After replaceAll---
id:1, Name:Mahesh -God
id:2, Name:Ram -God
id:3, Name:Krishna -God 

sort()

Find the syntax of sort() method.
sort(Comparator<? super E> c)
We need to pass Comparator on the basis of which List will be sorted.
SortDemo.java
package com.concretepage;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class SortDemo {
    public static void main(String[] args) {
    	List<Person> list = new ArrayList<>();    	
	list.add(new Person(1, "Mahesh"));
	list.add(new Person(2, "Ram"));
	list.add(new Person(3, "Krishna"));  
	Consumer<Person> style = (Person p) -> System.out.println("id:"+p.getPid() +", Name:"+p.getName());		
	System.out.println("---Before Sorting---");
	list.forEach(style);
	list.sort(new PersonComparatorByName());
	System.out.println("---After Sorting---");
	list.forEach(style);		
    }
} 
PersonComparatorByName.java
package com.concretepage;
import java.util.Comparator;
public class PersonComparatorByName implements Comparator<Person> {
    @Override
    public int compare(Person p1, Person p2) {
        return p1.getName().compareTo(p2.getName());
    }
} 
Find the output.
---Before Sorting---
id:1, Name:Mahesh
id:2, Name:Ram
id:3, Name:Krishna
---After Sorting---
id:3, Name:Krishna
id:1, Name:Mahesh
id:2, Name:Ram 
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us