实际编码,qc遇到的问题
package test;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class normalTest {
public static boolean compareStr(String a, String b) {
return a.compareTo(b) >= 0;
}
public static void main(String[] args) {
List<Custom> strList = new ArrayList<>();
strList.add(new Custom("a"));
strList.add(new Custom("m"));
strList.add(new Custom("d"));
strList.add(new Custom("y"));
strList.add(new Custom("e"));
strList.add(new Custom("s"));
strList.add(new Custom("!"));
strList = strList.stream().filter(o -> compareStr("g", o.getField())).collect(Collectors.toList());
strList.forEach(o->o.setField("#"));
System.out.println(strList);
}
@Data
static class Custom {
Custom (String str) {
field = str;
}
public String field;
}
}
可以用stream()先filter后再collect
得到新的list再foreach来进行修改
能不能一次循环解决,也就是filter的同时做foreach的操作