- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
It's known that Java 8 brought a lot of new features that is improved Java language writing scalability. Today we look through one of them, that is, java List collection new methods. Let's take a look through some examples below.
removeIf(Predicate<? super E> filter)
It removes all the elements from the
RemoveIfDemo.java
forEach()
Syntax of forEach() method.
forEach(Consumer<? super T> action)
ForEachDemo.java
public class ForEachDemo {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person(1, "Bob"));
list.add(new Person(2, "Tom"));
list.add(new Person(3, "Kate"));
Consumer<Person> style =
(Person p) -> System.out.println("id:"+p.getPid() +", Name:"+p.getName());
list.forEach(style);
}
}
removeIf()
It removes all the elements from the
List
which satisfies the given Predicate
. RemoveIfDemo.java
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);
}
}
- Get link
- X
- Other Apps
Comments
Post a Comment