Set

Set集合存储唯一无序的数据

public class SetTest {
    /**
     * Set:
     * 01.存贮的是唯一,无序的数据
     * 02.hashSet是常用的实现类
     * 03.存放的是对象的引用
     *   HashSet底层是hashMap实现的!
     *    01.hashSet在添加一个数据的时候,其实是把这个值当成hashMap的key来进行保存的!
     *    02.hashMap地城是通过初始化一个Entry数组来实现key,value保存数据的!
     *    03.hashMap中的Entry有四个属性变量!  key value next hash
     *    04.在hashMap中添加数据的步骤
     *       001.对key的hashCode进行hash运算,得到的值就是对应的index
     *       002.判断index所指向的数组元素是否为空,如果为空,直接插入数据
     *       003.如果不为空,依次查询entry中的next所指定的元素,
     *           判断key是否相等,如果相等,直接覆盖
     *       004.如果key不相等,通过next变量将值插入Entry数组中
* 04.如果a.equals(b)返回的是true,那么a和b的hashCode一定相等
   * 05.如果a.equals(b)返回的是false,那么a和b的hashCode有可能相等
*/ public static void main(String[] args) { Set set = new HashSet(); set.add("1"); set.add(new String("1")); System.out.println(set.size()); // 1 } }

Set集合测试类

public class SetTest {
    public static void main(String[] args) {
        /*
         * Set接口存储的是唯一(不能重复),无序(新增,删除,查询)对象的集合!
         * HashSet是Set常用的实现类
         * Set存放的是对象的引用
         */
        //创建一个Set集合
        Set s=new HashSet();
        //给集合赋值
        s.add("");    //空串
        s.add(null);  //空值
        s.add(new String("abc"));
        System.out.println(s.size());  // 3
        
        Set set=new HashSet();
        set.add(new News(1, "新闻1"));
        set.add(new News(2, "新闻2"));
        set.add(new News(3, "新闻3"));
        System.out.println(set.size()); //3
        for (Object object : set) {
            System.out.println(object);
        }
    
        Set set1=new HashSet();
        set1.add(new String("abc"));
        set1.add(new String("abc"));
        set1.add(new String("abc"));
        System.out.println(set1.size()); //1
        
        String  a ="abc";
        String  b =new String("abc");
        String  c =new String("abc");
        System.out.println(a==b);  //false
        System.out.println(c==b); //false
        //具有相同内容的String对象,hashCode是一致的!
        System.out.println(b.hashCode()==c.hashCode());  //T
        
        String str1="a";
        String str2=new String("a");
        System.out.println(str1==str2); //false  内存地址不一样
        System.out.println(str1.equals(str2)); //true  内容一样
        System.out.println(str1.hashCode()==str2.hashCode()); //T
    }
}

TreeSet集合

public class TreeTest {
    public static void main(String[] args) {
        /*
         * 1. HashSet是通过HashMap实现的,TreeSet是通过TreeMap实现的,只不过Set用的只是Map的key
         * 2. Map的key和Set都有一个共同的特性就是集合的唯一性.TreeMap更是多了一个排序的功能.
         * 3. hashCode和equal()是HashMap用的, 因为无需排序所以只需要关注定位和唯一性即可.
         * 4. 由于TreeMap需要排序,所以需要一个Comparator为键值进行大小比较.
         */
        Set set1=new HashSet();
        set1.add("z");
        set1.add("a");
        set1.add("y");
        set1.add("b");
        for (Object object : set1) {
            System.out.println(object);
        }
        
        System.out.println("*******************");
        TreeSet set2=new TreeSet();  //自动排序
        set2.add("z");
        set2.add("a");
        set2.add("y");
        set2.add("b");
        for (Object object : set2) {
            System.out.println(object);
        }
    }
}
原文地址:https://www.cnblogs.com/areyouready/p/6834805.html