Java Basics HASHMAP Statistics CSV file words

Source: Internet
Author: User
Tags set set

A: Knowledge supplement (this HashMap map and C + + map is still a big difference, the difference between the big people jaw-dropping, of course, the role of the two is consistent, but the function name is very large, even iterator difference is very large)

(1) difference between HashMap and Hashtable (only map wood in C + + is HashMap)

HashMap is not thread-safe
Hastmap is an interface that is a sub-interface of the map interface, an object that maps keys to values, where the keys and values are objects and cannot contain duplicate keys, but can contain duplicate values. HashMap allows null key and null value, while Hashtable is not allowed.

Hashtable is a thread-safe collection.

(2) Precautions for HashMap

HashMap the bottom layer maintains an array, and the objects we place in the HashMap are actually stored in the array;
When you put a pair of key values into HashMap, it calculates a position based on the hashcode value of the key, which is where the object is ready to be stored in the array.
HashMap Application Example: console input a sentence of English, simple statistics of the number of occurrences of each word

(3) iterator differs from C + +: The first step: Get the set of key values set<string> set = Map.keyset (); Step two: Use Iterator to traverse set Set iterator<string> Iterator = Set.iterator;  Iterator.hashnext ();   Iterator.next (); Step three: Put/get (key) evaluates the value Map.containskey (key) (whether it already contains this keyword), map.put (key,value) is stored in the key value pair, and Map.get (key) returns the value of the key value.

(4) in this, once again revealed oneself of a humble act: Scanner class oneself is the first time use, why say the humble count? Because I think it is relatively early to learn Java learning Java AWT, Swing, followed by Javawe JSP Servelet and so on, from the first Java applet simple calculation formula of the compiler start to Gobang; then to the first only JSP + Tomcat's Javaweb project is currently used in the spring RESTful Javaweb project. Even a scanner class or Java console input and output has not been studied, of course, the source code is not in-depth.

(5) in short, once again in the irony here, inspire themselves, but also give Yimeimei a piece of advice! Low-key person to do high-profile work: must study the source code level, must put the foundation to fight solid.

Two: console input statistics words

Package Edu.tju.cs;import Java.util.hashmap;import Java.util.iterator;import java.util.map;import Java.util.Scanner Import Java.util.set;public class Hashmap3dimensions {public void Mprint (string sentence) {string regex = ""; string[] Words = sentence.split (regex); map<string,integer> map = new hashmap<string,integer> (), int i;for (i=0;i<words.length;i++) {if ( Map.containskey (Words[i])) {//description map, presence of the element int num = Map.get (words[i]); Map.put (Words[i], ++num);} else{//first time Keymap.put (Words[i], 1);}} System.out.println ("Count the number of words appearing, the result is as follows:"); set<string> set = Map.keyset (); for (iterator<string> Iterator = Set.iterator (); Iterator.hasnext ();) { String key = Iterator.next (); Integer value = Map.get (key); SYSTEM.OUT.PRINTLN (key + ":" +value);}} Main entry public static void main (string[] args) {Scanner sc = new Scanner (system.in); System.out.println ("Please enter a sentence for Word statistics ():"); String sentence = Sc.nextline (); Hashmap3dimensions HM = new Hashmap3dimensions (); Hm.mprint (sentence);}}


Three: The content of the statistics CSV file

Package Edu.tju.cs;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.File;import Java.io.fileinputstream;import Java.io.filewriter;import Java.io.inputstreamreader;import Java.util.HashMap; Import Java.util.iterator;import java.util.map;import Java.util.scanner;import Java.util.set;public class hashmap3dimensions {private map<string, integer> originalmap = new hashmap<string, integer> ();p rivate Map <string, integer> destinationmap = new hashmap<string, integer> ();p rivate map<string, Integer> O_DMap = new hashmap<string, integer> (); static int original = 1; Original the subscript static int destination = 5; Destination the subscript static String Regre = ",";            The split function separates the matching characters public void Mprint (string filePath, String tofilepath) {try {string encoding= "GBK";            File File=new file (FilePath);            int cp = 1; if (File.isfile () && file.exists ()) {//Determine if the file exists inputstreamreader read = new InputSTreamreader (new FileInputStream (file), encoding);//considering the encoding format BufferedReader BufferedReader = n                                EW BufferedReader (read);            Write file name processing String filename = Tofilepath;            BufferedWriter writer = new BufferedWriter (new FileWriter (FileName));                The original row of data and whether the data needs to change the symbol String originalline = null;                while ((Originalline = Bufferedreader.readline ()) = null) {//string delimited int i = 1;                String tmp[] = Originalline.split (Regre); Count the degrees and degrees of each site if (tmp.length>5) {//original node if (Originalmap.contai                Nskey (Tmp[original])) {int num = Originalmap.get (tmp[original]);                Originalmap.put (tmp[original], ++num);                }else{Originalmap.put (tmp[original], 1); }//Destination node if (DestinAtionmap.containskey (Tmp[destination])) {int num = Destinationmap.get (tmp[destination]);                Destinationmap.put (Tmp[destination], ++num);                }else{Destinationmap.put (Tmp[destination], 1);                }//o_d node String od = tmp[original] + "," + tmp[destination];                if (O_dmap.containskey (OD)) {int num = o_dmap.get (OD);                O_dmap.put (OD, ++num);                }else{o_dmap.put (OD, 1);                }}}//Close Write file Writer.close ();    Read.close ();    } else {System.out.println ("The specified file cannot be found"); }} catch (Exception e) {System.out.println ("Readtowrite ...        Error reading file contents ");    E.printstacktrace (); }//system.out.println ("Count the number of words appearing, the results are as follows:");//set<string> Set = Map.keyset ();//for (iterator<string> Iterator= Set.iterator (); Iterator.hasnext ();) {//string key = Iterator.next ();//integer value = Map.get (key);// SYSTEM.OUT.PRINTLN (key + ":" +value);////}}//main entry public static void main (string[] args) {//Source address and destination address String file        Path = "D:\\tjdata_metro\\token_enex_201404_20w.csv"; String Tofilepath = "d:\\tjdata_metro\\ new folder \ \"; Hashmap3dimensions HM = new Hashmap3dimensions (); Hm.mprint (Filepath,tofilepath);}}


Four: Input and output Summary of Java console



Java Basics HASHMAP Statistics CSV file words

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.