About the use of list, set set, and map

Source: Internet
Author: User

 PackageTingjizifu;ImportJava.util.*; Public classTongji {/** Use scanner to read a string from the console, count the number of occurrences of each character in the string, and require that you use the learned knowledge to complete the above requirements * implementation ideas based on the set, List, map set characteristics. */     Public Static voidMain (string[] args) {//Input StringScanner input =NewScanner (system.in); String Shuru=Input.next (); //to load a string character into the list collectionlist<string> list =NewArraylist<string>();  for(inti = 0; I < shuru.length (); i++) {List.add (string.valueof (Shuru.charat (i))); }        //to load a string character into a set setSet<string> set =NewHashset<string>();  for(inti = 0; I < shuru.length (); i++) {Set.add (string.valueof (Shuru.charat (i))); }        //The characters in set are compared with the characters in the list, and the same is added 1 to count.//The character in the set set is then used as the key (key), the number of statistics as the value (value), and the data in the map is finally printed.map<string, integer> map =NewHashmap<string, integer>();  for(String str:set) {intsum = 0; //System.out.println (str);             for(inti = 0; I < list.size (); i++) {                if(List.get (i). Equals (str)) {sum++;        }} map.put (str, sum); } Set<String> SS =Map.keyset ();  for(String str:ss) {System.out.println (str+ "occurrences" + map.get (str) + "Times");    } input.close (); }}

About the use of list, set set, and map

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.