当前位置:网站首页>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)
- Calculate the saved location according to Hashcode, if this location is empty, save it directly, if not, execute the second step
- 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:
- Element (class) must implement Comparable interface
- 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());}}
边栏推荐
猜你喜欢
[Machine Learning] Basics of Data Science - Basic Practice of Machine Learning (2)
本体开发日记03-排错进行时
本体开发日记05-努力理解SWRL(上)
手机APP测试流程规范和方法你知道多少?
软件测试面试中,面试官问你一些比较“刁难”的问题你会怎么回答
测试用例的原则、缺陷报告怎么写你都知道吗?
接口测试主要测试哪方面?需要哪些技能?要怎么学习?
列表
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(4.Building An OWL Ontology)
China to create a domestic "Google Earth" clarity scary
随机推荐
软件测试面试中,面试官问你一些比较“刁难”的问题你会怎么回答
oracle查看表空间占用情况并删除多余表所占空间
接口性能测试方案设计方法有哪些?要怎么去写?
2.线程创建
Another implementation of lateral view explode
2.字节流
自动化测试框架有哪几种?搭建的思路是什么?一篇文章让你彻底了解自动化
try catch 对性能影响
单元测试是什么?怎么写?主要测试什么?
STM32F103实现IAP在线升级应用程序
Lecture 4 SVN
A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
4.泛型和工具类
本体开发日记05-努力理解SWRL(上)
7.FileFilter interface
2. Thread creation
关于一次性通过CISSP考试的一点经验分享
Anti App so层对抗分析
Redis high availability
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)