Basics-set and generic

Source: Internet
Author: User

1. Set

Treeset: It is maintained in an ordered state and can prevent duplication. Its element must be comparable.

Hashmap: pair key/value for access

Efficient list: highly efficient set designed for frequent insertion or deletion of intermediate Elements

Hashset: prevents repeated sets and quickly finds consistent elements.

Linkedhashmap: similar to hashmap, but you can remember the sequence of element insertion, or set it to sort by the order of the last element access.

Arraylist: avoid repeated elements and perform dynamic operations.

Ii. Sorting

You can use collection. Sort () to directly sort the basic primary data type.

Collection. Sort ():

Public static <t extends comparable <? Super T> void sort (list <t> List)

Public <t extends animal> void take (arraylist <t> List ){}

Sort by an instance variable in the class:

Class song implements comparable <song> {String title; @ overridepublic int compareto (Song s) {return title. compareto (S. tile );}}
The preceding method can only be sorted by title.

Sort by multiple instance variables:

1. create and implement the internal class of comparator, and replace the compareto () method with the compare () method;

2. Create an instance of this type;

3. Call sort () of the reload version ();

 
Public class box {... class compare implements comparator <song> {@ overridepublic int compare (song one, sone two) {return one. artist. compareto (two. artist) ;}@ overridepublic int wordscompare (song one, sone two) {return one. words-two.words ;}}... public void go (){... compare AC = new compare (); Collection. sort (songlist, AC );}}
Iii. Object equality judgment

 
If (FOO. Equals (bar) & Foo. hashcode () = bar. hashcode () {// Foo equals to bar}
Equals ensures that the reference is the same; hashcode conflicts;

Iv. polymorphism parameters and generics

 
Public <t extends animal> void take (arraylist <t> List) Public void take (arraylist <? Extends animal> List)
Example:

 
Arraylist <dog> dogs = new arraylist <animal> (); arraylist <animal> animals = new arraylist <dog> (); list <animal> List = new arraylist <animal> (); // okarraylist <dog> dogs = new arraylist <dog> (); // okarraylist <animal> animals = dogs; list <dog> doglist = dogs; // okarraylist <Object> objects = new arraylist <Object> (); // oklist <Object> objlist = objects; // okarraylist <Object> objs = new arraylist <dog> ();

V,

1. collection. Sort (list );

The classes of objects in the list must implement comparable and compareto ()

2. collection. Sort (list, compare );

The class of objects in the list does not need to implement comparable or comparator.

The Compare object class must implement comparator, compare ()




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.