Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.TreeMap;
- public class Str {
- public static void main(String[] args) {
- HashMap<String,Integer> m = new HashMap<String,Integer>(); Integer t;
- for(int i=0;i<args.length;i++){
- if(m.containsKey(args[i])) {
- t=m.remove(args[i]);
- ++t;
- m.put(args[i], t);
- }else {
- m.put(args[i], 1);
- }
- }
- List<Map.Entry<String,Integer>> s = new ArrayList(m.entrySet());
- Comparator<Entry<String,Integer>> c = new MyComparator<Entry<String,Integer>>();
- Collections.sort(s, c);
- displayStrings(s);
- }
- private static void displayStrings(List<Entry<String, Integer>> s) {
- for(Entry<String, Integer> str:s){
- System.out.println(str.getKey()+" "+str.getValue());
- }
- }
- }
- import java.util.Comparator;
- public class MyComparator<T> implements Comparator<Entry<String, Integer>> {
- @Override
- public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
- // TODO Auto-generated method stub
- return o2.getValue() - o1.getValue();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement