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 ()