当前位置:网站首页>5.Set interface and implementation class

5.Set interface and implementation class

2022-08-09 09:33:00 come here my bear

Set interface and implementation class

Set subinterface

Overview

  • Features: unordered, no subscripts, elements cannot be repeated

  • Methods: all inherited from the methods in Collection

Use of Set Interface

package com.jhkj.set;import java.util.HashSet;import java.util.Iterator;import java.util.Set;/*** Use of set method*/public class Demo1 {public static void main(String[] args) {// create collectionSet set = new HashSet<>();// 1. Add dataset.add("Huawei");set.add("Xiaomi");set.add("apple");System.out.println("Number of elements" + set.size());System.out.println(set.toString());// 2. Delete dataset.remove("Huawei");System.out.println(set.toString());// 3. Traverse the dataSystem.out.println("-------use enhanced for-------");for (String s : set) {System.out.println(s);}System.out.println("-------use iterator------------");Iterator iterator = set.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}// 4. JudgmentSystem.out.println(set.contains("Huawei"));System.out.println(set.isEmpty());}}

Set implementation class

Note: Any method can be overridden using any comparison

  • HashSet:
    • Storage structure: hash table (array + linked list + red-black tree)
    • Stored Procedure: (repeat by)
        1. Calculate the saved location according to Hashcode, if this location is empty, save it directly, if not, execute the second step
        2. Execute the equals method again, if the equals method is true, it is considered a duplicate, otherwise, a linked list is formed
    • Calculate element storage location based on HashCode
    • When the hash codes of the stored elements are the same, equals will be called to confirm, and if the result returns true, the latter will be rejected
  • TreeSet:
    • Comparable Comparator
    • Storage structure: red-black tree
    • Requirements:
        1. Element (class) must implement Comparable interface
        2. Judgment If the return value of the compareTo() method is 0, then the recognition is a duplicate element
    • Elements are not repeated based on the arrangement order
    • Implements the SortedSet interface to automatically sort the set elements
    • The type of the element object must implement the Comparable interface, specifying the collation
    • Use the CompareTo method to determine whether it is a duplicate element

Implement Comparator

package com.jhkj.set;import java.util.Comparator;import java.util.TreeSet;/*** Comparator* Comparatble* Use Comparator to implement customizable comparator*/public class Demo6 {public static void main(String[] args) {// create objectTreeSet set = new TreeSet<>(new Comparator() {@Overridepublic int compare(Person o1, Person o2) {int n1 = o1.getName().compareTo(o2.getName());int n2 = o1.getAge() - o2.getAge();return n1==0?n2:n1;}});Person p1 = new Person("xyz", 11);Person p2 = new Person("hello", 12);Person p3 = new Person("zero", 13);set.add(p1);set.add(p2);set.add(p3);System.out.println("Number of elements: " + set.size());System.out.println(set.toString());}}

Use TreeSet to sort strings by length

package com.jhkj.set;import java.util.Comparator;import java.util.TreeSet;/*** TreeSet case* Use the TreeSet collection to sort strings by length*/public class Demo7 {public static void main(String[] args) {TreeSet strings = new TreeSet<>(new Comparator() {@Overridepublic int compare(String o1, String o2) {int n1 = o1.length() - o2.length();int n2 = o1.compareTo(o2);return n1==0?n2:n1;}});strings.add("beijing");strings.add("pingzi");strings.add("helloworold");strings.add("car");System.out.println("Number of elements: " + strings.size());System.out.println(strings.toString());}}
原网站

版权声明
本文为[come here my bear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090923102965.html