Skip to content

Instantly share code, notes, and snippets.

@RGrun
Created May 2, 2017 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RGrun/528ea358e30227c3b29d334059e223ed to your computer and use it in GitHub Desktop.
Save RGrun/528ea358e30227c3b29d334059e223ed to your computer and use it in GitHub Desktop.
Sorting list problem
public static void main(String[] args) {
char[] aArray = {'a', 'a', 'a', 'a', 'a'};
char[] bArray = {'b', 'b', 'b'};
char[] cArray = {'c', 'c'};
char[][] combinedList = {aArray, bArray, cArray};
TreeMap<Character, Integer> map = new TreeMap<>();
StringBuilder builder = new StringBuilder();
for(char[] ar : combinedList) {
for(char k : ar) {
if(map.containsKey(k)) {
int count = map.get(k);
map.put(k, ++count);
} else {
map.put(k, 1);
}
}
}
map.forEach((k, v) -> System.out.println("k: " + k + " v: " + v));
while(!map.isEmpty()) {
Set<Map.Entry<Character, Integer>> entrySet = map.entrySet();
entrySet.forEach((e) -> {
Character k = e.getKey();
int v = e.getValue();
builder.append(k);
--v;
if(v > 0) {
map.put(k, v);
} else {
map.remove(k);
}
});
}
System.out.println(builder.toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment