将 Set<List<String>> 元素与一些限制逐一比较,并计算 Java 中的出现次数



假设我有这样的Set<List<String>>

["A", "B", "C", "D", "E"]
["A", "B", "C" "D", "F"]
["G", "K", "P", "C"]
["Z", "C", "R", "D"]
["F", "Z", "U"]

当比较阶段开始时,我想放弃列表的所有第一项。例如

["B", "C" "D", "E"]
["B", "C" "D", "F"]
["K", "P", "C"]
["C", "R", "D"]
["Z", "U"]

(多亏了jhyot注释,我成功地完成了这个修剪)

new Set应该是这样的,然后我想要抓取第一个列表的第一个项目"B"并逐一比较其他列表项

比较(第一个列表的"B"→第二个列表的"B","C","D","F","K","P","C","C","R","D","Z","U")

当"B"的比较完成时,计数出现次数(在本例中为1),只增加一个重叠值在另一个列表/集合等

# Overlap # Number
[(  0    :  null)]
[(  1    :  null)]
[(  2    :  null)]
[(  3    :  null)]
[(  4    :  null)]
[(  5    :  null)]
[(  6    :  null)]
...
When "B" comparision completes (which is find one time in the other list increase 1 overlap 1.
# Overlap # Number
[(  0    :  null)]
[(  1    :  1)]
[(  2    :  null)]
[(  3    :  null)]
[(  4    :  null)]
[(  5    :  null)]
[(  6    :  null)]
...

当第一个列表项目完成并计数后,继续进行第一个列表第二项("C"),并与其他列表进行比较,并继续。

comparision(first list's "C" -> second list's "B", "C", "D", "F", "K", "P", "C", "C", "R", "D", "Z", "U")
comparision(first list's "D" -> second list's "B", "C", "D", "F", "K", "P", "C", "C", "R", "D", "Z", "U")
comparision(first list's "E" -> second list's "B", "C", "D", "F", "K", "P", "C", "C", "R", "D", "Z", "U")
comparision(second list's "B" -> "K", "P", "C", "C", "R", "D", "Z", "U")
comparision(second list's "C" -> "K", "P", "C", "C", "R", "D", "Z", "U")
...

What i try so far

我使用了collections . frequency,但是我不知道如何丢弃每个列表的第一项。

我写了自定义函数,成功的列表的项目,但我失去了列表的信息。例如

"A"
"B"
"C"
"D"
"E"
"A"
"B"
"C"
...

这种形式的,我认为无法进行比较。我还是不知道怎么修剪第一项。

获取列表并从所有子列表中排除第一个元素

Set<List<String>> dataSet = Test.getList();
// Exclude 1st Element from all sub-lists
List<List<String>>  withOutFirstElement = dataSet.stream()
.map(item -> item.subList(1, item.size()))
.collect(Collectors.toList());

计算出现次数:

// Map which keeps track of Every string in current sublist with its occurrences
Map<String, Long> occurrencesMap = new LinkedHashMap<>();
for (int i = 0; i < withOutFirstElement.size(); i++) {
// get sublist list for execution
List<String> subList = withOutFirstElement.get(i);
// Iterate current selected sublist
for (String searchedItem : subList) {
long count = 0;
// iterate every sublist, excluding current sublist, since k+1
// And get matching occurrences count out of it
for (int k = i + 1; k < withOutFirstElement.size(); k++) {
count += withOutFirstElement.get(k)
.stream().filter(item -> Objects.equals(item, searchedItem)).count();
}
if(occurrencesMap.containsKey(searchedItem)){
occurrencesMap.put(searchedItem, occurrencesMap.get(searchedItem)+count);
}else{
occurrencesMap.put(searchedItem, count);
}
}
}
System.out.println(occurrencesMap);

// {B=1, C=6, D=3, E=0, F=0, K=0, P=0, R=0, Z=0, U=0}
  1. 丢弃每个列表中的第一个元素

这可以通过使用List::remove(int index)

删除第一个元素来解决。
Set<List<String>> lists = ...; // defined a set of lists
List<List<String>> withoutFirst = new ArrayList<>(lists);
withoutFirst.forEach(list -> {if (!list.isEmpty()) list.remove(0);});

或者可以使用List::subList(https://docs.oracle.com/javase/8/docs/api/java/util/List.html#subList-int-int-):

)创建每个列表的副本。
List<List<String>> withoutFirst = lists
.stream()
.filter(list -> !list.isEmpty())
.map(list -> list.subList(1, list.size()))
.collect(Collectors.toList());

  1. 每个元素的出现次数计数可以实现如下:
for (int i = 0, n = withoutFirst.size(); i < n - 1; i++) {
List<String> remainder = withoutFirst
.subList(i + 1, n)
.stream() // Stream<List<String>> 
.flatMap(List::stream) // Stream<String>
.collect(Collectors.toList());
Map<String, Integer> frequencies = withoutFirst.get(i)
.stream() // 
.collect(Collectors.toMap(
x -> x,
x -> Collections.frequency(remainder, x)
));
}

关于重叠和进一步使用计算频率的部分尚未澄清。

最新更新