Skip to content

Instantly share code, notes, and snippets.

@giuniu
Created December 10, 2011 09:54
Show Gist options
  • Save giuniu/789fae6a2d9d6e074f36 to your computer and use it in GitHub Desktop.
Save giuniu/789fae6a2d9d6e074f36 to your computer and use it in GitHub Desktop.
ダブり文字列並べ替えロジック Java版
package exercise;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeSet;
public class SortWordsJ {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
for(String str : args) {
Integer cnt = map.get(str);
if (cnt == null)
map.put(str, 1);
else
map.put(str, cnt + 1);
}
SortedSet<Entry<String, Integer>> set = new TreeSet<>(new Comparator<Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
int result = - o1.getValue().compareTo(o2.getValue());
if (result == 0)
result = o1.getKey().compareTo(o2.getKey());
return result;
}});
set.addAll(map.entrySet());
for (Entry<String, Integer> entry : set)
System.out.println(entry.getKey());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment