Collection sub-interface (List/Set/Queue/SortedSet), queuesortedset

Source: Internet
Author: User

Collection sub-interface (List/Set/Queue/SortedSet), queuesortedset
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:


Differences and relationships between List, Set, and Map interfaces in Java

First, both list and set are inherited from Collection, and elements are stored in the form of list sequences. Therefore, the order obtained may be different from the order placed. Set is characterized by the inability to store duplicate elements. Map A ing cannot contain duplicate keys. Each key can map only one value at most. All three of the data stored in key-value pairs are interfaces and cannot be instantiated.

List and Set in java

Both List and Set are interfaces. They have their own implementation classes, with or without sequential implementation classes.
The biggest difference is that the List can be repeated. Set cannot be repeated.
List is suitable for frequent append, insert, and delete data. However, the efficiency of getting data is relatively low.
Set is suitable for storing, inserting, and deleting data frequently. However, the time duration is relatively low.

Related Article

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.