Java class set framework simple comprehension of List, set, Iterator, map

Source: Internet
Author: User

Java class set framework simple comprehension of List, set, Iterator, map

1. Java class set framework body structure:

(1). Collection (SET): objects are not sorted by a particular time, and there are no duplicate objects.
(2). List: objects are sorted by index location and can have duplicate objects.
(3). Map: Each element contains a key object and a value object, the key cannot be duplicated, and the value can be repeated.

2. The collection interface provides the following methods:

3. The code is as follows:

ImportJava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.HashSet;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Set; Public  class testdemo {     Public Static void Main(string[] args)        {Listdemo ();        Setdemo ();        Iteratordemo ();    Mapdemo (); }/** * Map test: Key cannot be duplicated, value can be repeated, in the form of a key-value pair */    Private Static void Mapdemo() {map<string, string> Map =NewHashmap<string, string> (); Map.put ("1","a"); Map.put ("2","B"); Map.put ("3","C"); Map.put ("4","D"); Map.put ("5","a"); System.out.println ("---map-----"+ map); }/** * can have duplicate elements and are ordered * /     Public Static void Listdemo() {//Create the objectArraylist<string> Listdeno =NewArraylist<string> ();/ * * Add the data * *Listdeno.add ("1"); Listdeno.add ("2"); Listdeno.add ("3"); Listdeno.add ("4");//can add repeating elementsListdeno.add ("1"); Listdeno.add ("4"); System.out.println ("---list---"+ Listdeno);//---list---[1, 2, 3, 4, 1, 4]}/** * cannot have duplicate elements and is unordered */     Public Static void Setdemo() {//Create the objectSet<string> Setdemo =NewHashset<string> ();/ * * Add the data * *Setdemo.add ("a"); Setdemo.add ("B"); Setdemo.add ("C"); Setdemo.add ("D");//can add repeating elementsSetdemo.add ("a"); Setdemo.add ("D"); System.out.println ("---setdemo---"+ Setdemo);//---setdemo---[A, B, C, d]} Public Static void Iteratordemo() {//Create the objectSet<string> Setdemo =NewHashset<string> ();/ * * Add the data * *Setdemo.add ("a"); Setdemo.add ("B"); Setdemo.add ("C"); Setdemo.add ("D");//can add repeating elementsSetdemo.add ("a"); Setdemo.add ("D"); System.out.println ("---setdemo---"+ Setdemo);//---setdemo---[A, B, C, d]        //iterator used with collectionsiterator<string> Iterator = Setdemo.iterator (); while(Iterator.hasnext ())            {String element = (string) iterator.next (); System.out.println ("---element----"+ Element);//---element----a            //---element----b            //---element----c            //---element----d}    }}

Java class set framework simple comprehension of List, set, Iterator, 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.