List列表元素常用操作 - 循环forEach、删除 removeIf、替换replaceAll和排序sort
在这一页,我们将提供java 8
List
例子,包括forEach()
, removeIf()
, replaceAll()
和sort()
。
List
中的forEach()
方法继承自java.lang.Iterable
,removeIf()
方法继承自java.util.Collection
。
所有这些方法都是在Java 8
中添加的。
找到每个方法的例子。
循环 - forEach()
找到forEach()
方法的语法
forEach(Consumer<? super T> action)
它接受java 8
Consumer
,并迭代每个元素的列表。
ForEachDemo.java
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
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;
}
}
输出
id:1, Name:Mahesh
id:2, Name:Ram
id:3, Name:Krishna
删除 - removeIf()
找到removeIf()
方法的语法。
removeIf(Predicate<? super E> filter)
它从List
中删除所有满足给定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);
}
}
输出
---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()
找到replaceAll()
方法的语法。
replaceAll(UnaryOperator operator)
它通过UnaryOperator
得到的结果来替换List
中的每个元素。
ReplaceAllDemo.java
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;
}
}
输出
---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()
找到sort()方法的语法。
sort(Comparator<? super E> c)
我们需要传递比较器(Comparator
),在此基础上对列表进行排序。
SortDemo.java
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
import java.util.Comparator;
public class PersonComparatorByName implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
return p1.getName().compareTo(p2.getName());
}
}
输出
---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
参考文献
【1】Java 8 List Example with forEach(), removeIf(), replaceAll() and sort()