当前位置:网站首页>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());}}
边栏推荐
猜你喜欢
Sweet alert
Command line query database
常用命令之思科常用基础配置
软件测试外包公司怎么样?有什么好处和坏处?为什么没人去?
Cisco common basic configuration of common commands
本体开发日记01-Jena配置环境变量
How much do you know about the mobile APP testing process specifications and methods?
通过程序发送 Gmail 邮件
unittest测试框架原理及测试流程解析,看完绝对有提升
性能测试的基本概念是什么?做好性能测试需要掌握哪些知识?
随机推荐
Sweet alert
问卷问题和答案的合并
RPC服务远程漏洞
本体开发日记04-努力理解protege的某个方面
接口测试主要测试哪方面?需要哪些技能?要怎么学习?
本体开发日记03-理解代码
class object property method class member
迭代
接口性能测试方案设计方法有哪些?要怎么去写?
记录一次被入侵5900端口经历
测试计划包括哪些内容?目的和意义是什么?
软件测试面试常见问题及答案(发散思维、接口、性能、概念、)
8.递归遍历和删除案例
Ontology Development Diary 01-Jena Configuration Environment Variables
seata处理分布式事务
unix环境编程学习-多线程
本体开发日记05-努力理解SWRL(Built-Ins)
6.Map interface and implementation class
unittest测试框架原理及测试流程解析,看完绝对有提升
3.练习Thread