【Java 8 新特性】List列表元素常用操作 - 循环forEach、删除 removeIf、替换replaceAll和排序sort

List列表元素常用操作 - 循环forEach、删除 removeIf、替换replaceAll和排序sort

在这一页,我们将提供java 8 List例子,包括forEach(), removeIf(), replaceAll()sort()

List中的forEach()方法继承自java.lang.IterableremoveIf()方法继承自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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫巳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值