Java collection classes

Source: Internet
Author: User
Tags comparable set set

15th Day Knowledge Point Summary

First, LinkedList Collection Class

unique methods in LinkedList:

1. Method Description:

Add to:

AddFirst (e e); add an element in the first bit of the collection

AddLast (e e); Add the last element in the collection

Get:

GetFirst (); gets the first element in a collection

GetLast (); gets the last element in the collection

Delete:

Removefirst (); Delete the first element in a collection

Removelast (); Delete the last element in a collection

2. Data Structure

1: Stack (1.6) Advanced post-out (stack's structure)

Push ();

Pop ();

2: Queue (dual-ended queue 1.5) FIFO

Offer ();

Poll ();

3. returning an Iterator object in reverse order

Descemdingiterator (); returns an iterative object in reverse order

Second, Vector collection class

Vector: Describes a thread-safe ArrayList using the same as ArrayList

What you need to know is

the difference between Vector and ArrayList :

Same point: Vector and ArrayList are used object[] to achieve

different points: 1.ArrayList is a thread that is out of sync and operating efficiently

Vector threads are synchronous and operate at low efficiency

2.ArrayList appeared in the jdk1.2 , andtheVector was in jdk1.0 .

Three, set set class

The set Collection class is also an interface: Inheritance Collection Features: unordered elements, cannot be duplicated.

HashSet A principle of the deposit value:

First, call the Hashcode method of the calling object to get the hashcode value, and get a position by means of the displacement operation.

Scenario 1: If there is no element at the location, store the element directly in that position

Case 2: If an element is already in place, compare the equals method of the pair with the object that already exists on the position, and if equals returns the True that represents the duplicate object, and the local village, if False returns the object and the original object exist together.

want to traverse Set set:1. to change a collection to an array 2. Iterate through iterators (unordered: The order of addition is different from the order of printing.) )

Four, TreeSet Collection Class

TreeSet Collection classes: Elements are sorted using the natural order of the elements, and are implemented using a two-fork tree at the bottom.

Note: The objects you have stored in need to have natural sorting characteristics.

TreeSet Use note points:

1. When adding objects to TreeSet, if the object has a naturally ordered attribute, sort by natural sort.

2. When adding objects to TreeSet, if the object itself does not have the nature of natural ordering, when running a direct error, if you need to store the object, then the class of the existing object must be implemented comparable the interface defines the comparison rules for elements in the compreto method.

3. When adding objects to TreeSet, if the object itself does not have a natural ordering feature and does not implement the comparable interface, then this time you need to create a Pass in a comparator when TreeSet.

How comparators are defined:

class name implements compartor{

}

4. If the comparable interface is implemented in the class and a comparator is passed in when the TreeSet is created , the comparator is the standard.

Comparison rules for strings:

1. gets the first value in the string to compare, by natural sort comparison, if it is the same as the next value, or the same as in the next, until the comparison can not be compared, this time will compare the length of the string.

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.