gpt4 book ai didi

java - 计算二维数组中出现的元素数量

转载 作者:行者123 更新时间:2023-12-01 06:13:55 28 4
gpt4 key购买 nike

大家好,现在我正在做一项研究,我需要统计“已实现”、“未实现”和“正在进行”的发生情况。我需要计算它,因为我的研究是关于马尔可夫决策过程,我需要定义路径。所以,我需要计算每个任务到每个 P1 或 P2 的结果,关于已实现、未实现和正在进行

My file is like that :

a->P1-> Interrupted 
a->P1-> Achieved
a->P1-> Achieved
a->P1-> Achieved
a->P1-> Achieved
a->P1-> Achieved
a->P1-> Achieved
a->P1-> Achieved
a->P1-> On-going
a->P1-> Interrupted
a->P2-> Interrupted
a->P2-> Achieved
a->P2-> Achieved
a->P2-> Achieved
a->P2-> Achieved
a->P2-> Achieved
a->P2-> Achieved
a->P2-> Achieved
a->P2-> Achieved
a->P2-> On-going
b->P1-> Achieved
b->P1-> Achieved
b->P1-> Achieved
b->P1-> Achieved
b->P1-> Achieved
b->P1-> Achieved
b->P1-> Achieved
b->P1-> On-going
b->P1-> Interrupted
b->P2-> Interrupted
b->P2-> Achieved
b->P2-> Achieved
b->P2-> Achieved
b->P2-> Achieved
b->P2-> Achieved
b->P2-> Achieved
b->P2-> Achieved
b->P2-> Achieved
b->P2-> On-going

and i want result :

a->P1-> achieve 7 times
a->P1-> Unachieved 2 times
a->P1-> On-going 1 time
a->P2-> achieve 8 times
........
........

谢谢!! :)

最佳答案

使用 Java 8 Streams 功能,您可以执行以下操作:

    Map<String, List<String>> byType =
Files.lines(Paths.get("c:\\Temp\\markov.txt"), Charset.defaultCharset()).
collect(
Collectors.groupingBy(String::toString));

byType.entrySet().stream().forEach(t -> System.out.println(t.getKey() + ":" + t.getValue().size()));

查看以下输出:

a->P2-> Interrupted:1
a->P1-> Achieved:7
b->P1-> Interrupted:1
b->P2-> On-going:1
a->P1-> On-going:1
b->P1-> On-going:1
a->P2-> On-going:1
b->P2-> Interrupted:1
a->P1-> Interrupted:1
b->P2-> Achieved:8
b->P1-> Achieved:7
a->P2-> Achieved:8

关于java - 计算二维数组中出现的元素数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28607578/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com