Created
May 2, 2017 20:24
-
-
Save RGrun/528ea358e30227c3b29d334059e223ed to your computer and use it in GitHub Desktop.
Sorting list problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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