Java Collection Class Learning (i)

Source: Internet
Author: User
Tags addall pear set set stub

1. Common methods of the list interface

The list interface inherits the collection interface, so it contains all the methods in the collection. The list interface also provides a number of common methods that are appropriate for you, because lists are types of listings.

1.1. Example code 1

Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.LinkedList;Importjava.util.List; Public classCollectiondemo { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString a= "A", b= "B", c= "C", d= "D", e= "E";//defines a string object to insert into the collectionList<string> list=NewLinkedlist<string> ();//Create a list collectionList.add (a);//adding elements to the collectionList.add (b);        List.add (d); Iterator<String> Firstiterator=list.iterator ();//to create an iterator for a collectionSystem.out.println ("******* elements in the pre-modified collection are: ********");//Output Information         while(Firstiterator.hasnext ()) {//iterating through the elements in the collectionSystem.out.print (Firstiterator.next () + "\ T"); } list.set (1, c);//modify an object with an index position of 1 to object CList.add (2,e);//Add object E to position 2 at index positionIterator<string> It=list.iterator ();//creates an iterator object after modifying the collection objectSystem.out.println (); System.out.println ("The elements in the ******** modified collection are: ********");  while(It.hasnext ()) {//Loop gets the elements in the collectionSystem.out.print (It.next () + "\ T"); } System.out.println ("\ n"); System.out.println ("Other common methods of ********list collection: ********"); List<String> list2=NewArraylist<string> ();//To Create a list collection ObjectList2.add (a);//object A has an index position of 0List2.add (e);//the index position of object E is 1List2.add (b);//Object B has an index position of 2List2.add (e);//the index position of object E is 3List2.add (c);//Object C has an index position of 4List2.add (e);//the index position of object E is 5List2.add (d);//the index position of object D is 6System.out.println (LIST2);//all elements in the output listSystem.out.println ("E" the first occurrence of the index position is: "+List2.indexof (e)); System.out.println ("E The last occurrence of the index position is:" +List2.lastindexof (e)); System.out.println ("C The first occurrence of the index position is:" +List2.indexof (c)); System.out.println ("C The last occurrence of the index position is:" +List2.lastindexof (c)); }}

The results of the execution are shown below:

The elements in the pre-modified collection are: ********a    B    d    ******** The elements in the modified collection are: ********a    C    E    D    other common methods of ********list collection: ********[A, E, B, E, C, E, d]e the first occurrence of the index position is:1E The last occurrence of the index position is:5  c The first occurrence of the index position is:4c The last occurrence of the index position is:4

2. Common methods for Set interfaces
The set set consists of the implementation classes of the set interface and the set interface. Use the AddAll () method to remove duplicate values. The sample code is as follows:

Importjava.util.ArrayList;ImportJava.util.HashSet;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Set; Public classCollectiondemo { Public Static voidMain (string[] args) {//TODO auto-generated Method StubList<string> list=NewArraylist<string> ();//Create a list collectionList.add ("Orange");//to add an object to the collectionList.add ("Apple"); List.add ("Pear"); List.add ("Banana"); List.add ("Apple"); List.add ("Pear"); Set<String> set=NewHashset<string> ();//To Create a list collection ObjectSet.addall (list);//to add a list collection to the Set collectionIterator<string> It=set.iterator ();//Create a set set iteratorSystem.out.println (the element in the ******** collection is: ******** ");  while(It.hasnext ()) {System.out.print (It.next () )+ "\ T"); }    }}

The execution results are shown below:

The elements in the collection are: ********orange    banana    apple    pear    

3. Common methods for map interfaces
The map interface provides an object that maps keys to values. A map cannot contain duplicate keys, and each key can be mapped to at most one value.

ImportJava.util.HashMap;ImportJava.util.Map; Public classMapdemo { Public Static voidMain (string[] args) {//TODO auto-generated Method StubMap<string,string> map=NewHashmap<string,string> ();//Create a Map collectionMap.put ("1", "pear");//to add an object to the collectionMap.put ("2", "Orange"); Map.put ("3", "banana");  for(inti=0;i<=3;i++) {System.out.println (The "+i+" element is: "+map.get (" "+i+"));//output The element value for the corresponding position        }    }}

The results of the execution are shown below:

The No. 0 element is: thenull 1th element is: Pear The 2nd element is: Orange 3rd element is: Banana

Java Collection Class Learning (i)

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.