Universal Collection Library

Source: Internet
Author: User
Tags arrays hash

4.4 Universal Collection Library

1.The Collections API

. A collection is an object that contains a set of object objects.
. The Collection API contains 3 interfaces, which organize the object into:
. Collection: A set of objects, with no special order, that allows repeating elements.
. List: elements must be accommodated in a specific order, allowing repeating elements.
. A set (set): There is no special order and no duplicate elements are included.



See examples:
Setexmple.java
Listexmple.java

2.Map

. Map (interface) maintains a "key-value" correspondence (pair) to find the corresponding value by a key

. HashMap is based on a hash list implementation (instead of Hashtable). This form has the most stable performance for the insertion and retrieval of key-value pairs.

See examples:
Printingcontainers.java
Fill containers: Only list, cannot set or MAP
See examples:
Fillinglists.java
Container Disadvantages:
. Do not know the data type
. You must do the type conversion yourself

See Example: Cat.java
Dog.java
Catsanddogs.java



Compare list, LinkedList, ArrayList
The list (interface) order is the most important feature of the list:
. Elements are arranged in the specified order.
. List adds a number of methods to collection:
. Inserting and deleting elements in the middle of a list
. (Listiterator (list repetition), which can be traversed in a list in two directions, inserting and deleting elements in the middle of the list
Compare list, LinkedList, ArrayList
. ArrayList: Allows us to quickly access elements, but is slightly slower when inserting and deleting elements from the middle of the list. In general, you should only use Listiterator to move forward and backward through a ArrayList, and do not use it to delete and insert elements;

. LinkedList: You can efficiently insert and delete operations in the middle of the list. But at random access, the speed is slow. AddFirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), and Removelast () (not defined in any interface or base class) are provided for use as a specification, Queue and a two-way queue use
Implement queue operations with LinkedList.
See Example: Queue.java
Map Comprehensive Examples:
Map1.java
Array
The arrays class provides an overloaded sort () and binarysearch () for arrays of all basic data types, which can also be used for string and object.

See Example: Array1.java

3. Repetitive device

Iterators Re-timer:
A repeating device is used to extract elements from a collection.
. An Iterator of a Set is unordered.
. Listiterator of a List can traverse the movement before and after

Iterators Re-timer:
See examples:

Catsanddogs2.java
Hamstermaze.java
See Iterators and collection's comprehensive example: Collection1.java

4. Sorting and searching

A list can be sorted and searched in the same form as the array. static methods for sorting and searching lists are included in the class collections, and sort (list) is used for sorting a list of objects that implement comparable; BinarySearch (List,object)
An array element can be a basic data type and a reference data type. Array element type is explicit. can be multidimensional. The disadvantage is that once created, the size cannot be changed.
Colloction stores a single set of elements, and map stores a set of elements
The list holds an ordered element as an array, but cannot hold the base data type and requires a type conversion when reading an element.
The ArrayList is used to randomly read data. LinkedList is used to delete large amounts of data from the middle insert.
LinkedList for implementing queues and stacks
Map is used to connect a pair of object-object. The HashMap is used for quick lookups.

. Set cannot accept duplicate elements. The hashset is used to provide quick lookups.

5.Collections class

1.Enumeration interface

The enumeration interface uses two methods to retrieve a series of objects or values:
Nextelement (): Get Next Object
hasMoreElements (): Checks if there are more objects in the sequence

Attention:
The elements of the enumeration interface can only be accessed once.
2.Vector class
The Vector class implements the ability to grow an array. Arrays can be either larger or smaller.
Functions: Adding, deleting, and inserting objects, testing the contents of the vector, and retrieving the specified object.

See Example: Example66Vector.txt

3. Stack Stacks
The stack class extends the vector class and inherits the method of the vector class. It implements the function of last-in-first-out.

Create Stack class: New Stack ()
Common methods:
Object push (Object o)
Object pop ()
Object Peek ()
Boolean empty ()
int search (Object o)

See Example: Example67Stack.txt
4. Hash table Hashtable
A hash table can also store an indeterminate number of object pairs (key/element). It can be implemented to put different types of objects into the same hash list.
Some of the commonly used methods are:
Put (object key, Object value)
Get (Object key)
Remove (Object Key)
Elements ()
Keys ()
Size ()
IsEmpty ()

See Example: Example68HashApp.txt
A hash table enables you to put different types of objects into the same hash table. It is to be combined with a forced type conversion.

See Example: Example68Hashtable.txt

5.StringTokenizer class
The StringTokenizer class is used to create a syntax parser for a string object. It parses a string based on a set of delimiters. It executes the enumeration interface.

Some of the commonly used methods are:
Hasmoretokens ()
NextToken ()
Counttokens ()
Nextelement ()
hasMoreElements ()

See Example: Example69Token.txt
6.Math class
The math class contains a set of static mathematical methods, including: algebra, triangles, exponents, logarithms, random numbers, and so on.
Examples:
public class Mathapp {

public static void Main (string[] args) {
System.out.println (MATH.E);
System.out.println (Math.PI);
System.out.println (Math.Abs (-1));
System.out.println (Math.max (3,5));

for (int i=0;i<5;i++)
System.out.print (Math.random () + "");
System.out.println ();
}
}

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.