Java Collection Basics

Source: Internet
Author: User
Tags addall

Java Collection Basics

Set concepts and functions

1. In real life, gathering many things together is a collection. Collection class in java: a tool, such as a container, stores any number of objects with common attributes.

2. Organize data within the class;

Simple and fast search for a large number of entries

Some collections provide a series of ordered elements, and can be quickly inserted or deleted in the middle of the sequence.

Some set interfaces provide ing relationships. You can use keywords to quickly find the corresponding unique object. This keyword can be of any type.

3 Collection (List, Queue, Set)

1) List and Queue: ordered and repeatable

2) Set: unordered

3) List (sequence) and Set (Set) are commonly used)

 

 

Three commonly used ArrayList HashSet HashMap

List interface, ordered and repeatable, can precisely control the insert position of each element, or delete an element at a certain position

ArrayList, an array sequence, is an important implementation of List. The underlying layer is an array implementation.

1) all objects stored in the collection will be converted to the object type. type conversion is required during retrieval.

2) add () add a value

Add ("location", "value") to the specified location (when the value of the location is greater than the current maximum value, an exception occurs (indexOutOfBoundsException ))

AddAll (Arrays. asList (array ));

Course [] course = {new Course ("2", "java"), new Course ("3", "english ")};

CoursesToSelsct. addAll (Arrays. asList (course ));

3) obtain the elements in the List

First obtain the List length, size ()

Then use get ();

  

Traverse List through iterator

The iterator does not have the storage function. It depends on a set and cannot exist independently. There is another way for each ()

Iterator it = coursesToSelect. Iterator ()

While (it. hashNext ())

{

Course cr = (course) it. next ();

}

 

For (Object obj: courseToSelect) // The object type is automatically converted

5) modify the Set ("index location", "new element") method of the element in the List.

6) remove () & removeAll () from the List ()

Get the corresponding element with get ("location") before deleting, and print the output

Remove (Course) courseToSelect. get ("location "))

   

RemoveAll (), where the arraysList. asList () method is used to convert an array to a set type.

7) Generic: adding a specific type of object to the set will check the type during compilation. You can directly specify the type to obtain the set element (no type conversion is required)

Public List <Course> course

Public TestGeneric (){

This. course = new ArrayList <Course> ();

}

Objects of its subtype can be stored in the generic type.

The specified type in the generic cannot use the basic type.

List <int> list = new ArrayList <int> () --> incorrect. int Is the basic type.

List <Intger> list = new ArrayList <Integer> ()

Set interface and its implementation class -- HashSet (there is no Set () method similar to List to modify data)

1) The get () method is not available in the set. You can only use the foreach and iterator methods to traverse each data and execute the two operations to obtain inconsistent results.

2) The set is unordered and cannot be repeated. A unique data object reference is saved, and a null value can be added.

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.