如何使用流将Map<String, Object>数组转换为Map<Long, Long>

问题描述 投票:0回答:1
public class Sample {

    Map<Long, Long> count(Map<String, UserStats> visits[]) {

        Map<Long, Long> visitsNum = new HashMap<>();
        Arrays.stream(visits).map(m -> {

            Map<Long, Long> visitsList = new HashMap<>();
            m.forEach((k, v) -> {
                String microSerNum = k;
                UserStats user = v;
                Long count1 = 0L;
                Long longMicroSerNum =0L;
                try {
                    longMicroSerNum = Long.parseLong(microSerNum);
                } catch (Exception e) {

                    System.out.println("Microserive number is not a Long data type");
                    return;
                }
                if (user == null)
                {
                    System.out.println("User not visited the Microservice " + longMicroSerNum);
                    return;
                }
                else {
                    Optional<Long> count = user.VisitCounter;

                    if (count.isPresent())

                    count1 = count.get();

                    System.out.println("Number of times user vistied the Microservice " +longMicroSerNum + " is "+ count1);

                }

                visitsList.put(longMicroSerNum, count1);

            });
            
            return visitsList;

        }).//collect(Collectors.toMap(e->e.getKey, e->e.getValue));
        
        forEach((e) -> {
            System.out.println(e);
        });


        return visitsNum; // should return a map
    }

    record UserStats(Optional<Long> VisitCounter) {

    }

    public static void main(String ar[]) {
        Sample a = new Sample();
        UserStats u = new UserStats(Optional.of(10L));
        UserStats u1 = new UserStats(Optional.of(20L));

        Map<String, UserStats> m1[] = new HashMap[10];

        Map<String, UserStats> m2 = new HashMap<>();

        Map<String, UserStats> m3 = new HashMap<>();
        m2.put("20", u);
        m2.put("30", u1);
        m3.put("", u);
        m3.put("50", null);

        m1[0] = m2;
        m1[1] = m3;

        Map<Long, Long> visitsNum = a.count(m1); //should catch the returned map
        
    }

}

必须将 String、Object 的可选值解析为 Long 类型,然后丢弃空对象或空字符串值。

得到以下控制台输出:

Number of times user vistied the Microservice 30 is 20
Number of times user vistied the Microservice 20 is 10
{20=10, 30=20}
Microserive number is not a Long data type
User not visited the Microservice 50
{}
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Map.forEach(java.util.function.BiConsumer)" because "m" is null
    at CodingPractice.Sample.lambda$0(Sample.java:12)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
    at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
    at CodingPractice.Sample.count(Sample.java:48)
    at CodingPractice.Sample.main(Sample.java:78)
arrays dictionary java-8 nullpointerexception stream
1个回答
1
投票
Map<String, UserStats> m1[] = new HashMap[10];

进一步:

m1[0] = m2;
m1[1] = m3;

长度为 10 的数组中只有两个元素,其他元素则设置为

null
。那么,如果
count()
为 null,您认为在
m.forEach
调用
m
时会发生什么?

Map<String, UserStats> m1[] = new HashMap[10];
更改为
Map<String, UserStats> m1[] = new HashMap[2];

旁白:您正在创建一个通用数组,但这很糟糕(编译器应该抱怨);你的问题主要是由于你使用了一个数组(固定大小和逻辑错误),其中像列表这样的东西会更安全。或者至少过滤流中的非空元素...

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