Java 集合-Set接口简析

doc中Set接口的描述如下:

public interface Set
extends Collection;// 继承了Collection接口的所有操作;
A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
常用子类:
1.public class HashSet
extends AbstractSet
implements Set, Cloneable, Serializable;// 散列表,无顺序!
—–HashSet输出:
package com.mldn;
import .util.Set;
import .util.HashSet;
public class HashSetDemo
{
        public static void main(String[] args)
        {
                Set hash = null;        // 声明一个Set
                hash = new HashSet();// 实例化空的散列集合
                hash.add(“a”);
                hash.add(“b”);
                hash.add(“c”);
                hash.add(“d”);
                System.out.println(hash);        // 调用toString()
        }
}
/*
administrator@xu-desktop:~$ com.mldn.HashSetDemo
[d, b, c, a] // 不是按照添加时的顺序输出的!
*/
——TreeSet输出:
package com.mldn;
import .util.Set;
import java.util.TreeSet;
public class TreeSetDemo
{
        public static void main(String[] args)
        {
                Set tree = null;        // 声明一个Set
                tree = new TreeSet();// 实例化空的散列集合
                tree.add(“a”);
                tree.add(“b”);
                tree.add(“c”);
                tree.add(“d”);
                System.out.println(tree);        // 调用toString()
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.TreeSetDemo
[a, b, c, d]        // TreeSet是有序存储,
*/

———Set接口排序原理:
0.Set类型:定义上不允许保存重复的元素!
1.TreeSet类排序:前提,泛型类必须实现Comarable接口,元素是否重复根据compareTo方法定义的规则判断,重复的元素添加将失败!
2.HashSet类排序:泛型类必须覆写来自Object类的hashCode(),equals(Object obj)方法;将根据hashCode,equals判断是否重复从而不保存重复的元素;
3.String类默认覆写了hashCode(),equals(Object obj),toString();方法;也实现了Comparable接口;
——–TreeSet排序原理分析:
1.对泛型实现Comparable接口,但只实现部分属性的比较:
package com.mldn;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetSort
{
        public static void main(String[] args)
        {
                Set tree = new TreeSet();        // 实例化TreeSet实例
                boolean[] f = new boolean[10];
                for (int x = 0; x                 {
                        f[x] = false;
                }               
                // 添加不同的元素
                f[1] = tree.add(new People(“张三”, 21));        // 添加元素
                f[2] = tree.add(new People(“李三”, 23));        // 添加元素
                f[3] = tree.add(new People(“王三”, 22));        // 添加元素
                // 添加相同的元素
                f[4] = tree.add(new People(“赵三”, 20));        // 添加元素
                f[5] = tree.add(new People(“赵三”, 20));        // 添加元素        // f[5] == f[6] == false: 说明Set不保存重复的元素,添加失败!
                f[6] = tree.add(new People(“赵三”, 20));        // 添加元素
                // 添加姓名相同的元素
                f[7] = tree.add(new People(“孙三”, 28));        // 添加元素
                f[8] = tree.add(new People(“孙三”, 25));        // 添加元素
                //添加年龄相同的元素
                f[0] = tree.add(new People(“周三”, 29));        // 添加元素
                f[9] = tree.add(new People(“毛三”, 29));        // 添加元素 // f[9] == false : 说明Set不保存重复的元素,添加失败!
                // TreeSet 判断元素是否重复的标准:泛型对Comparable的实现,这里People类对比较器的实现不全面:没有对name属性进行比较,所以f[9] 操作失败!应该加入所有属性的定义;
                for (int x = 0; x                 {
                        System.out.println(x + ” ” + f[x]);
                }
                System.out.println(“该集合的长度:” + tree.size());
                System.out.println(tree);        // 输出按TreeSet排序的元素
        }
}
class People implements Comparable
{
        private String name = null;        // 姓名
        private int age = 0;        // 年龄
        public People(String name, int age)
        {
                this.name = name;
                this.age = age;
        }
        public int compareTo(People o)
        {
                if (this.age > o.age)
                {
                        return 1;
                }
                else if (this.age                 {
                        return -1;
                }
                else
                {
                        return 0;
                }
        }
        public String toString()
        {
                return “姓名:” + this.name + ” 年龄:” + this.age;
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.TreeSetSort
0 true
1 true
2 true
3 true
4 true
5 false        // 失败操作, 是重复元素
6 false        // 失败操作,是重复的
7 true
8 true
9 false        // 失败操作,不是真的重复,只是实现Comparable时定义了age,没有考虑name,这里name不同,不算重复!
该集合的长度:7
[姓名:赵三 年龄:20, 姓名:张三 年龄:21, 姓名:王三 年龄:22, 姓名:李三 年龄:23, 姓名:孙三 年龄:25, 姓名:孙三 年龄:28, 姓名:周三 年龄:29]
*/
———–重新定义泛型对Comparable的实现,对所有属性进行全面合理的比较!
package com.mldn;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetSort
{
        public static void main(String[] args)
        {
                Set tree = new TreeSet();        // 实例化TreeSet实例
                boolean[] f = new boolean[10];
                for (int x = 0; x                 {
                        f[x] = false;
                }               
                // 添加不同的元素
                f[1] = tree.add(new People(“张三”, 21));        // 添加元素
                f[2] = tree.add(new People(“李三”, 23));        // 添加元素
                f[3] = tree.add(new People(“王三”, 22));        // 添加元素
                // 添加相同的元素
                f[4] = tree.add(new People(“赵三”, 20));        // 添加元素
                f[5] = tree.add(new People(“赵三”, 20));        // 添加元素        // f[5] == f[6] == false: 说明Set不保存重复的元素,添加失败!
                f[6] = tree.add(new People(“赵三”, 20));        // 添加元素
                // 添加姓名相同的元素
                f[7] = tree.add(new People(“孙三”, 28));        // 添加元素
                f[8] = tree.add(new People(“孙三”, 25));        // 添加元素
                //添加年龄相同的元素
                f[0] = tree.add(new People(“周三”, 29));        // 添加元素
                f[9] = tree.add(new People(“毛三”, 29));        // 添加元素 // f[9] == false : 说明Set不保存重复的元素,添加失败!
                // TreeSet 判断元素是否重复的标准:泛型对Comparable的实现,这里People类对比较器的实现不全面:没有对name属性进行比较,所以f[9] 操作失败!应该加入所有属性的定义;
                for (int x = 0; x                 {
                        System.out.println(x + ” ” + f[x]);
                }
                System.out.println(“该集合的长度:” + tree.size());
                System.out.println(tree);        // 输出按TreeSet排序的元素
        }
}
class People implements Comparable
{
        private String name = null;        // 姓名
        private int age = 0;        // 年龄
        public People(String name, int age)
        {
                this.name = name;
                this.age = age;
        }
        public int compareTo(People o)
        {
                if (this.age > o.age)       
                {
                        return 1;
                }
                else if (this.age                 {
                        return -1;
                }
                else        // 年龄相等,比较姓名
                {
                        return this.name.compareTo(o.name);        // String实现了Comparable接口
                }
        }
        public String toString()
        {
                return “姓名:” + this.name + ” 年龄:” + this.age;
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.TreeSetSort
0 true
1 true
2 true
3 true
4 true
5 false        // 所有属性相同,是重复!
6 false
7 true
8 true
9 true        // 虽然年龄相同,但姓名不同,不是重复元素!
该集合的长度:8
[姓名:赵三 年龄:20, 姓名:张三 年龄:21, 姓名:王三 年龄:22, 姓名:李三 年龄:23, 姓名:孙三 年龄:25, 姓名:孙三 年龄:28, 姓名:周三 年龄:29, 姓名:毛三 年龄:29]
*/

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《Java 集合-Set接口简析
本文地址:https://www.zhiletu.com/archives-116.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微