Object-Oriented Programming: Java collection manages elements more effectively

Source: Internet
Author: User

 


Sets
In the Java Collection structure, a set is a collection of many elements. It identifies an exact set model containing equivalent elements. The Set interface expands the collention interface, this means that you can add objects, delete objects, and re-iterate objects in the set. On the Set interface, two methods are added for sorting names, and a set can be converted into a column of objects.

SortedSets
SortedSets is the set for the iterative process of arranging elements in ascending order. The elements in the Set are classified in natural order or comparative method.

Comparators
Comparators is the interface for defining comparison functions or other functions. In this way, the result of running its object is a comparison or equivalent function. Comparators is passed to the classification program to control many elements.

Maps
Unlike Set, Map is not generated by Collection. Instead, it provides an interface that uses some key input and output values, which is very similar to java. util. Hashtable.

Map is an object that contains a column of key/value pairs. Map does not contain a copy key, and each key can have only one value, map can restore a set of keys, a set of values, and a series of mapping functions.

Sorting
There are two basic classification methods for the collection Structure

You can use any running list interface of the two static methods in the Collection class to classify objects. One method is to obtain the LIST Parameters of the running comparison interface, and the other method is to obtain the LIST Parameters and comparison parameters and classify the list elements of the comparison objects.
You can also add the comparison interface function to the Collention class and add the comparison method to the class. The result is to subtract the second parameter from the first parameter, then, the Collection object is passed to the object running the comparison interface.
The example in table A demonstrates the comparison interface for classification of MySortedMapComparator objects.

Listing

Class MySortedMapComparator
 Implements Comparator
 {
 Public MySortedMapComparator ()
 {
 }
 
 Public int compare (Object o1, Object o2)
 {
Return (MySortedMapImpl) o1). compareTo (MySortedMapImpl) o2 );
 }

 Public boolean equals (Object obj)
 {
If (! (Obj instanceof MySortedMapComparator ))
Return false;
Return false;
 }
 }


SortedMaps
SortedMaps is a map that provides the ability to arrange elements in ascending order. These elements are classified by natural order or comparative method. See the example in table B.

Listing B

Import java. util .*;

 Public class MySortedMapImpl
 Implements SortedMap, Comparable
 {
 Private java. util. HashMap

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.