Thanks for the reply but I have given you the statement of the problem very clearly appreciate if you can solve that showing how to use retain all.
For now I have resolved it as shown below .
package com.company;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class adfg
{
//Method Get Frequency of Counts for each
word in the
string which returns a hashmap
public Map<Character, Integer> Mapofcharfreq(String w)
{
System.out.println("String received is" + w);
Map<Character, Integer> KeyFrequency = new HashMap<>();
Map<Character, Integer> charcount = new HashMap<>();
int k = 0;
int[] freq = new int[w.length()];
for (int m = 0; m < w.length(); m++) {
if (charcount.containsKey(w.trim().charAt(m)))
{
System.out.println("value of freq before increment for "+w.charAt(m)+"is"+freq[k]);
freq[k]++;
System.out.println("value of freq after increment for "+w.charAt(m)+"is"+freq[k]);
KeyFrequency.put(w.charAt(m), freq[k]);
} else {
//Need to mark the position so that we need to increment here
freq[m] = 1;
k=m;
System.out.println("since "+w.charAt(m)+"is not existing the frequncy is made as "+freq[m]);
KeyFrequency.put(w.charAt(m), 1);
charcount.put(w.charAt(m), 1);
}
}
System.out.println("Outside for loop ");
return KeyFrequency;
}
//Method to show uncommon key in the hashmap which returns a character array
public Character[] showUnCommonkey(Map<Character, Integer> m1, Map<Character, Integer> m2)
{
if (!(m1.keySet().equals(m2.keySet())))
{
HashSet<Character> unionKeys = new HashSet<>(m1.keySet());
unionKeys.addAll(m2.keySet());
unionKeys.removeAll(m1.keySet());
Character[] array = new Character[unionKeys.size()];
unionKeys.toArray(array);
return array;
}
else
{
return null;
}
}
public static void main(String[] args) throws IOException
{
//String str = "aabccd aaabcccdd aaaabccdd";
String str = "gghhhiiij gghhhiiijj ghhijk";
adfg a = new adfg();
String word[] = str.split(" ");
List<Map<Character, Integer>> l = new ArrayList();
for (int z = 0; z <= word.length - 1; z++)
{
l.add(a.Mapofcharfreq(word[z]));
System.out.println("Arrays as list************" + Arrays.asList(l.get(z)));
}
//Take all the hashmaps stored in the list and get the minimum values using Java streams
Map<Character, Integer> leastRepeatingCharmap = Stream.concat(l.get(0).entrySet().stream(), l.get(1).entrySet().stream())
.collect(Collectors.toMap(
entry -> entry.getKey().charValue(),
entry -> entry.getValue(), // The value
// The "merger"
(frequency1, frequency2) ->Math.min(frequency1,frequency2)
)
);
Map<Character, Integer> leastRepeatingCharmap1 = Stream.concat(l.get(2).entrySet().stream(), leastRepeatingCharmap.entrySet().stream())
.collect(Collectors.toMap(
entry -> entry.getKey(),
entry -> entry.getValue(), // The value
(frequency1, frequency2) ->Math.min(frequency1,frequency2)
)
);
//Remove the uncommon key
Character[] s1=a.showUnCommonkey(l.get(1), l.get(2));
for (Character s: s1 )
{
System.out.println("..........."+s);
leastRepeatingCharmap1.remove(s);
};
System.out.println("Arrays as list>>>>>>>>>" + Arrays.asList(leastRepeatingCharmap1));
}
}