Summary of Java collection classes

Source: Internet
Author: User

Http://www.blogjava.net/EvanLiu/archive/2007/11/12/159884.html Collection Inheritance Diagram

Collection is a super interface, and there are a lot of classes that implement this interface. In particular, the use of these classes to achieve it, such as ArrayList, ArrayList is more commonly used.
Collection also belong to the container, the container, the general can not avoid "adding and deleting" the four operation!

Iterator

All implementations of the collection interface container class have a iterator method to return an implementation iterator interface object

The iterator object is called an iterator, which facilitates the traversal of elements within the container.

Set

The set set is not allowed to add two identical elements, while comparisons are made by the Equals method.

(= = compares the addresses of 2 objects, while equals compares the contents of 2 objects)

HashSet

HashSet is a typical implementation of the set interface, mostly when using the set set is the implementation class,

HashSet stores the elements in the collection by the hash algorithm, so it has good storage and lookup performance.

HashSet Features:

There is no guarantee that the elements should be ordered

HashSet is not thread-safe

The collection element can use NULL

The time the element is deposited depends on the value of the Hanscode to determine where the object is stored in the hashset (so there is no guarantee of where the element will be stored). If the value of equals is the same, but Hashcode is different, it can be placed in a different location.

Linkedhashset

Linkedhashset is a subclass of HashSet

The Linkedhashset collection determines where elements are stored based on the hashcode value of the element, while maintaining the order of elements using the linked list, which makes the elements appear to be saved in the order in which they are inserted.

Linkedhashset's insertion is slightly lower than hashset but it has good performance when iterating through all the elements in the set.

Linkedhashset does not allow collection elements to be duplicated.

TreeSet the implementation class of the SortedSet interface, TreeSet can ensure that the collection is in a sorted state.

TreeSet supports two sorting methods, natural sorting and custom sorting, which, by default, TreeSet use natural sorting.

* If you want to sort the objects of a custom class into TreeSet, you must implement the comparable interface
* Implement comparable on the class
* Override CompareTo () method
* Define the comparison algorithm within the method, return positive negative numbers or 0 depending on the size relationship
* When using TreeSet to store objects, the Add () method automatically calls the CompareTo () method for comparison, and is stored using a two-fork tree as the result of the comparison.
*/

Import Java.util.iterator;import java.util.*;p ublic class Treesettest {public static void main (string[] args) {        Set ts = new TreeSet ();        Ts.add ("abc");        Ts.add ("xyz");        Ts.add ("rst");        Iterator it = Ts.iterator ();        while (It.hasnext ()) {System.out.println (It.next ()); }    }}

Output Result:

Abc

Rst

Xyz

The printed result is not the same as the order in which it was previously added, it is sorted by a single letter sorting method. This is because the string class implements the comparable interface.

If the object of a class that we define ourselves is to be added to the TreeSet, then this class must implement the comparable interface.

package test.treeset;import java.util.iterator;import java.util.set;import  java.util.treeset;public class test_treeset {      @SuppressWarnings (" Unchecked ")     public static void main (String[] args)  {             set ts = new treeset ();             ts.add (New Teacher ("Zhangsan",  1));             ts.add (New Teacher (" Lisi ",  2));             ts.add (new  Teacher ("Wangmazi",  3));             ts.add ( New teacher ("Wangwu", 4));             ts.add ( New teacher ("Mazi",  3));            iterator it = ts.iterator ();             while  (It.hasnext ())  {                 system.out.println (It.next ());            }         }}class Teacher implements Comparable {    int  Num;    string name;    teacher (String name, int  NUM)  {        this.num = num;         this.name = name;    }    public  string tostring ()  {        return  "Study No.:"  +  num +  "\t\t Name: " + name;    }    //o in the red and black binary tree when stored in the node, starting from the root node comparison      public int compareto (Object o)  {         Teacher ss =  (Teacher)  o;        int  result = num < ss.num ? 1 :  (num == ss.num ? &NBSP;0&NBSP;:&NBSP;-1);//Descending         //int result = num  > ss.num ? 1 :  (num == ss.num ? 0 : -1);//Ascending         if  (result == 0)  {             result = name.compareto (Ss.name);         }        return result;     }}

Operation Result:

School Number: 4 Name: Wangwu
School Number: 3 Name: Mazi
School Number: 3 Name: Wangmazi
School Number: 2 Name: Lisi
School Number: 1 Name: Zhangsan

List

Represents an ordered, repeatable set of elements, each of which has its own sequential index, which is indexed by default in order to add elements to an element.

ArrayList and vectors are two typical implementations of a list interface,

It is generally recommended to use ArrayList.

ArrayList threads are unsafe while vectors are thread-safe

Summary of Java collection 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.