Main sub-interfaces of collection:
- List: stores duplicate content.
- Set: duplicate content cannot be stored. All duplicate content is differentiated by hashcode () and equals ().
- Queue: queue Interface
- Sortedset: sorts data in a set.
List interface: summarizes the extension methods of the List interface, that is, adding, deleting, modifying, and querying methods.
A common subclass of the List interface: arraylist: The list interface can be instantiated directly through the object polymorphism. vector: it is a primitive class. The list operation class is represented by the list operation class, and the list interface and queue interface are implemented at the same time.
Set interface: A common subclass of the Set interface: hashset: duplicate elements cannot be stored and stored in a hash mode. Therefore, there is no sequence treeset: duplicate elements cannot be stored, however, the input data is ordered (the sortedset interface is implemented)
Specify sorting:
Class person implements comparable <person> {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String tostring () {return "name:" + this. name + "; age:" + this. age;} public int compareto (person per) {If (this. age> Per. age) {return 1;} else if (this. age <per. age) {return-1;} else {return this. name. compareto (Per. name); // call compareto () method in string }}; public class treesetdemo {public static void main (string ARGs []) {set <person> allset = new treeset <person> (); allset. add (new person ("Zhang San", 30); allset. add (new person ("Li Si", 31); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Zhao ", 33); allset. add (new person ("Sun Qi", 33); system. out. println (allset );}};
Result: [name: Zhang San; age: 30; Name: Li Si; age: 31; Name: Wang Wu; age: 32; Name: Sun Qi; age: 33; Name: zhao 6; age: 33]
Distinct element:
Class person {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} public Boolean equals (Object OBJ) {// overwrite equals, complete object comparison if (this = OBJ) {return true;} If (! (OBJ instanceof person) {return false;} person P = (person) OBJ; // perform a downward transformation if (this. name. equals (P. name) & this. age = P. age) {return true;} else {return false;} public int hashcode () {return this. name. hashcode () * This. age; // define a formula} Public String tostring () {return "name:" + this. name + "; age:" + this. age ;}}; public class repeatdemo {public static void main (string ARGs []) {set <person> allset = new hashset <person> (); allset. add (new person ("Zhang San", 30); allset. add (new person ("Li Si", 31); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Zhao ", 33); allset. add (new person ("Sun Qi", 33); system. out. println (allset );}};
Result: [name: Li Si; age: 31; Name: Zhang San; age: 30; Name: Sun Qi; age: 33; Name: Wang Wu; age: 32; Name: zhao 6; age: 33]
Queue interface: queue operation interface
Sortedset interface: