Java Collection Framework

Source: Internet
Author: User

Java Collection Framework

Collection interface: Stores a set of non-unique, unordered objects

The list interface stores a set of objects that are not unique, ordered (in order of insertion)

The set interface stores a set of unique, unordered objects.

List

In the example, the dog object is first created and assigned by set.

// Create a Dog object        Dog dog1=new  Dog ();        Dog1.setname ("Grass 1");        Dog1.setstrain ("Labrador");        Dog Dog2=new  Dog ();        Dog2.setname ("Floret 2");        Dog2.setstrain ("Teddy");

Then create the collection object from the list:

List<dog> list=new arraylist<dog> ();
List.add (DOG1);
List.add (DOG2);

The List.size () method can determine the number of actual elements, assign values to a variable and output:

int num=list.size (); SYSTEM.OUT.PRINTLN (num);

You can move an element in an array by List.remove (); The parentheses can be an element subscript or an element object value.

There are 3 ways to output elements in a collection, namely:

1. Iterating through the elements in the output collection with a For loop

2. Use foreach to enhance the elements in the For Loop output collection

3. Iterate through the elements in the collection using iterators

The first is the For loop:

 for  (int  i = 0; i < list.size (); I ++) { //  remove            Each element in the collection  Object object=list.get (i);  //  Convert to real subclass object (DOG)  if  (object instanceof   Dog) {  //  coercion type conversion  Dog Dog= (Dog) object;              //  output dog information              System.out.println (dog); }        }

The normal for loop traversal of the collection needs to be noted that before the loop output, the instanceof keyword needs to be converted to the real subclass object, and then the type conversion is forced to output the dog's information.

foreach Loop

The Foreach loop is always convenient, and it is not to be overlooked that the instanceof keyword is still needed for judgment.

         for (Object object:list) {            ifinstanceof  Dog) {            // coercion type conversion            Dog dog=(dog) object;             // information about the output dog             System.out.println (dog);            }        }

Use of iterators:

Using iterator, you first determine whether there are elements in the collection, and then you get each value in the collection.

        Iterator its=list.iterator ();          while (Its.hasnext ()) {            = its.next ();            System.out.println (obj);        }

The final output of the program is:

Dog's name        dog breed Small class 3        Husky grass 1        Labrador

LinkedList Collection Operations

LinkedList list=New  LinkedList ();        List.add (STAFF1);        List.add (STAFF2);
List.addfirst (1000); // add to the first position of the list List.removefirst (); // Remove the first list.removelast (); // remove the last list.push ("222"); // add Element list.poll (); // gets and removes the first

ArrayList implements a variable-length array that allocates contiguous space in memory. The efficiency of traversing elements and random access elements is high.

Special methods for LinkedList:

void AddFirst (Object o) adds an element to the header of the list

void AddLast (Object o) adds an element at the end of the list

Object GetFirst () returns the first element in a list

Object GetLast () returns the last element in the list

Object Removefirst () deletes and returns the first element in a list

Object removelast () deletes and returns the last element in the list

When are ArrayList and LinkedList used separately?

ArrayList:

High efficiency for traversing elements and random access elements

Low performance when inserting, deleting, etc. frequently

LinkedList

High efficiency when inserting and deleting elements

Less efficient to find

Example of a collection generic:

                // require data  to be placed in int type Integer        list<integer> list=new arraylist<integer> ();    

ValueOf method to convert a number to a string

        List1.add (string.valueof (1000));

Generics require data to be added and must be of the specified data type.

Java Collection Framework

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.