Simple use of Map,set in Java

Source: Internet
Author: User

 Packagetest2;ImportJava.util.*;Import Staticjava.lang.System.out; Public classTest2extendsstaticsuper{//put input, get, get    Static voidmaptest () {//TODO Auto-generated method stubshashmap<string, string> map =NewHashmap<string, string>(); Map.put ("1", "A1"); Map.put ("2", "A2"); Map.put ("3", "A3"); //the method of traversal one, disadvantage, time-consumingIterator<string> Iterator =Map.keyset (). iterator ();  while(Iterator.hasnext ()) {//Object key = iterator;Object key =Iterator.next (); System.out.print ("Key =" +key); System.out.println (", Value =" +Map.get (key)); //Iterator.next ();        }                //traversal method Two, time-savingIterator entries =Map.entryset (). iterator ();  while(Entries.hasnext ()) {Map.entry Entry=(Map.entry) entries.next (); Object Key=Entry.getkey (); Object value=Entry.getvalue (); System.out.println ("key =" + key + ", Value =" +value); } System.out.println ("Map end!"); }        Static voidsettest () {Set<String> Set1 =NewHashset<string>(); Set1.add ("S1_1"); Set1.add ("S1_2"); Set1.add ("S1_3"); Set1.add ("One");                System.out.println (SET1); Set<String> Set2 =NewHashset<string>(); Set2.add ("S2_1"); Set2.add ("S2_1"); Set2.add ("S2_3"); Set2.add ("One");                System.out.println (Set2); Set<String> sn =NewHashset<string>(SET1);        Sn.retainall (Set2);    SYSTEM.OUT.PRINTLN (SN); }         Public Static voidMain (string[] args) {}}

Simple use of Map,set in Java

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.