1:filter():添加过滤条件
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List li = Arrays.asList(1,2,3,4,5,6);
System.out.println(li.stream()
.filter(n -> n>=3)//转换成新的stream
.collect//转换成List
(Collectors.toList()));//[3, 4, 5, 6]
}
}
2:map方法指对一个流中的值进行某种形式的转换
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List li = Arrays.asList(1,2,3,4,5,6);
System.out.println(li.stream()
.filter(n -> n>=3)//转换成新的stream
.map(m -> m+3)//map方法指对一个流中的值进行某种形式的转换
.collect//转换成List
(Collectors.toList()));//[6, 7, 8, 9]
}
}
3:limit方法和skip方法
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List li = Arrays.asList(1,2,3,4,5,6);
System.out.println(li.stream()
.filter(n -> n>=3)//转换成新的stream
.map(m -> m+3)//map方法指对一个流中的值进行某种形式的转换
.skip(1)//本来是[6, 7, 8, 9],跳过第一个就是 7, 8, 9]
.limit(2)// 7, 8, 9]只留前面2个变成[7, 8]
.collect//转换成List
(Collectors.toList()));//[7, 8]
}
}
4:去重和排序
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List li = Arrays.asList(3,3,3,7,5,6);
System.out.println(li.stream()
.distinct()//去重
.collect//转换成List
(Collectors.toList()));//[3, 7, 5, 6]
}
}
6:max方法和min方法,findFirst和findAny方法
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List li = Arrays.asList(3,3,3,7,5,6);
System.out.println(li.stream()
.distinct()//去重
// .max(Integer::compareTo).get());//获取最大值
.findFirst().get());
}
}
7:anyMatch方法
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List li = Arrays.asList(3,3,3,7,5,6);
System.out.println(li.stream()
.distinct()//去重
.anyMatch(i -> i==9));
}
}
8:求和
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List li = Arrays.asList(3,3,3,7,5,6);
System.out.println(li.stream()
.distinct()//去重
.reduce(Integer::sum).get());
}
}
9:变成set集合
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List li = Arrays.asList(3,3,3,7,5,6);
System.out.println(li.stream()
.collect(Collectors.toSet()));//[3, 5, 6, 7]
}
}
或者变成自己想要的set:
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List li = Arrays.asList(3,3,3,7,5,6);
System.out.println(li.stream()
.collect(Collectors.toCollection(HashSet::new)));//[3, 5, 6, 7]
}
}
10:对map集合的操作
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
Map params = new HashMap<>(16);
params.put("a","1");
params.put("b",null);
params.put("c","c");
System.out.println(getMap(params));
}
public static Map getMap(Map map){
return Optional.ofNullable(map)
.map(m -> {
Map ma = m.
entrySet().
stream().
filter((e) -> checkValue(e.getValue())).
collect(Collectors.toMap(
e -> (String) e.getKey(),
e -> Optional.ofNullable(e.getValue()).orElse("")
));
return ma;
})
.orElse(null);
}
private static boolean checkValue(Object object) {
if(object!=null && !object.equals("")){
return true;
}
return false;
}
}