Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.TreeMap;
- public class SortList {
- public static void main(String[] args) {
- Scanner in = null;
- try {
- in = new Scanner(new File("List1.txt"));
- }
- catch (Exception ex) {
- ex.printStackTrace();
- }
- Map<String, Integer> items = new TreeMap<String, Integer>();
- while (in.hasNextLine()) {
- String key = in.nextLine().trim();
- if (!items.containsKey(key)) items.put(key, 0);
- items.put(key, items.get(key) + 1);
- }
- in.close();
- while (!items.isEmpty()) {
- int larger = Integer.MIN_VALUE;
- Entry<String, Integer> item = null;
- for (Entry<String, Integer> e: items.entrySet()) {
- if (larger < e.getValue()) {
- larger = e.getValue();
- item = e;
- }
- }
- System.out.println(item.getKey() + item.getValue());
- items.remove(item.getKey());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement