Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedWriter;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.nio.file.StandardOpenOption;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.TreeMap;
- public class MapExample {
- public static void main(String[] args) {
- Scanner reader = null;
- try {
- reader = new Scanner(Paths.get("C:\\List1.txt"));
- }
- catch (Exception e) {
- }
- BufferedWriter writer = null;
- try {
- writer =
- Files.newBufferedWriter(Paths.get("C:\\List2.txt"),
- Charset.forName("UTF-8"), StandardOpenOption.WRITE);
- }
- catch (Exception e) {
- }
- if (reader == null || writer == null) System.exit(1);
- Map<String, Integer> map = new TreeMap<String, Integer>();
- while (reader.hasNextLine()) {
- String name = reader.nextLine().trim();
- if (map.get(name) == null) map.put(name, 0);
- else map.put(name, map.get(name) + 1);
- }
- reader.close();
- while (!map.isEmpty()) {
- Entry<String, Integer> max = map.entrySet().iterator().next();
- for (Entry<String, Integer> e: map.entrySet())
- if (max.getValue() < e.getValue()) max = e;
- try {
- writer.write(max.getKey() + "\n");
- }
- catch (Exception e) {
- }
- map.remove(max);
- }
- try {
- writer.close();
- }
- catch (Exception e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement