JAV Basics ———— A simple understanding of collections

Source: Internet
Author: User
Tags hash
Collection:
You can store different types of objects and automatically increase as the storage objects increase in capacity.
The object is added, and cannot be a base type.
Each collection stores data in a different way, that is, using a different data structure.
The important interfaces in collection are: List,set.
List: The stored objects are ordered and can be duplicated. (ordered refers to the order of objects in the collection and the order in which the objects are added.) )
List:
ArrayList LinkedList Vector
ArrayList: The data structure used in the bottom layer is an array, the thread is unsafe, the search speed is fast, and the deletion speed is slow.
LinkedList: The data structure used at the bottom is a linked list, the thread is unsafe, the search speed is slow, and the speed of deletion is fast.
Vector: Similar to arreaylist, but thread-safe, replaced by ArrayList.


Set: The stored object is unordered and cannot be duplicated.
Set:
HashSet TreeSet
HashSet: The underlying data structure used is a hash table, which is not thread-safe.
TreeSet: The underlying data structure used is a two-fork tree, which is not thread-safe.


ArrayList:
Remove duplicate objects from the collection
ArrayList list = new ArrayList ();
List.add ("Str01");
List.add ("Str02");
List.add ("str03");
List.add ("Str02");
ArrayList list2 = new ArrayList ();//Create a fresh collection
for (int i=0;i<list.size (); i++)
{
Object obj = List.get (i);
The contains () method is based on the Boolean Equals () method
If (! ( List2.contains (obj))
{
List2.add (obj);
}
}
To determine if two new objects are the same, override the Equals () method in the new object class
public boolean equals (Object obj)
{
If (! ( obj instanceof Student))
{
throw new ClassCastException ();}
}
Student stu = (Student) obj;
return this.age==stu.age && this.name.equals (stu.name);
}


LinkedList:
Unique method: AddFirst (), AddFirst ()---add
GetFirst (), getlast ()---get
Removefirst (), removelast ()---Delete
Starting from jdk1.6:
Offerfirst (), Offerlast ()---add
Peekfirst (), peeklast ()---get
Pollfirst (), polllast ()---Delete


HashSet:
The only way to guarantee the object:
1. When adding an object, first compare the hash value of the object with the hash value of the object in the collection, if the hash value of the collection object
are not the same, then add the object to the collection
2. If the hash value is the same, then calling the Equals () method returns True to be considered the same object, not to join the object
collection, otherwise considered to be a different object, join the collection


TreeSet:
Adding an object using Add () calls the CompareTo () method to compare the objects that are joined to the collection (in dictionary order)
1. Sorting method One:
The class to which the object belongs in the collection implements the Int CompareTo (object obj) method in the comparable interface, which is treeset sorted by this method
The only way to guarantee the object: CompareTo () The return value is 0, the object is the same
2. Sorting method Two:
Customizing a sort order to implement the int compare (Object obj) method in interface comparator
Note: Custom sorting takes precedence when TreeSet has a custom sort method

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.