Home > Enterprise >  Compare Set<List<String>> elements one by one with some restrictions and count the occur
Compare Set<List<String>> elements one by one with some restrictions and count the occur

Time:09-17

Let's say i have Set<List<String>> like this

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

When comparision stage begin i want to discard all list's firs item. for example

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

(I managed to achieve this trim thanks to jhyot comment)

new Set should become like this and then i want to grab first list's first item "B" and compare other list's item one by one.

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

When "B"'s comparision finished count the occurences(in this example is 1) increase ONLY 1 OVERLAPPING value in another list/set etc.

# 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)]
...

When first list items finished and counted, proceed to first list second item("C") and compare to another lists and goes on.

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 tried so far

I used Collections.fruquency but i couldnt figure out how i discard the each list's first item.

I wrote custom function and succeded to list's items but i lost the list's information. for example

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

in this shape i assume comparision cannot be made. Still i cannot figure out how to trim first item.

CodePudding user response:

  1. Discarding the first element in each list

This can be resolved either by removing the first element using 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);});

Or a copy of each list may be created using 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. Counting occurrences per each element may be implemented as follows:
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)
        ));
}

The part about overlapping and further use of calculated frequencies is yet to be clarified.

CodePudding user response:

Get the list and exclude 1st element from all sublists

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());

To count occurances :

// 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}
  • Related