Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(final String[] args) {
        final List<String> asu = new ArrayList<String>();
        asu.add("2");
        asu.add("11");
        asu.add("7");
        asu.add("10");
        asu.add("7");
        asu.add("12");
        asu.add("2");
        asu.add("11");
        asu.add("11");
        asu.add("7");
        asu.add("7");
        asu.add("7");

        List<String> list = new ArrayList<String>();
        Map<String, Integer> counts = new HashMap<String, Integer>();
        list.addAll(asu);
        for (String item : list) {
            Integer count = counts.get(item);
            if (count == null) {
                count = 1;
            } else {
                count = count + 1;
            }
            counts.put(item, count);
        }
        Collections.sort(asu, new Comparator<String>() {
            @Override
            public int compare(final String left, final String right) {
                int result = counts.get(left).compareTo(counts.get(right));
                if (result == 0) {
                    result = left.compareTo(right);
                }
                return result;
            }
        });
        System.out.println(asu);
    }
}