Java Container Classes

Source: Internet
Author: User

Container class: A variety of data structures that can be used directly by Java in the form of a class library for users to develop programs.

There are two types of container interfaces: List interface Set set interface

List of interface lists

Property: List is a collection sub-interface, which is a linear table containing ordered elements, in which the elements must be stored sequentially , repeatable, and nullable

There are two types of lists: List class LinkedList and array listing class ArrayList

Create two kinds of table objects (you need to introduce java.util.* packages first)

1 linkedlist<?> ld=new linkedlist<?> ();    // Linked list 2 arraylist<?> al=new arraylist<?> ();        // Array List

Two, set interface set

Set property: does not contain duplicate elements , but also inherits from the collection interface. The objects in the Set collection are not sorted in a particular way , but simply add the object to the collection. The order of the elements in the collection is independent of the order in which the elements are added to the collection.

There are two main classes for implementing a set interface: HashSet Hash Collection TreeSet Collection

HashSet: Accesses the elements in the collection according to the hash code. A hash set establishes a specific correspondence between where the element is stored and the value of the element's K, so that each element corresponds to a unique storage location. (Can be simply imagined as a function y=f (k))

Treeset:treeset is similar to HashSet, but the elements in the TreeSet collection are always in an orderly state.

Simple use of Set

1 ImportJava.util.*;2  Public classTreesetclass {3      Public Static voidMain (string[] args) {4Set<string> hs=NewHashset<string> ();//creating a Hash Collection Object HS5Hs.add ("Tang priest"));6Hs.add ("Monkey King"));7Hs.add ("Pig");8Hs.add ("The Sand Monk");9Hs.add ("White Dragon Horse"));
Creates a TreeSet object with an element in the HS object
as a parameter of the HashSet object
TenTreeset<string> ts=NewTreeset<string> (HS);//Create a Tree collection OneSystem.out.println ("Tree collection:" +ts);//Output Tree Collection ASystem.out.println ("The first element of a tree collection:" +Ts.first ()); -System.out.println ("The last element of the tree collection:" +ts.last ()); -System.out.println ("HeadSet (Monkey King) elements:" +ts.headset ("Monkey King"))); theSystem.out.println ("Tailset (Monkey King) element" +ts.tailset ("Monkey King"))); -System.out.println ("Ceilset (Sand) Element:" +ts.ceiling ("sand")); - } -}

Three, iterators

Iterator iterator: Similar to traversal, accesses the elements in the container and accesses them only once.

There are three methods of iterator:

Hasnext (): Determines whether a subsequent element is included and returns True

Next (): Return to subsequent elements

Remove (): Removes the element that the iterator is currently pointing to

Iterator has a subclass interface Listiteraotr,listiterator can only be used for list and its subclasses.

Create an iterator:

1 import java.util.*; 2 arraylist<integer> al=new arraylist<integer>();//create array list 3 Iterator<integer> iterator=al.iterator ();     // iterator returning to Al

Iv. Mapping Interface Map

Map is another object that stores data structures.

Map properties: The elements in the map appear paired, providing a mapping of the key to the value (value). (Map does not inherit collection) interface

There are two types of maps: HashMap and TreeMap

Like sets, HashSet are unordered, treemap are orderly.

General Methods of Map:

Put (K key,v value)

Putall (map<? extends K,? extends V) m) add all elements of M to the Map object

Get (k key)//returns the value mapped by K, no return null

Set<k> KeySet ()//returns the set set of all key objects in the collection

V Remove (Object key)//Remove the entry for keys key

Java Container Classes

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.