Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java8中,如何一次迭代两个数组?_Java_Arrays_Java Stream - Fatal编程技术网

在Java8中,如何一次迭代两个数组?

在Java8中,如何一次迭代两个数组?,java,arrays,java-stream,Java,Arrays,Java Stream,我正在编写一个从CSV文件获取数据的Java程序。对于每一行数据,我需要使用相应的头作为键将每个数据元素放入一个映射中。例如,headerRow[7]和dataElements[7]应该是映射中的键值对 下面是我传统上使用Java编写的代码: private Map<String, Double> readLine(String[] headerRow, String[] dataElements) { Map<String, Double> headerToDa

我正在编写一个从CSV文件获取数据的Java程序。对于每一行数据,我需要使用相应的头作为键将每个数据元素放入一个映射中。例如,headerRow[7]和dataElements[7]应该是映射中的键值对

下面是我传统上使用Java编写的代码:

private Map<String, Double> readLine(String[] headerRow, String[] dataElements) {
    Map<String, Double> headerToDataMap = new HashMap<>(); 
    for (int i=0; i < nextLine.length; i++) {
        headerToDataMap.put(headerRow[i], Double.valueOf(dataElements[i]));
    }
    return headerToDataMap;
}
private Map readLine(String[]headerRow,String[]dataElements){
Map headerToDataMap=新HashMap();
for(int i=0;i

有没有一种方法可以让我使用Java 8流来编写这段代码?请记住,我同时在两个数组上进行迭代?

在vanilla Java 8中,最接近这一点的可能是

 IntStream.range(0, nextLine.length())
    .boxed()
    .collect(toMap(i -> headerRow[i], i -> dataElements[i]));

您可以使用双功能界面使某些内容稍微长一点

private Map<String, Double> readLine(String[] headerRow, String[] dataElements) {
        Map<String, Double> headerToDataMap = new HashMap<>();  
        BiFunction<String,String, KeyValue> toKeyValuePair = (s1,s2) -> new KeyValue(s1,s2);
        IntStream.range(0, nextLine.length)
                .mapToObj(i -> toKeyValuePair.apply(headerRow[i], dataElements[i]) )
                .collect(Collectors.toList())
                .stream()
                .forEach(kv -> {
                    headerToDataMap.put(kv.getKey(), Double.valueOf(kv.getValue()));
                });
        return headerToDataMap;
    }

不幸的是,没有内置的Zip方法没有
headerRow::[]
+1@PeterLawrey我也认为这是一个简洁的符号。Guava 21将有
Streams.zip
,但我认为之后将其转换为
地图将不会比这更好。不知何故,OP的
nextLine.length
已变异为
nextLine.length()
,但我仍然不知道
nextLine
是什么……此外,您的代码缺少
字符串到
转换。无装箱解决方案:
IntStream.range(0,nextLine.length)。collect(HashMap::new,(m,I)->m.put(headerRow[I],Double.valueOf(dataElements[i]),Map::putAll).collect(Collectors.toList()).stream()步骤的意义是什么?它们没有为操作添加任何有用的内容,只是增加了开销。您可以简单地将两者都删除。此外,如果您知道
collect
,为什么最后要求助于
forEach
?为什么要先创建
BiFunction
,只是为了在另一个lambda表达式中使用它,而不是在
mapToObj
步骤中的lambda表达式中编写预期的代码?@Holger感谢您的评论。我会尽快检查并反馈
private class KeyValue {
        String key;
        String value;
        public String getKey() {
            return key;
        }
        public void setKey(String key) {
            this.key = key;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
        public KeyValue(String key, String value) {
            super();
            this.key = key;
            this.value = value;
        }
        public KeyValue() {
            super();
        }       
    }