Basic Java Review: Set Interface

Source: Internet
Author: User
Tags comparable

Set features:

Set cannot remember the order in which elements are added. duplicate elements are not allowed.

Set determines whether two objects are equal using the equals method.

Common subclass:

HashSet Hash Storage, fast query speed

TreeSet ordered Storage

Import java. util. hashSet; import java. util. set; public class SetDemo {public static void main (String [] args) {Set set = new HashSet (); set. add ("well"); // HashSet does not allow repeated element set. add ("well"); System. out. println (set); // [well] set. add (null); // HashSet can only add one nullset. add (null); System. out. println (set); // [null, well] // clear the element set. clear (); System. out. println (set );}}

 

TreeSet uses the natural sequence of elements for sorting, or sorts the elements based on the Compatator provided when the Set is created. The elements involved in sorting must be of the same type.

Natural sorting:

TreeSet calls the compareTo (Object o) method of the element to compare the size relationship between elements, and then sorts the elements in the Set in ascending order. In this case, the class for sorting elements must implement the Comparable interface and overwrite its compareTo method.

 

Import java. util. set; import java. util. treeSet;/** sort by age in ascending order **/class Person implements Comparable {private String name; private Integer age; Person (String name, Integer age) {super (); this. name = name; this. age = age;}/** requires sorting. You must implement the Comparable interface and overwrite the compareTo Method */public int compareTo (Object o) {if (o instanceof Person) {Person p = (Person) o; return this. age. compareTo (p. age) ;}return 0 ;}@ Overridepublic String toString () {return this. name + "-" + this. age ;}} public class TreeSetDemo {public static void main (String [] args) {Set set = new TreeSet (); set. add (new Person ("well", 21); set. add (new Person ("tom", 20); set. add (new Person ("Lily", 22); System. out. println (set); // [tom-20, well-21, Lily-22]}

 

Custom sorting:

If you want to sort objects according to your requirements, you can use the Comparator interface, which contains the compare (o1, o2) method to compare the sizes of two objects.

Import java. util. comparator; import java. util. set; import java. util. treeSet;/** by default, TreeSet is in ascending order. * Currently, requirements: sort by age, in descending order. */class Student {private String name; private Integer age; student (String name, Integer age) {super (); this. name = name; this. age = age;} public String getName () {return name;} public Integer getAge () {return age;} public String toString () {return this. name + "-" + this. age ;}}/*** custom sorting rules: sort in descending order * Implement the Comparator interface and overwrite the compare Method * return a negative integer, zero, or positive integer based on the first parameter smaller than, equal to, or greater than the second parameter. */Class MyComparator implements Comparator {@ Overridepublic int compare (Object o1, Object o2) {Student s1 = (Student) o1; Student s2 = (Student) o2; if (s1.getAge ()> s2.getAge () return-1; else if (s1.getAge () <s2.getAge () return 1; return 0 ;}} public class ComparatorDemo {public static void main (String [] args) {/** TreeSet (Comparator <? Super E> comparator) * constructs a new empty TreeSet, Which is sorted by the specified comparator. */Set set = new TreeSet (new Comparator () {// use an anonymous internal class. Here we write a comparison rule @ Overridepublic int compare (Object o1, Object o2) {Student s1 = (Student) o1; Student s2 = (Student) o2; if (s1.getAge ()> s2.getAge () return-1; else if (s1.getAge () <s2.getAge () return 1; return 0 ;}}); set. add (new Student ("Tom", 20); set. add (new Student ("Lily", 18); set. add (new Student ("Well", 25); System. out. println (set );}}

 

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.