JAVA----集合----set用法

List与set区别:

1.List:有序可重复。有序指:按照新增的顺序存储。可重复:存储元素的值可以重复。

2.set:无序不可重复。录入的顺序和存储的顺序不一致。不可重复:相同值的元素不可重复存储。

 一:使用set构建集合,并赋值读取。

   @Test
    public void test0(){
        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="鸿鹄之志";
        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.size()+"个元素");
        System.out.println(set);
    }

结果为:

 二:迭代器与foreach两种方式遍历用set写法构建的集合。

        1.foreach遍历用set写法构建的集合。(因为set创建的集合没有下标,所以无序,所以只能foreach遍历)

  @Test
    public void test02() {
        Set<String> set = new HashSet<>();
        for (int i = 0; i < 5; i++) {
            set.add("浩楠" + (i + 1));
        }
        System.out.println(set);
        for (String name : set) {
            System.out.println(name);
        }
    }

 结果为:

        2. 迭代器遍历用set写法构建的集合。

 @Test
    public void test03(){
            Set<String> set = new HashSet<>();
            for (int i = 0; i < 5; i++) {
                set.add("浩楠" + (i + 1));
            }
            System.out.println(set);
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                String name = it.next();
                System.out.println(name);
            }

        }

结果为:

   三: HashSet是创建无序的集合,那么有有序的集合吗?

           答案是有,如下(使用LinkedHashSet创建集合):LinkedHashSet是HashSet的子类

   @Test
        public void test04(){
            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 = 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);
            System.out.println("该集合中有" + set.size() + "个元素");
            System.out.println(set);
        }

结果是:

 四:除了HashSet能创建集合外,TreeSet也能创建集合

        但TreeSet有点特殊,它可以自定义集合内元素的排序规则。

        例如:我们先创建一个Book类,标明属性(正着排序)

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

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

        再写一个测试类:

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

        结果是:

        我们先创建一个Book类,标明属性(倒着排序) 

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

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

        写一个测试类:

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

         结果是:

不难发现:return值为正数,正着排序; return值为负数,倒着排序。

自定义排序:

        我们先创建一个Book类,标明属性(自定义排序) 

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

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

        测试:

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

        结果为:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星星不喝药

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值