提交 ad103605 编写于 作者: 武汉红喜's avatar 武汉红喜

lambda

上级 fbe68c57
...@@ -6,6 +6,10 @@ import java.util.List; ...@@ -6,6 +6,10 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.partitioningBy;
import static java.util.stream.Collectors.toList;
/** /**
* Created by shenhongxi on 2018/1/11. * Created by shenhongxi on 2018/1/11.
*/ */
...@@ -30,17 +34,19 @@ public class Test { ...@@ -30,17 +34,19 @@ public class Test {
.reduce(0, Integer::sum); .reduce(0, Integer::sum);
System.out.println(totalPoints); System.out.println(totalPoints);
Map<Status, List<Task>> map = tasks.stream().collect(Collectors.groupingBy(Task::getStatus)); Map<Status, List<Task>> map = tasks.stream().collect(groupingBy(Task::getStatus));
System.out.println(map); System.out.println(map);
Map<Status, Long> map2 = tasks.stream().collect(Collectors.groupingBy(Task::getStatus, Collectors.counting())); Map<Status, Long> map2 = tasks.stream().collect(groupingBy(Task::getStatus, Collectors.counting()));
System.out.println(map2); System.out.println(map2);
Map<Boolean, List<Task>> map3 = tasks.stream().collect(partitioningBy(e -> e.getPoint() > 7));
System.out.println(map3);
Collection<String> result = tasks Collection<String> result = tasks
.stream() .stream()
.mapToInt(Task::getPoint) .mapToInt(Task::getPoint)
.mapToDouble(point -> point * 100 / totalPoints) .mapToDouble(point -> point * 100 / totalPoints)
.mapToObj(percentage -> (int) percentage + "%") .mapToObj(percentage -> (int) percentage + "%")
.collect(Collectors.toList()); .collect(toList());
System.out.println(result); System.out.println(result);
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册