First, Introduction: Want to give ArrayList sort but found no sorting method? You have two options:
1, swap with treeset;
2. Use Collection.sort (list<t> list)/Collection.sort (list<t> list, comparator<? Super T> C) method.
Second, to expand the analysis of several major Java data structure and sorting methods:
| LinkedList |
Efficient operation of elements |
| TreeSet |
No repetition, orderly |
| HashSet |
Do not repeat, quick find |
| HashMap |
NVP (key cannot be duplicated), stored by hash algorithm, often inconsistent with the order of deposit |
| Linkedhashmap |
NVP (key cannot be duplicated), remembers the order in which the elements are inserted, and is taken in the same order as the deposit |
Ps:hashset<t>.addall (arraylist<t>) can add all the elements in this way
Iii. type parameters and generics
often seen in the APIPublic class Arraylist<e> extends Abstractlist<e> {...}the e here is replaced by the actual type that is actually passed in, which is also known as the type parameter . Class arraylist<e> is called a generic class . Similarly, a generic method represents a method declaration with a type parameter. There are two ways to use it: (1) used in a class that uses the type parameter
1 Public class extends Abstractlist<e>{2public boolean Add (E o) {3 ...... 4 }5 }
(2) used in a class that does not use the type parameter, but cannot add anything to the collection, only read and Invoke methods: (Add a declaration before the return type)
1 Public extends void takething (arraylist<t> list) {2 ..... 3 }
or use?
1 Public void extends animal> animals) {2 ..... 3 }
Note: This is not the same as what the following code means:
1 Public void takething (arraylist<animal> list) {2 ..... 3 }
The former is a representation of the type parameter as long as it is animal or its subclasses , and the latter can only be animal If there is an incorrect operation for the latter, such as adding a subclass of animal, it will only be discovered during runtime. in generics, extend represents extend or implement
Four, using the Collections.sort (list<t> List) method
(i) sort the ArrayList that hold the String
1 New Arraylist<string>(); 2 collections.sort (songList);
(ii) ArrayList sorting of the objects to be stored(1) The object needs to implement the Comparable<e> interface (rewrite the CompareTo method, write the comparison logic in it), for example:
1 Public int CompareTo (Song s) {2 return Title.compareto (S.gettitle ()); 3 }
(2) The steps are the same as for string sorting. v. Using Collections.sort (list<t> List, Comparator<? Super T> C) methodThe approach is to use homemade comparator to help with sorting for a more flexible sort. Code:
1 //Inner class2 classArtistcompareImplementsComparator<song>{3 Public intCompare (song one, song) {4 returnone.getartist (). CompareTo (. getartist ()); 5 }6 }7Artistcompare Artistcompare =NewArtsitcompare ();8Collections.sort (SongList, Artistcompare);
Six, think about, holding two objects, there are so many ways to compare, you than that? So: Do you think the comparison of objects is so simple?
The default behavior of hashcode () is to produce unique values for objects on the heap. The default behavior of equals () is to perform a = = comparison. That is, the comparison between object byte combinations. so if you have not rewritten the above two methods, two objects will be considered different anyway. following: Equality of references and equality of objects 1 . equality of references : references to the same object, the reference is equal. call Hashcode () to get the same result, hashcode () returns a unique ordinal number based on the object's position in memory, which is not duplicated. you can use = = to determine if the reference variable is equal (the object's byte combination); 2 . equality of Objects : If you want to treat two different objects as equal, you must overwrite the Hashcode () method inherited from object with the Equals () method. Make the former equal, the latter returns True (previously mentioned) but two objects that have the same hashcode are not necessarily equal (the collision value of the hash algorithm exists). Overlay Example: Compare songs with the title of a song to see if the song is the same:
1 classSongImplementsComparable<song>{2 PrivateString title;3 Public Booleanequals (Object o) {4Song s =(Song) o;5 returngetTitle (). Equals (S.gettitle);6 }7 Public inthashcode () {8 returngetTitle (). Hashcode ();9 }Ten PublicString GetTitle () { One return This. title; A } -}
Seven, in the introduction of the above objects how to compare, still remember the beginning mentioned can mention ArrayList TreeSet? TreeSet elements must be (implemented) comparable or constructors that use overloads, take comparator parametersThe second example:
1 New Treeset<book> (bcompare); // Bcompare is an object that implements the comparator interface class.
Viii. How to obtain the map key value pair
1. Traditional Method 1 (obtained after turning into set)
2. Traditional Method 2 (obtained after turning into set)
3. Enhanced for loop (recommended, jdk1.5 can be used later)
instead of Iterater, you can iterate directly over the set of the above two methods.
Java Data structure and sequencing