Java Collection Framework 1

Source: Internet
Author: User
Tags comparable java se

Java SE contains a Java collection framework consisting of a set of classes and interfaces used to organize stored data in a certain structure and access it in a specific way, in order to provide a common framework for working with collections of objects.

The Java Collection framework divides the way objects are stored into three types, namely:

Set: Objects in an object container have no order and cannot be duplicated.

List: Objects in the object container are sorted by index order and can have duplicate objects.

Map: An element in an object container contains a pair of "key Object-Value object" mappings where key objects cannot be duplicated and value objects can be duplicated.

To support the ordering and traversal of objects, the following interfaces are available:

Interface SortedSet provides sorting functionality for set type containers.

Interface SortedMap is a sort of key object that is provided for the map type container.

Interface iterator provides a walker to iterate through the collection object.

Interfaces comparable and comparator are used to implement the ordering of objects in the collection.

The collection Collection interface is located at the top of the set interface and the list interface, and is the parent interface of the set interface and the list interface. The collection interface defines the basic operations (additions and deletions ...). ), bulk operations, array operations, and the basic methods.

There is a iterator () method in the collection interface that returns a iterator object that iterates through all the elements in the collection.

Iterator interface Method:

1.hasNext (): If there are more elements in the collection, the method returns True.

2.next (): Returns the next element in the collection.

3.remove (): Deletes the last element returned by iterator.

Each collection provides a dedicated mechanism to access their data, but they all support the iterator method, so we can apply a unified mechanism to all the traversal of the combined class.

In addition, a Foreach Loop statement is provided after JDK1.5, which allows you to iterate through the collection and arrays in a simpler way.

In actual development we are often subinterfaces using the collection Interface: List and set

List interface

The elements in 1.List are sequential.

2.List usually allows elements to be duplicated.

3. Support for NULL elements

4. The elements in a list object can be accessed by index

1) ArrayList (using arrays as containers in the bottom layer, different can be deleted and modified)

Common methods: Add (), AddAll, Get (), set (), remove ()

Vectors are similar to ArrayList, except that vectors are thread-safe and slow.

Generic type:

Allows you to specify a type parameter when defining a class or interface, forcing the program to check the type of data obtained from the collection at compile time.

Equals () Method:

Locating an object is positioned by comparing it with the Equals method.

2) LinkedList

LinkedList is a list of functions that implement a doubly linked list, where each element is placed in a separate space, and each space holds the index of the previous and next linked lists.

When you need to make a large number of additions and deletions to the middle part of the element, you can choose to use LinkedList.

Set interface

Inherits all the methods of the collection interface and has the following characteristics:

1. Element cannot be duplicated (must override Equals () method &hashcode () method)

2. Elements may or may not be in order.

3. You cannot use an index to directly access elements in a set.

The most common classes of set are HashSet and TreeSet

1.HashSet

The implementation of set interface based on hash algorithm has the following characteristics:

1. When traversing hashset, the elements have no order.

2. Duplicate elements are not allowed. (with the same hash code and the Equals () method to compare two objects that return true)

3. Allow null elements to be included

2.TreeSet

The SortedSet interface is implemented at the same time that the set interface is implemented, and the objects are ordered in a certain order. This order is ordered not by the order in which the objects are added, but by some algorithm.

You can also sort according to the comparator provided.

There are some classes in the JDK class library that implement the comparable interface, such as Integer, Double, string wait, the interface has a CompareTo (object O) method, return the integer data, return a positive number for a large backward sort.

TreeSet is arranged in ascending order according to the size of the collection element, and the custom sort is sorted by a specific comparer (using the comparator interface while overwriting the compare (object 01,object O2) method).

Map interface

The map interface is another important interface in the Java Collection framework that differs from the collection interface, which corresponds to a collection of the corresponding relationships from key (key) to a value (value). The map type has two sets of objects, key and Value,key cannot be duplicated, and value can be repeated.

The map interface defines the methods common to map objects: basic operations, bulk operations, collection view operations (provides the means to convert map to collection)

The basic method of map: Get (Object key) returns the value associated with the specified key, put (object Key,object value) adds a key-value pair to the map, remove (object key) deletes the specified key-value pair

ContainsKey (Object key) if the map contains the specified key that returns True, Containsvalue (object value) if the map contains the specified value that returns True, KeySet (), Values ()

      

Map Features: We can set interesting keys (such as setting the user name to the key); map cannot traverse keys and values at the same time, only one of them is traversed.

  

HashMap

The implementation of map interface based on hash algorithm. You must override the Hashcode () and the Equals method.

HashMap uses a hash code to quickly find its contents using a key.

TreeMap

TreeMap class is based on the red and Black Tree algorithm map interface implementation, the key is stored in a way similar to TreeSet, stored in the tree, the order of the keys in the natural order or custom order in two ways. Because it takes the sorting problem into account, the speed is slower than HashMap.

Tool classes Collections and arrays

Tool classes Collections and arrays provide a number of ways to sort, query, modify, and manipulate elements, all of which are static methods.

Collections.sort (Object o) natural ordered, Collections.reverse (object o) reversed sort, collections.shuffle (object o) Mixed

In addition, collections provides several static methods for creating a thread-safe synchronization collection:

1.synchronizedCollection (Collection c) returns a thread-safe Collection.

2.synchronizedList (List list) returns a thread-safe list

3.synchronizedSet (set S) returns a thread-safe Set

4.synchronizedMap (map m) returns a thread-safe Map

Arrays class

Used to manipulate the sorting and searching of arrays, and so on.

Sub-category properties of Hashtable

Used to process the Properties folder, the map form of the key value to the data in a text file with the extension ". Properties", as the software configuration file.

The main methods of the properties class are:

1.getProperty (String key) gets the corresponding value based on the property key key.

2.setProperty (String key,string value) setting properties

3.load (InputStream in): Loads all properties from the input stream

4.store (OutputStream out,string comments) Outputs the property content through the output stream

Java Collection Framework 1

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.