Two programs about containers

Source: Internet
Author: User

When two learning containers are attachedProgram, Easy to review.

1. Test the traversal of list, set, and map sets.

/*** Test the traversal operation of list, set, and map sets * when traversing containers in the future, the List should adopt index traversal as much as possible, and the set should adopt the iterator as much as possible **/package COM. bjsxt. test. collection; import Java. util. arraylist; import Java. util. collection; import Java. util. hashmap; import Java. util. hashset; import Java. util. iterator; import Java. util. list; import Java. util. map; import Java. util. map. entry; import Java. util. set; public class testiterate {public static void testlist () {list <student> List = new arraylist <student> (); list. add (new student ("James"); list. add (new student ("Li Si"); list. add (new student ("Wang Wu"); list. add (new student ("Zhao "); // use the index to traverse for (INT I = 0; I <list. size (); I ++) {student stu1 = List. get (I); system. out. println (stu1.getname ();} // use the enhanced for loop for (student STU: List) {system. out. println (Stu. getname ();} // use iterator. This method can iterate both list and setiterator <student> iter = List. iterator (); While (ITER. hasnext () {student stu2 = ITER. next (); // returns the current element and points the cursor to the next system. out. println (stu2.getname ();} // or use the following method for (iterator <student> iter2 = List. iterator (); iter2.hasnext ();) {student Stu = iter2.next (); system. out. println (Stu. getname () ;}} public static void testset () {set <student> set = new hashset <student> (); set. add (new student ("James"); set. add (new student ("Li Si"); set. add (new student ("Wang Wu"); set. add (new student ("Zhao "); // use the enhanced for loop for (student STU: Set) {system. out. println (Stu. getname ();} // use iterator (list or set can be iterated) iterator <student> iter = set. iterator (); While (ITER. hasnext () {student Stu = ITER. next (); system. out. println (Stu. getname () ;}for (iterator <student> iter2 = set. iterator (); iter2.hasnext ();) {student Stu = iter2.next (); system. out. println (Stu. getname () ;}} public static void testmap () {Map <string, student> map = new hashmap <string, student> (); map. put ("A", new student ("Zhang San"); map. put ("B", new student ("Li Si"); map. put ("C", new student (""); // traverses keyset <string> set1 = map. keyset (); // Why does iterator <string> iter1 = set1.iterator (); While (iter1.hasnext () {string key = iter1.next (); system. out. println (Key + "-----" + map. get (key ). getname ();} // traverse valuecollection <student> set2 = map. values (); iterator <student> iter2 = set2.iterator (); While (iter2.hasnext () {student v = iter2.next (); system. out. println (v. getname ();} // traverses the entry (key-Value Pair) set <entry <string, student> set3 = map. entryset (); iterator <entry <string, student> iter3 = set3.iterator (); While (iter3.hasnext () {entry <string, student> E = iter3.next (); system. out. println (E. getkey () + "-----" + E. getvalue () ;}} public static void main (string [] ARGs) {testlist (); testset (); testmap ();}}


2. Use Map to classify and count elements in a string

Import Java. util. hashmap; import Java. util. iterator; import Java. util. map; import Java. util. map. entry; import Java. util. set; import Java. util. stringtokenizer; public class testhashmap {public static void main (string [] ARGs) {string STR = "what a Woderful day A what"; Map <string, integer> map = new hashmap <string, integer> (); // construct the container stringtokenizer S = new stringtokenizer (STR); While (S. hasmoreelements () {s Tring strtemp = (string) S. nextelement (); // forcibly convert the object type to string type // store if (! Map. containskey (strtemp) {// empty bag map. put (strtemp, 1); // create a container and put it into the value} else {map. put (strtemp, map. get (strtemp) + 1) ;}/// method 1 // traverse the key in the map, iterator + keyset/get <recommended> set <string> key = map. keyset (); iterator <string> iter1 = key. iterator (); While (iter1.hasnext () {string str1 = iter1.next (); system. out. println (str1 + "--->" + map. get (str1);} // use enhanced for + keyset/get for (string keys: Key) {system. out. println (Key + "--->" + map. get (KEYS);} // method 2 // use entryset + foreach + iterator set <map. entry <string, integer> entry = map. entryset (); iterator <map. entry <string, integer> iter2 = entry. iterator (); While (iter2.hasnext () {map. entry <string, integer> en = iter2.next (); string key2 = en. getkey (); integer it1 = en. getvalue (); system. out. println (key2 + "--->" + map. get (key2);} // foreachfor (Entry <string, integer> entry2: entry) {string key3 = entry2.getkey (); integer it2 = entry2.getvalue (); system. out. println (key3 + "--->" + map. get (key3);}/* This is the element in the print string ** while (S. hasmoretokens () {system. out. println (S. nexttoken ());}*/}}
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.