Gathered all in one place.
Using Google Guava's « com.google.common.collect.Multiset<Character> Interface to find repeating Character's.
String str = "how are you doing today";
Multiset<Character> set = HashMultiset.create();
for (char c : str.toCharArray()) {
set.add( c );
}
for( Multiset.Entry<Character> entry : set.entrySet() ) {
System.out.println("Element :-> "+entry.getElement()+" Repeat Cnt :-> "+entry.getCount());
}
java8 « usage of Map.merge function
:
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 1);
map.merge(1, 5, (prevValue, newValue) -> prevValue + newValue );
// key 1 is present, so new value 5 will be added to previous value 1, [K:V]=[1:6]
map.put(2, 5);
map.merge(2, 10, (prevValue, newValue) -> prevValue < newValue ? prevValue : newValue);
map.merge(1, 10, (prevValue, newValue) -> null);
// Since the BiFunction results in null, the element will be removed from map
Example:
String str = "how are you doing today";
System.out.println("Using LinkedHashMap to get first most repeated char.");
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for (char c : str.toCharArray()) {
map.merge(c, 1, (prevValue, newValue) -> prevValue + newValue );
}
maxValueFromMap( map );
Output:
Using LinkedHashMap to get first most repeated char.
Char ['o'] repeated 4 times
Char [' '] repeated 4 times
To get max repeated Character value count from Map
use Collections.max(listofElements) or stream().max(Comparator).get()
Returns the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface.
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
map.put('y', 5);
map.put('a', 7);
map.put('s', 6);
map.put('h', 7);
int maxValueInMap = Collections.max( map.values() );
System.out.println( maxValueInMap ); // 7
int maxValueInMap2 = map.values().stream().max( Comparator.comparing( e -> e) ).get();
System.out.println( maxValueInMap2 ); // 7
Example:
public static Character maxValueFromMap( Map<Character, Integer> map) {
Character mostRepeatedChar = ' ';
int maxValueInMap = Collections.max( map.values() );
for (Entry<Character, Integer> entry : map.entrySet()) {
if ( entry.getValue() == maxValueInMap ) {
System.out.format("Char ['%s'] repeated %d times\n", entry.getKey(), maxValueInMap);
mostRepeatedChar = entry.getKey();
}
}
return mostRepeatedChar;
}
using java version below 8. map.containsKey( key )
public static Map<Character, Integer> charCountFromString_Java7(String str) {
Map<Character, Integer> map = new HashMap<>();
for (char c : str.toCharArray()) {
if( map.containsKey( c ) ) {
map.put(c, map.get(c)+1 );
} else {
map.put(c, 1);
}
/*Integer count = map.get(c); // null if this map contains no mapping for the key
if ( count == null ) {
count = 0;
}
map.put(c, count + 1);*/
}
return map;
}
using java 8.
public static Map<Character, Integer> charCountFromString_Java8(String str) {
Map<Character, Integer> map = new HashMap<>();
IntStream chars = str.chars();
//Stream<Integer> boxedInt = chars.boxed(); // Returns a Stream consisting of the elements boxed to an Integer.
Stream<Character> boxedChars = chars.mapToObj( x -> (char) x );
Set<Entry<Character, Long>> enteries =
boxedChars.collect( Collectors.groupingBy( boxedChar -> boxedChar , Collectors.counting() )).entrySet();
Map<Character, Integer> mapObj =
enteries.stream().collect( Collectors.toMap( Map.Entry::getKey, entry -> entry.getValue().intValue() ) );
//mapObj.forEach( (k,v)->{ System.out.println(k+":"+v); } ); // (int)(long)entry.getValue()
map = mapObj;
return map;
}
public class MostRepeatedChar {
public static void main(String[] args) {
String str = "how are you doing today";
Map<Character, Integer> repeatedCharsCount = charCountFromString_Java8(str);
// charCountFromString_Java7(str);
maxValueFromMap( repeatedCharsCount );
}
}