栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java集合1.2 ? set-HashSet、set-LinkedHashSet、?set-SortedSet-TreeSet

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java集合1.2 ? set-HashSet、set-LinkedHashSet、?set-SortedSet-TreeSet

Java集合 set特点
  • 可以存储任何类型的值
  • 无序且不可重复,无序不会按照新增的顺序存储,不可重复指元素的值不可重复储存
  • HashSet:无序不可重复
  • LinkedHashSet:有序不可重复,有序指按照新增的顺序储存

set与Lisi

1、HashSet

无序不可重复

public class HasMap {
    @Test
    public void test01() {
    //无序且不可重复,无序不会按照新增的顺序存储,不可重复指元素的值不可重复储存
        Set set = new HashSet();
        byte a = 1;
        short b = 2;
        int c = 3;
        long d = 4l;
        double e = 5.0d;
        float f = 6.0f;
        char g = 'a';
        boolean h = false;
        String i = "你好java";

        set.add(a);
        set.add(b);
        set.add(c);
        set.add(d);
        set.add(e);
        set.add(f);
        set.add(g);
        set.add(h);
        set.add(i);
        System.out.println(set);//长度
    }
    
    运行:
   		 [6.0, 1, a, 2, 3, 4, 5.0, false, 你好java]
   		 

Set遍历

for循环:
    @Test
    public void test02() {
        //没有下标是无序的:所以不能用下标遍历,使用增强for循环,或迭代器
        Set set = new HashSet();
    for (int i=0;i<5;i++){
        set.add("你好"+i);
    }
        System.out.println(set);
//-----------------------------------------------------------------------

增强for循环遍历:        
    for(String name:set ){
        System.out.println(name);
    }
//------------------------------------------------------------------------
迭代器遍历:
		//推荐使用迭代器遍历,效率高
		//HasSed无序且不可重复
		//LinkedHasSed有序不可重复
        Iterator ite = set.iterator();
        while (ite.hasNext()){
            String next = ite.next();
            System.out.println(next);
        }}}
      
    运行:
		[你好1, 你好2, 你好3, 你好4, 你好0]
		你好1
		你好2
		你好3
		你好4
		你好0

		你好1
		你好2
		你好3
		你好4
		你好0          
2、LinkedHashSet

有序不可重复,按照新增的顺序储存,

public class HasMap {
    @Test
    public void test01() {
        Set set = new LinkedHashSet();
        byte a = 1;
        short b = 2;
        int c = 3;
        long d = 4l;
        double e = 5.0d;
        float f = 6.0f;
        char g = 'a';
        boolean h = false;
        String i = "你好java";

        set.add(a);
        set.add(b);
        set.add(c);
        set.add(d);
        set.add(e);
        set.add(f);
        set.add(g);
        set.add(h);
        set.add(i);
        System.out.println(set);//长度
}
运行:
	[1, 2, 3, 4, 5.0, 6.0, a, false, 你好java]
!3、TreeSet

用compareTo比较排序


@Data
public class Books implements Comparable{
    String name;
    String author;
    int price;
    int sales;

    public Books(String name, String author, int price, int sales) {
        
        this.name = name;
        
        this.author = author;
        
        this.price = price;
        
        this.sales = sales;
    }

    @Override
    public int compareTo(Object o) {
        Books books = (Books) o;
        int x =this.author.compareTo(books.getAuthor());
        int y =this.price-books.getPrice();
        int z =this.sales-books.getSales();
        int k =this.name.compareTo(books.getName());
        if (x != 0){
            return x;
        }else if(y != 0){
            return  y;
        }else if (z != 0){
            return  -z;
        }else {
            return k;
        }

    }
}

public class Terrset {
    @Test
    public void test01(){
        SortedSetbooks=new TreeSet<>();
        books.add(new Books("一步之遥","姜文",23,200));
        books.add(new Books("二步之遥","姜武",23,201));
        books.add(new Books("三步之遥","姜文",33,200));
        books.add(new Books("四步之遥","姜武",23,240));
        books.add(new Books("五步之遥","姜文",33,100));
         Iterator book = books.iterator();
         while (book.hasNext()){
             Books book2 = book.next();
             System.out.println(book2);
         }
    }

}
//--------------------------------------------------

运行:
	Books(name=一步之遥, author=姜文, price=23, sales=200)
	Books(name=三步之遥, author=姜文, price=33, sales=200)
	Books(name=五步之遥, author=姜文, price=33, sales=100)
	Books(name=四步之遥, author=姜武, price=23, sales=240)
	Books(name=二步之遥, author=姜武, price=23, sales=201)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/842003.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号