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

JAVA入门学习 —集合(Set使用)

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

JAVA入门学习 —集合(Set使用)

前言

集合中List和Set的区别:

  • List:有序可重复,有序指的是下标有序 、可重复指的是不同下标位置上可以存储相同值的元素。
  • Set:无序不可重复,录入的顺序和存储的顺序不一致、不可重复指的是相同值的元素不可重复存储。
show code
 @Test
    public void test() {
        Set set = new HashSet();
        byte a = 1;
        short b = 2;
        int c = 3;
        long d = 4l;
        double e = 5.0;
        float f = 6.0f;
        char g = 'a';
        boolean h = true;
        String i = "路飞";
        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);
        set.add(a);
        System.out.println("该集合长度为 >>> " + set.size());
        System.out.println(set);
    }


       我们发现set创建的集合无序所以无法使用下标读取元素,那么遍历元素应该选择加强for循环和迭代器遍历,如下:

 @Test
    public void test01(){
        Set set = new HashSet();
        //创建一个String类型的集合
        for (int i = 0; i < 10; i++) {
            set.add("路飞"+i);
        }
        //使用for each遍历集合
        for (String name:set
             ) {
            System.out.print(name + "t");
        }
    }

 @Test
    public void test02(){
        Set set = new HashSet();
        //创建一个String类型的集合
        for (int i = 0; i < 10; i++) {
            set.add("索隆"+i);
        }
        //使用迭代器遍历集合
        Iterator it = set.iterator();
        while(it.hasNext()){
            String next = it.next();
            System.out.print(next + "t");
        }
    }


       既然HashSet是创建无序的集合,那么就会有有序的集合,如下(使用LinkedHashSet):

 @Test
    public void test() {
        Set set = new LinkedHashSet();
        byte a = 1;
        short b = 2;
        int c = 3;
        long d = 4l;
        double e = 5.0;
        float f = 6.0f;
        char g = 'a';
        boolean h = true;
        String i = "路飞";
        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);
        set.add(a);
        System.out.println("该集合长度为 >>> " + set.size());
        System.out.println(set);
    }


       接下来看一下另外一个排序的set使用(TreeSet):
正序:
先创建一个books类:

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

    @Override
    public int compareTo(Object o) {
        Books book = (Books) o;
        return 1;
    }
}

创建一个books的测试类:

 @Test
    public void test02(){
        Set books = new TreeSet<>();
        books.add(new Books("浮城谜事","姜烨",35,150));
        books.add(new Books("邪不压正","姜文",35,150));
        books.add(new Books("推拿","姜烨",33,150));
        books.add(new Books("让子弹飞","姜文",33,150));
        books.add(new Books("颐和园","姜烨",35,180));
        books.add(new Books("一步之遥","姜文",35,180));
        Iterator it = books.iterator();
        while(it.hasNext()){
            Books next = it.next();
            System.out.println(next);
        }
    }


倒序:
先创建一个books类:

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

    @Override
    public int compareTo(Object o) {
        return -1;
    }
}

创建一个books的测试类:

@Test
    public void test02(){
        Set books = new TreeSet<>();
        books.add(new Books("浮城谜事","姜烨",35,150));
        books.add(new Books("邪不压正","姜文",35,150));
        books.add(new Books("推拿","姜烨",33,150));
        books.add(new Books("让子弹飞","姜文",33,150));
        books.add(new Books("颐和园","姜烨",35,180));
        books.add(new Books("一步之遥","姜文",35,180));
        Iterator it = books.iterator();
        while(it.hasNext()){
            Books next = it.next();
            System.out.println(next);
        }
    }


可见:顺序由正负数而定(正数为正序,负数为倒序)
自定义排序:
先创建一个books类:

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

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

创建一个books的测试类:

 @Test
    public void test02() {
        Set books = new TreeSet<>();
        books.add(new Books("浮城谜事", "姜烨", 35, 150));
        books.add(new Books("邪不压正", "姜文", 35, 150));
        books.add(new Books("推拿", "姜烨", 33, 150));
        books.add(new Books("让子弹飞", "姜文", 33, 150));
        books.add(new Books("颐和园", "姜烨", 35, 180));
        books.add(new Books("一步之遥", "姜文", 35, 180));
        Iterator it = books.iterator();
        while (it.hasNext()) {
            Books next = it.next();
            System.out.println(next);
        }
    }

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

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

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