11
Mar

Character statistics

package sr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class test111 {

	private static final Integer Integer = null;

	public static void main(String[] args) {
		String str = "Jackychan";

		Map<Character, Integer> map = new LinkedHashMap<>();

		for (Character x : str.toCharArray()) {
			map.put(x, map.containsKey(x) ? map.get(x) + 1 : 1);
		}

		System.out.println(map);

		// first non-repeating character
		for (Map.Entry<Character, Integer> es : map.entrySet()) {
			if (1 == es.getValue()) {
//				System.out.println("First non-occurring char  => "+es.getKey());
				break;
			}
		}

		// 2nd / 3rd etc. non-repeating character
		int rep = 3;
		int count = 0;
		Character ch = null;
		for (Map.Entry<Character, Integer> es : map.entrySet()) {
			if (1 == es.getValue()) {
				count++;

				if (rep == count) {
					ch = es.getKey();
					break;
				}
			}
		}

//		System.out.println("2nd non-repeating character => "+ch);

		// first maximum occurring character
		Integer max = 0;
		for (Character c : map.keySet()) {
			if (map.get(c) > max)
				max = map.get(c);
		}

		for (Map.Entry<Character, Integer> es : map.entrySet()) {
			if (max == es.getValue()) {
//				System.out.println("First max-occuring char => "+es.getKey());
				break;
			}
		}

		// last non-repeating character
		Character m = null;
		for (Map.Entry<Character, Integer> es : map.entrySet()) {
			if (1 == es.getValue()) {
				m = es.getKey();
			}
		}
//		System.out.println("Last non-occuring char => "+m);

		// 2nd or 3rd or nth max occuring character -- ToDo
		Integer mxx = 4;
		List<Integer> lst = new ArrayList<>(map.values());
		Collections.sort(lst, Collections.reverseOrder());
//		System.out.println(lst);

		Integer trd = null;
		for (int j = 0; j < mxx; j++) {
			trd = lst.get(j);
		}
		System.out.println(trd);
		Integer cnt = 1;
		Character chr = null;

		for (Map.Entry<Character, Integer> es : map.entrySet()) {
			cnt++;
			if (trd == es.getValue()) {
				if (trd == cnt) {
					chr = es.getKey();
				}
			}
		}
		System.out.println("4th max-occuring char => " + chr);

	}

}