Java 8 Streams 从列表中过滤掉空 String[] <String[]>?

问题描述 投票:0回答:3

假设我们有 2 个测试 String[],类似于:

String[] test = {"","","","",""};
String[] test2 = {"Test","Name", "5.00", "NY", "Single"};

然后我们将它们附加到这样的列表中:

List<String[]> testList = new ArrayList<>();
testList.add(test);
testList.add(test2);

我想做的目标是使用 Java 8 流在每个 String[] 的索引 2 中找到支付的美元总和。但我似乎无法过滤掉每个索引中包含空值的 String[] 。这是我的尝试:

public static void main(String[] args) {
        String[] test = {"","","","",""};
        String[] test2 = {"Test","Name", "5.00", "NY", "Single"};
        List<String[]> testList = new ArrayList<>();
        testList.add(test);
        testList.add(test2);
        System.out.println(testList);
        testList.stream().map(Arrays::asList).filter(i -> !i.isEmpty())
                                             .mapToDouble(columnsPerRow -> 
                                                Double.parseDouble(columnsPerRow.get(2)))
                                             .sum();
        System.out.println(testList);
    }

我尝试使用 filter(i -> !i.isEmpty()) 来过滤它

它抛出的错误仍然是:

Exception in thread "main" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at Main.lambda$main$0(Main.java:23)
    at java.util.stream.ReferencePipeline$6$1.accept(ReferencePipeline.java:244)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.DoublePipeline.collect(DoublePipeline.java:500)
    at java.util.stream.DoublePipeline.sum(DoublePipeline.java:411)
    at Main.main(Main.java:23)
java java-8 java-stream
3个回答
2
投票

i -> !i.isEmpty()
中,
i
ArrayList
,它包含
("","","","","")
,如果它不为空,你可以检查索引2处的值,你可以这样做

double r = testList.stream().map(Arrays::asList)
                   .filter(i -> !i.isEmpty())              // ensure list not empty
                   .map(i -> i.get(2))                     // keep only 3rd element
                   .filter(i -> !i.isEmpty())              // ensure string isn't empty
                   .mapToDouble(Double::parseDouble)       // map to double
                   .sum();

第一个

.filter(i -> !i.isEmpty())
可以替换为
.filter(i -> i.size() >= 3)


1
投票

您过滤了错误的实体:

public static void main(String[] args)
{
    String[] test = {"","","","",""};
    String[] test2 = {"Test","Name", "5.00", "NY", "Single"};
    String[] test3 = {"Test","Name", "13.00", "NY", "Single"};
    List<String[]> testList = new ArrayList<>();
    testList.add(test);
    testList.add(test2);
    testList.add(test3);
    double sum = testList.stream()
                    .map(Arrays::asList)
                    .filter(i -> i.size() > 2 && !i.get(2).isEmpty())
                    .mapToDouble(columnsPerRow -> Double.parseDouble(columnsPerRow.get(2)))
                    .sum();
    System.out.println(sum);
}

产生预期的

18.0

过滤阶段删除少于 2 个元素或第二个元素为空的数组。


1
投票

相信你的话,过滤掉每个索引中包含空值的 String[]。

    String[] test = {"","","","",""};
    String[] test2 = {"Test","Name", "5.00", "NY", "Single"};

    double total = Stream.of(test, test2)
            .filter(arr -> !Arrays.stream(arr).allMatch(String::isEmpty))
            .mapToDouble(arr -> Double.parseDouble(arr[2]))
            .sum();
    
    System.out.println(total);

输出是:

5.0

我的代码仅过滤掉字符串数组,其中all的值都是空字符串。因此,如果美元值为

""
并且数组包含其他非空字符串,则会出现异常。这就是您想要的没有美元值的数组只需要保存空字符串的情况。为了验证。您可能想要捕获
NumerFormatException
并报告验证错误。或者在进行计算之前验证流。

© www.soinside.com 2019 - 2024. All rights reserved.