Java 8 Mapping( 映射)练习题
来源:互联网 发布:软件发明专利范文 编辑:程序博客网 时间:2024/06/15 03:53
1.给定一个数字列表, 如何返回每个数字的平方列表?例如, 给定 [1、2、3、4、5], 您应该返回 [1、4、9、16、25]。
Given a list of numbers, how would you return a list of the square of each number?For example, given [1, 2, 3, 4, 5] you should return [1, 4, 9, 16, 25].
List<Integer> number1=Arrays.asList(1,2,3,4,5);List<Integer> number2=number1.stream().map(n->n*n).collect(toList());number2.forEach(System.out::println);
2.给定两个数字列表, 如何返回所有对数字?例如, 给定一个列表 [1, 2, 3] 和一个列表 [3, 4] 你应该返回 [(1, 3), (1, 4), (2, 3), (2, 4), (33), (3, 4)]。为了简单起见, 可以将一对作为具有两个元素的数组表示。
Given two lists of numbers, how would you return all pairs of numbers? For example,given a list [1, 2, 3] and a list [3, 4] you should return [(1, 3), (1, 4), (2, 3), (2, 4), (3,3), (3, 4)]. For simplicity, you can represent a pair as an array with two elements.
List<Integer> number3=Arrays.asList(1,2,3);List<Integer> number4=Arrays.asList(3,4);List<int[]> parms=number3.stream() .flatMap(i->number4.stream() .map(j->new int[]{i,j})) .collect(toList());parms.forEach(pair -> System.out.println("[" + pair[0] + ", " + pair[1] + "]"));
3.如何扩展前面的示例以返回其总和可被3整除的对?例如, (2、4) 和 (3、3) 是有效的。
How would you extend the previous example to return only pairs whose sum is divisible by 3? For example, (2, 4) and (3, 3) are valid.
import java.util.*;import static java.util.stream.Collectors.toList;public class Filtering{ public static void main(String...args){ List<Integer> number3=Arrays.asList(1,2,3); List<Integer> number4=Arrays.asList(3,4); List<int[]> parms1=number3.stream() .flatMap(i->number4.stream() .filter(j->(i+j)%3==0) .map(j->new int[]{i,j})) .collect(toList()); parms1.forEach(pair -> System.out.println("[" + pair[0] + ", " + pair[1] + "]")); }}
- Java 8 Mapping( 映射)练习题
- [ElasticSearch]映射(Mapping)
- 映射集合(Mapping collections)
- 纹理映射(Texture Mapping)
- Elasticsearch之映射(mapping)。
- mapping 映射
- Message Mapping(消息映射)(转)
- 内存映射文件(File Mapping)API
- 投影纹理映射(Projective Texture Mapping)
- 什么是纹理映射(Texture Mapping)
- 投影纹理映射(Projective Texture Mapping)
- 投影纹理映射(Projective Texture Mapping)
- ggplot2作图详解3:映射(mapping)
- 投影纹理映射(Projective Texture Mapping)
- ggplot2作图详解:映射(mapping)
- [OpenGL] shadow mapping(实时阴影映射)
- HDR中的Tone Mapping(色调映射)
- 投影纹理映射(Projective Texture Mapping)
- 进入Unity开发的奇幻世界 Roll A Ball
- Android IPC进程间通讯都有哪些方式
- 处理GitHub不允许上传大于100M文件问题
- Oracle 插入insert语句
- Fast RCNN学习笔记
- Java 8 Mapping( 映射)练习题
- Java设计模式_(行为型)_迭代模式
- Android_AsyncTask的使用
- Zookeeper+dubbo分布式开发学习(一)
- Vsftpd原理安装配置详解
- GKBillowNoiseSource
- Harris角点检测变种之边缘提取
- 未完成--字典--《数据结构与算法》
- 在QT中使用irrlicht