Home > Enterprise >  How to Optimize the code after creating map
How to Optimize the code after creating map

Time:04-03

I want to make the second part work thanks for the help:)

If arraylist is ['A','a','B','C','C'] then dups:['A','a','C','C'] nondups: ['B'] I tried :

Map<String, Long> counts = nums.parallelStream()
  .collect( Collectors.groupingBy( {k -> k.toLowerCase()}, Collectors.counting()) )

It gives counts: {a:2, b:1, c:2}

Now I am finding dups and nondups which is done by below code or alternate suggestion to solve this:

 Map<Boolean, List<String>> hasDup =
            counts.entrySet().parallelStream()
              .collect(Collectors.partitioningBy(
                 entry -> entry.getValue() > 1,
                 Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
List<String> dup = hasDup.get(true);
     List<String> nodup = hasDup.get(false);

dup:['a','c'] nondups:['b'] I need help in second part because the expected output is not the same as I want which is

dups:['A','a','C','C'] nondups: ['B']

CodePudding user response:

It seems you're mixing things, because the algorithm does not returns what you want even if it was a non-parallel stream. In the second part you are obtaining the key you've used to group due to duplication counting, which also is in lowercase. So, you won't retrieve the uppercase ones.

Then, what you really need is to "unroll" the dups as many times as the associated count.

List<Character> nums = List.of('A' , 'a' , 'B' , 'C' , 'C');
Map<String, Long> counts = nums.parallelStream()
    .map(String::valueOf)
    .collect(Collectors.groupingBy(String::toLowerCase, Collectors.counting()));

List<Character> dup = new ArrayList<>();
List<Character> nodup = new ArrayList<>();
nums.forEach(x -> (counts.get(x.toString().toLowerCase()) > 1 ? dup : nodup).add(x));

Then, since it iterates over nums to fill both dup and nodup lists, you have the original Characters.

CodePudding user response:

As Leo notes, the first part does not benefit from parallelism since the work nearly all happens in the collector. The same probably applies to the second part as well.

I think that the you will get significantly better performance if you don't use streams.

  • Related