Read file content and save to a TreeMap : TreeMap « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Collections Data Structure » TreeMapScreenshots 
Read file content and save to a TreeMap
 

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class LogTest {
  public static void main(String[] argsthrows IOException {
    String inputfile = args[0];
    String outputfile = args[1];

    Map<String, Integer> map = new TreeMap<String, Integer>();

    Scanner scanner = new Scanner(new File(inputfile));
    while (scanner.hasNext()) {
      String word = scanner.next();
      Integer count = map.get(word);
      count = (count == null : count + 1);
      map.put(word, count);
    }
    scanner.close();

    List<String> keys = new ArrayList<String>(map.keySet());
    Collections.sortkeys );

    PrintWriter out = new PrintWriter(new FileWriter(outputfile));
    for (String key : keys)
      out.println(key + " : " + map.get(key));
    out.close();
  }
}

   
  
Related examples in the same category
1.TreeMap
2.Get Synchronized Map from Java TreeMap
3.Check if a particular key exists in Java TreeMap
4.Get Set view of Keys from Java TreeMap
5.Get Tail Map from Java TreeMap
6.Get Size of Java TreeMap
7.Remove value from Java TreeMap
8.Iterate through the values of Java TreeMap
9.Check if a particular value exists in Java TreeMap
10.Remove all values from Java TreeMap
11.Get Sub Map from Java TreeMap
12.Get lowest and highest key stored in Java TreeMap
13.Get Head Map from Java TreeMap
14.Use Generic TreeMap to store Integer as key and String as value
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.