Java Fundamentals 22-Collection Class 1 (overview, List interface)

Source: Internet
Author: User

I. Overview of collection classes

Before Java2 (jdk1.2), Java was not a complete collection framework. It only has some simple and extensible container classes, such as the Vector,stack class.

We know that arrays can store multiple data, and since arrays can store multiple data, why do we need a collection class?

Let's take a look at the drawbacks of arrays:

1. The length is immutable, once the array is initialized, the length is fixed.

2. There is a need to store multiple data in n places, it is necessary to write the operation method of the array, so it has not embodied the dry principle and high maintenance cost.

3. Even if everyone is using an array class, the class name and method name defined by different people are different, and the implementation details are uneven.

So Sun has defined its own container class, and every developer can invoke it.

(1) What is a set frame?

The collection class is stored in the Java.util package.

The collection class holds the object's reference, not the object itself, and for the convenience of expression, we call the object in the collection to refer to the object in the collection.

A collection framework is a uniform standard architecture that is defined for representing and manipulating collections. Any set frame contains three chunks of content: external interfaces, interface implementations, and algorithms for set operations.

(2) Why do I need a set frame?

1. Reuse of functions
2. Focus on business development, not data structures and algorithms (underlying data structures and algorithms)

(3) Common collection class

Set (SET): Objects in the collection are not sorted in a particular way, and elements are not allowed to repeat
List: Objects in the collection, sorted by index position, allowing elements to be duplicated
Map: Each element in the collection contains a pair of key and value objects, does not allow the key object to be duplicated, and the value object can be repeated

There are two main branches: collection and map

Second, List collection

1. List interface Features:

① order, the element access is orderly, so it is an indexed collection, through the index can precisely manipulate the elements in the collection.

List allows storage of an item's value to be empty, and also allows storage items that store equal values

② repeatability, duplicate elements can exist.

Common methods in 2.List interfaces

(1) ArrayList class

The structure of the ArrayList collection data store is the array structure.

It is characterized by: since the data is stored in an array, the elements are slow to delete, and the lookup is fast.

 list<string> List = new  arraylist<string> ();  // 1, adding elements.  list.add ("Little Red"  "Mei"  "Xiao Qiang"  // 2, insert element. The collection before inserting the element ["Little Red", "Xiao Mei", "Xiao Qiang"]  list.add (1, "Lao Wang"); //  The collection after inserting elements ["Little Red", "Lao Wang", "Xiao Mei", "Xiao Qiang"]  // 3, delete the element.  List.remove (2); //  delete element after collection ["Little Red", "Lao Wang", "Xiao Qiang"]  // 4, modify the element.  List.set (1, "The Old King Next Door"); //  The collection after modifying the element ["Little Red", "Old King next Door", "Xiao Qiang"]  

(ii) LinkedList

The structure of the LinkedList collection data store is the linked list structure . is a doubly linked list

1. Features are: a collection of convenient elements to add and delete

2. Unique methods are:

New Linkedlist<string>();         // adding elements        Link.addfirst ("ABC1");        Link.addfirst ("ABC2");        Link.addfirst ("ABC3");         // get Element         System.out.println (Link.getfirst ());        System.out.println (Link.getlast ());         // Delete Element         System.out.println (Link.removefirst ());        System.out.println (Link.removelast ());

Java Fundamentals 22-Collection Class 1 (overview, List interface)

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.