Java Basics: Util package of commonly used data structure introduction

Source: Internet
Author: User
Tags constructor empty int size

Linear table, linked list, hash table is a commonly used data structure, in the Java development, JDK has provided us with a series of corresponding classes to achieve the basic data structure.

These classes are in the Java.util package. This paper attempts to explain the roles of the classes and how to use them correctly by a simple description.

Collection
    ├List
    │├LinkedList
    │├ArrayList
    │└Vector
    │ └Stack
    └Set
   Map
    ├Hashtable
    ├HashMap
    └WeakHashMap

Collection interface

Collection is the most basic set interface, and a collection represents a set of object, the collection element (Elements). Some collection allow the same elements while others do not. Some can be sorted and others not. The Java SDK does not provide classes that directly inherit from collection, and the Java SDK provides classes that inherit from collection such as list and set.

All classes that implement the collection interface must provide two standard constructors: parameterless constructors are used to create an empty collection, and a constructor for a collection parameter is used to create a new collection. This new collection has the same element as the incoming collection. The latter constructor allows the user to replicate a collection.

How do I traverse every element in the collection? Regardless of the actual type of collection, it supports a iterator () method that returns an iteration that can be used to access each element of the collection one at a time. Typical usage is as follows:

Iterator it = collection.iterator(); // 获得一个迭代子
     while(it.hasNext()) {
       Object obj = it.next(); // 得到下一个元素
     }

The two interfaces that are derived from the collection interface are list and set.

Main methods:

Boolean Add (Object o) Adding objects to the collection

Boolean remove (object o) deletes the specified object

int size () returns the number of elements in the current collection

Boolean contains (object O) to find if the specified object is in the collection

Boolean IsEmpty () to determine whether the collection is empty

Iterator iterator () returns an iterator

Boolean containsall (Collection c) to find out if the collection has elements in set C

Boolean AddAll (Collection c) adds all the elements in the collection C to the collection

void Clear () deletes all elements in the collection

void RemoveAll (Collection c) removes elements from the collection that are also in the C collection

void Retainall (Collection c) Removes elements that are not contained in collection C from the collection

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.