"Java Basics" 04_ collection framework

Source: Internet
Author: User
Tags addall

First,the diagram of the class is assembled in Java "lite"

Note:

Dashed rectangles represent interfaces

Solid-line rectangles represent concrete implementation classes

Solid rectangular bold means that the specific implementation of the class used high frequency, as the key to grasp

A collection is a container for storing multiple elements, but because of the different data structures, Java provides a variety of collection classes.

And this kind of collection class has the common function, therefore, through the continuous upward extraction, finally formed the collection architecture.

Similarities and differences of sets and arrays ( face test )1.same Point

They are all containers and can store data.

2.difference

  Length Difference

Fixed array: The length is fixed when the array object is created

Set Variable: The set can vary in length as the number of stored elements changes

storage element Type differences

Arrays can store basic data types, or they can store reference data types.

Collections can only store reference data types.

whether to store the same type difference

Array storage element types must be identical.

The collection element types can be inconsistent. You can restrict the storage element type of a collection by generics

third, single-column collection succession system

Collection ( single-column collection )

|--list ( element access is ordered , indexed , can be duplicated )

| |--arraylist

|   | The underlying is an array implementation , thread insecure, find and modify Fast , Add and delete slow

| |--linkedlist

|   | The underlying is linked list implementation , thread insecure, add and delete faster , find and modify the relatively slow

| |--vector

|   | The bottom is the array implementation , thread safety , regardless of additions and deletions are slow

| |-- Usage Scenarios

| |-- If you find and modify many , with ArrayList

| |-- If you increase and delete more , use LinkedList

| |-- If you have more , use ArrayList

|

|--set ( element access unordered , no index , can not be duplicated )

|--hashset

|    | the bottom layer is the hash algorithm implementation

| |--linkedhashset

| The bottom line is the chain list implementation , which guarantees the element access order , but also can guarantee the element unique , and the same HashSet principle

|--treeset

| the bottom layer is the implementation of two-fork tree algorithm

|-- Use

generally in the development time does not need to the storage element sorts , mostly uses the Hashset,hashset the efficiency is relatively high

four, double row set inheritance system

Map ( double-column collection )

| -- HashMap

| | The underlying is a hashing algorithm that is valid for keys

| | -- Linkedhashmap

| The underlying is a linked list , valid for keys

| -- TreeMap

| the underlying is a two-fork tree algorithm , which is valid for keys

| --Usage scenarios

used in development HashMap more effective

study methods of inheriting system

Learning Top level: because the top layer defines the common content. Example: Water Cup

Use the underlying: because the underlying is the specific implementation. Example: Square water Cup. Oval Water Cup

common use steps for collections (four-step walk)

A: Create A Collection Object

B: Create an Element object

C: Adding an Element object to the collection object

D: Traversing an Element object in a collection object

1. Gets the iterator object through the collection object.

2. Use the Hasnext () methodof the iterator object to determine if there are elements.

3. The next () methodof the iterator object Gets the element and moves to the next element.

      gets the return value of " Object ", a method that uses a subclass object to be transformed downward

Liu, java.util.collection<e>Interface.Common Methods

1add function (Master)
    1. Boolean Add (Object obj):

      Adds an element to the collection.

2.boolean AddAll (Collection c):

Adds all the elements of the incoming collection to the collection.

2Delete function (master)

    1. void Clear ():

      Empties all the elements in the collection.

    2. Boolean remove (Object obj):

      Removes the first occurrence of the specified element object in the collection.

3. Boolean removeall (Collection C):

Deletes the specified collection element from the collection. Corresponds to the intersection.

returns True whenever a "one" element is removed .

3Judging function (master)

    1. Boolean IsEmpty ():

      Determines whether the collection is empty.

    2. Boolean contains (Object obj):

      Determines whether the collection contains the specified element.

3. Boolean containsall (Collection C):

Determines whether the collection contains the "All" element in the specified collection.

Contrast Memory :

String 's judgment function also has isEmpty () and contains ()

4traversal function (Master)

Iterator Iterator (): gets the iterator. The private traversal of the Collection class collection.

Iteratormethods in the interface
      1. Boolean Hasnext ():

        To determine if there is another element, return True

      2. Object Next ():

        return value is Object Gets the element ,

        do not use it.next ()

        may be reported: nosuchelementexception There is no such element exception.

        When you get to the end of the element, you have to get, there is no element, so error.

      3. void Remove ():

        from Iterators point to Collection remove iteration to current element

        Each call to next ()

5length function (master)

int size ():

Gets the number of elements of the collection.

Contrast Memory:

Array: Property Array.Length

String and stringbuffer: Method Length ();

6intersection function (learn)

Boolean retainall (Collection C):

Determines whether the same element is in the collection. Take the meaning of the intersection.

original set change, return true

when the collection of objects is a subset of the parameter collection, false is returned because the original collection object has not changed.

Thinking:

Boolean retainall (Collection C):

What are the elements of two sets ? where does the thought element go, and what does the Boolean return mean ?

Suppose there are two sets a,B.

A to B intersection, the final result is saved in A ,b unchanged .

The return value indicates whether a has changed.

7conversion function (Master)

Object[] ToArray ():

turns the collection into an array . A way to traverse a collection.

Note return to an Object array that needs to be transformed downward, for example:string s = (string) objs[x];

How the array turns into a collection:

Java.util. Arrays;

public static <T> list<t> aslist (T ... a) returns a set of fixed-length sizes supported by the specified array.

Seven,Java.util.list<e>Interface(unique methods related to indexes)

Public interface List<e> extends collection<e>

an ordered collection(also known as a sequence).

Users of this interface can precisely control the insertion position of each element in the list.

The user can access the element based on the integer index of the element (the position in the list).

and search for the elements in the list.

Unlike set , a list usually allows repeating elements.

1 Listthe characteristics

1. ordered ( same order of storage and extraction )

2. elements can be duplicated

3. you can manipulate the element of the corresponding position by the index value

2Add features

    1. void Add (int index,object obj):

      Inserts the specified element at the specified index position

      Attention:

      The Add method in Collection returns a value of Boolean

      Indexoutofboundsexception- If the index is out of range (Index < 0 | | index > Size ())

      ClassCastException- if the class of the specified element does not allow it to be added to this list

2. Boolean addall (int index, Collection c)

Adds a stack of elements to a collection at the specified index bit

3Remove Features

Object Remove (int index):

Deletes the element at the specified index, returning the deleted element

4Modifying features

Object set (int index,object obj):

Replaces the element at the specified index position with the specified element, returning the original element

5Get features
    1. Object get (int index):

      Gets the element at the specified index position

      Special usage:

      combined with the normal for loop and the size () method, you can traverse the List collection or its subclasses

    2. int indexOf (Object obj):

      Gets the index position of the first occurrence of the specified element

    3. int lastIndexOf (Object o)

      Gets the index of the last occurrence of the specified element in the collection

4. Listiterator Listiterator ():

Get list iterator

You can iterate through the collection and make modifications and additions to the collection elements during the traversal.

6interception function

list<e> sublist (int fromIndex, int toindex)

Returns a subset, starting at the specified index (including), ending at the specified index (not included)

7Summary

List Collection Unique traversal mode

normal for loop +int size () method +object get (int index) method + down transformation

can traverse List collection and its subclasses, which can be manipulated using the collection object to manipulate the element object

Summary

the unique methods of the List interface are related to index indexes

Note: The return value of the method is the majority of Object

"Java Basics" 04_ collection framework

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.