Javase Advanced Collection Class

Source: Internet
Author: User
Tags repetition

? The following is a summary of the collection classes in Java, over time will share Java network programming, multithreading and other content, welcome criticism.

1. A collection in Java is used to hold an object, that is, a collection is a collection of objects, objects are elements of a collection, and the Java API Collection class is located in the Java.util package.

2. Common collection classes:

Collection interface, list interface, set interface, map interface. interface classes in combination with generics

3. The difference between a set and an array is that the length of the collection is dynamically expanded, and the arrays can hold the base data type and the reference data type, and only the reference data type in the collection.

Collection hierarchies in 4.java

Interface:

Iterator

|-collection

| |-lis

|   | |-arraylist

|   | |-linkedlist

|   | |-vector

|       | |-stackt

| |-set

| |-hashset

| |-treeset

|-listiterator

Map

|-hashmap

|-hashtable

|-treemap

Collection interface:

1. A collection object can put different types of data, is the set interface and the list interface of the parent class, whether the order of the element is allowed to repeat depends on the specific implementation

2.Set is a collection, the elements are unordered and do not allow repetition, and the List element is ordered, allowing repetition.

The common methods in 3.Collection are as follows:

add element: Add (element:object): Boolean

remove element: Remove (element:object): Boolean

Number of elements: size (): int

Determine if null: IsEmpty (): Boolean

Determine if the element exists: contains (element:objects): Boolean

Iterator: Iterator (): iterator

Produces an array containing all the elements: object[] ToArray ();

List interface

add element: Add (index:int. Element:object)

remove element: Remove (Index:int): Object

Get element: Get (Index:int)

Set element: Set (Index:int, element:objects);

Gets the index of the element: IndexOf (element:object): int

Iterator: Listiterator (): Listiterator

Iterator Interface:

Iterator is called an iterator, which facilitates the traversal of the container elements, all the collection classes that implement the interface collection have the iterator () method, return an object that implements the iterator interface, and iterator can only be used in one direction in the traversal. The iterator interface is designed for traversal to remove and delete elements from the collection, but it is not possible to delete the elements in them

The main methods in iterator are:

Object next () points to the next element in the collection

Boolean Hasnext () to determine if there is a next element

Void Remove () removes the element from the collection and executes next () only once remove ();

Collections class:

The Collections class is a tool class similar to the Arrays class, which provides some static static methods for use by the collection class or for manipulating the collection class

Map interface:

The map stores the group of objects that appear in pairs such as key/value pairs, and the "key" object to find the "value" object, the "key" in the map is unique, and the key object is associated with the value object.

Common methods in the map interface:

Object put (object key, object value) adds a key-value pair

Object get (Object key) Gets the value corresponding to the key

Set KeySet () returns the Set view containing the key in this map

Comparison of HashMap and Hashtable:

Hashtable is thread-safe, that is, thread-synchronous, HashMap is thread insecure

HashMap allows a null to be used as a entry key or value two hashtable not allowed

How to select a collection class

The elements stored in the set are not allowed to be duplicated, are unordered, and the elements in the list are ordered and allowed to repeat.

The application of map is mainly using key/value pair for quick query.

The difference between ArrayList and LinkedList is that the performance of a random query is ArrayList better, but LinkedList is better at removing and adding the intermediate elements.

The main difference between HashSet and TreeSet is whether the elements within the collection are sorted

Use of generics

The role of generics is to limit the types in the collection to avoid casting when elements are removed

Combination of HashMap and set

1 package cn.sdut.lu; 2   3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Set; 6   7 public class Hashmapdemo { 8   9 public     static void Main (string[] args) {Ten  -         //hashmap instantiation of         HashMap HM = new HashMap ();         add an element to the collection (1,new person         ("AA"), and         hm.put ("VV", "Hm.put         "); Gets the key view of the         Set s = hm.keyset ();/         /using iterators to traverse the key view         Iterator I = S.iterator ();         Traversal key view gets key value of         while (I.hasnext ()) {             System.out.println (Hm.get (I.next ()));         }28     }29}

Javase Advanced Collection Class

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.