对于您给定的数据结构;
Map<String, Long> countMapOfColumn = list.stream()
.filter(innerList -> innerList.size() >= column)
.map(innerList -> innerList.get(column - 1))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
where column = 3
,会给你一张地图{"C", "G"}
作为键集,值将为您提供给定列中每个字符的计数。它也适用于非均匀矩阵,因为它只会跳过没有第 n 列的行。
任何列的唯一计数将决定收集方法生成的映射的大小;countMapOfColumn.size()
要获取单个字符计数,请使用带有键的结果映射作为输入,例如获取C
in column = 3
, use countMapOfColumn.get("C")
,这将返回2
但我宁愿用番石榴Table
,您可以直接选择任何给定的行或列,没有任何麻烦,那么它只是获取第 n 列,并过滤我们的重复项。
update
获取以连续列表开头的行数list = {"A", "B"}
;
List<String> startingList = Arrays.asList("A", "B");
Long count = list.stream()
.filter(innerList -> innerList.size() >= startingList.size()
&& innerList.subList(0, startingList.size()).equals(startingList))
.count();