[Java] to read the text output frequency up words

Now comes word frequency, it is certainly the number of key-value pairs to use to store the words and corresponding.

Here insert about the HashMap and TreeMap:

HashMap : Based on the hash table. Use HashMap key request to add the class defined hashCode () and the equals () [can override hashCode () and the equals ()], HashMap to optimize the use of space, you can tune the initial capacity and the load factor. Suitable for insertion in a Map, delete and locating elements.

(1) HashMap (): construct an empty hash map
(2) HashMap (Map m) : Construction of a hash map, and adding all the mapped images m
(3) HashMap (int initialCapacity) : Construction of a certain capacity have empty hash map
(4) HashMap (int initialCapacity, float loadFactor): Construction of empty has a specific capacity and load factor of the hash map

TreeMap : Based red-black tree. TreeMap no tuning options, because the tree is always in equilibrium. Suitable natural order or custom order traversal key (key).

(1) TreeMap (): construct an empty image of the tree
(2) TreeMap (Map m) : Construction of a treemap, and add the image m in all elements
(3) TreeMap (Comparator c) : Construction of a treemap, using specific sort keywords comparator
(4) treeMap (SortedMap s) : Construction of a treemap, add all of the mappings treemap s, s image and ordered using the same sort comparator

 

 

 

HashMap usually (data tree and hash table dictates), it is recommended to use HashMap, in the Map when you need to sort it faster than with TreeMap TreeMap point.

To make a word automatically in alphabetical order (looked pleasing to the eye), I used TreeMap, but TM custom Comparator can only be set on the key, it can not set collation value, because it is built on key tree, so used is: first turn into a collection List, which put the TM Entry key right, right to sort this collection of writing a Comparator method.

 1 import java.io.BufferedReader;
 2 import java.io.File;
 3 import java.io.FileReader;
 4 import java.io.IOException;
 5 import java.util.ArrayList;
 6 import java.util.Collections;
 7 import java.util.Comparator;
 8 import java.util.List;
 9 import java.util.Map;
10 import java.util.TreeMap;
11 
12 public class texMain {
13     public static void main(String[] args) throws IOException {
14         File file=new File("E:/test.txt");
15         BufferedReader bReader= new BufferedReader(new FileReader(file));
16      
17         TreeMap<String, Integer> map=new TreeMap<String, Integer>();
18         String line=null;
19         while((line=bReader.readLine())!=null) {
20             String[] strs=line.trim().split("[^A-Za-z]");
21             for(String s:strs) {
22                 if(s.trim().length()==0)continue;
23                 if(!map.containsKey(s))
24                     map.put(s,1);
25                 else {
26                     map.put(s, map.get(s)+1);
27                 }
28             }
29         }
30         bReader.close();
31         List<Map.Entry<String,Integer>> list=new ArrayList<>(map.entrySet());
32         Collections.sort(list,new Comparator<Map.Entry<String, Integer>>(){
33             @Override
34             public int compare(java.util.Map.Entry<String, Integer> o1, java.util.Map.Entry<String, Integer> o2) {
35                 
36                 return o2.getValue().compareTo(o1.getValue());
37             }
38         });
39         for(Map.Entry<String, Integer> m:list) {
40             System.out.println(m.getKey()+" "+m.getValue());
41         }
42         System.out.println(list.get(0).getKey()+" "+list.get(0).getValue());
43     }
44 }

 

PS: HashMap load factor: when the load factor is large, the possibility of expansion to go to the table of the array will be less, the relative take up less memory (less space), but the elements on each strand would be relatively entry and more time for the query will grow (more time). On the other hand is, when the load factor is less possibility to table an array of expansion will be high, so the more memory space occupied, but the chain elements on the entry will be relatively small, isolated time will be reduced. So only the load factor is a compromise on the time and space to say. So set the load factor to consider when seeking for a little over time or space.

Guess you like

Origin www.cnblogs.com/zhangmora/p/12652733.html
Recommended