前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java集合collection之Set

Java集合collection之Set

原创
作者头像
金GoS
修改2019-12-30 10:51:34
5230
修改2019-12-30 10:51:34
举报
文章被收录于专栏:技术学习实践

一、HashSet、TreeSet、LinkedHashSet区别:

1.HashSet:将元素存储在哈希表中,性能较好,但不保证迭代顺序。

代码语言:javascript
复制
package learn.collection;
import org.testng.annotations.Test;

import java.util.*;

public class hashset {
    Set<String> hashset = new HashSet<String>();
    Set<String> treeset = new TreeSet<String>();
    Set<String> linkedhashset = new LinkedHashSet<String>();

    @Test(groups = {"set"},priority = 0)
    public void addmethod(){
        hashset.add("3");
        hashset.add("e");
        hashset.add("a");
        hashset.add("1");
        hashset.add("2");
        System.out.println("hashset add 方法:"+hashset);
    }

    @Test(groups = {"set"},priority = 1)
    public void addAllmethod(){
        Set<String> newhashset = new HashSet<>();
        System.out.println("addAll方法调用前hashset:"+hashset);
        newhashset.add("a");
        newhashset.add("c");
        newhashset.add("金");
        hashset.addAll(newhashset);
        System.out.println("addAll方法调用后hashset:"+hashset);
    }

    @Test(groups = {"set"},priority = 2)
    public void containsmethod(){
        String str = "5";
        String str2 = "1";
        boolean judge = hashset.contains(str);
        boolean judge2 = hashset.contains(str2);
        if(!judge) {
            System.out.println(hashset + "不包含" + str);
        }
        if(judge2) {
            System.out.println(hashset + "包含" + str2);
        }
    }

    @Test(groups = {"set"},priority = 3)
    public void containsallmethod(){
        Set<String> newhashset = new HashSet<>();
        newhashset.add("1");
        newhashset.add("a");
        boolean judge = hashset.containsAll(newhashset);
        if(judge){
            System.out.println(hashset+"包含"+newhashset);
        }
        newhashset.add("b");
        boolean judge2 = hashset.containsAll(newhashset);
        if(judge2){
            System.out.println(hashset+"包含"+newhashset);
        }else{
            System.out.println(hashset+"不包含"+newhashset);
        }
    }

    @Test(groups = {"set"},priority = 4)
    public void removemethod(){
        System.out.println("remove方法调用前hashset:"+hashset);
        hashset.remove("b");
        System.out.println("remove方法调用后hashset:"+hashset);
        hashset.remove("a");
        System.out.println("remove方法调用后hashset:"+hashset);
    }


    @Test(groups = {"set"},priority = 5)
    public void retainmethod(){
        Set<String> newhashset = new HashSet<>();
        newhashset.add("金");
        newhashset.add("6");
        hashset.retainAll(newhashset);
        System.out.println(hashset+"调用retain方法:"+hashset);
    }
}

2.TreeSet:将元素存储在红黑树中,按元素值顺序排列,比HashSet处理较慢。

代码语言:javascript
复制
package learn.collection;
import org.testng.annotations.Test;

import java.util.Set;
import java.util.TreeSet;

public class treeset {
    private Set<String> treeset = new TreeSet<>();

    @Test(groups = {"treeset"},priority = 0)
    public void treesetmethod(){
        System.out.println("treeset add调用前"+treeset);
        treeset.add("3");
        treeset.add("b");
        treeset.add("1");
        treeset.add("a");
        treeset.add("2");
        System.out.println("treeset add调用后"+treeset);
    }
}

3.LinkedHashSet:作为哈希表实现,用链表连接元素,按元素插入顺序排列。

代码语言:javascript
复制
package learn.collection;

import org.testng.annotations.Test;

import java.util.LinkedHashSet;
import java.util.Set;

public class linkedhashset {
    Set<String> linkedhashset = new LinkedHashSet<>();

    @Test(groups = {"linkedhashset"},priority = 0)
    public void treesetmethod(){
        System.out.println("linkedhashset add调用前"+linkedhashset);
        linkedhashset.add("3");
        linkedhashset.add("b");
        linkedhashset.add("1");
        linkedhashset.add("a");
        linkedhashset.add("2");
        System.out.println("linkedhashset add调用后"+linkedhashset);
    }
}

扩展知识:

接口、接口继承

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、HashSet、TreeSet、LinkedHashSet区别:
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档