zoukankan      html  css  js  c++  java
  • Set的用法

    java中Set的用法

     

    Set集合的特点:

    不能存储相同的元素。

    同时因为其是一个抽象的接口:所以不能直接实例化一个set对象。(Set s = new Set() )错误

    该接口主要继承于Collections接口,所以具有Collection的一些常见的方法。

    Sr.No.Method & Description
    1

    add( )         向集合中添加元素

    2

    clear( )        去掉集合中所有的元素

    3

    contains( )    判断集合中是否包含某一个元素

    4

    isEmpty( )    判断集合是否为空

    5

    iterator( )    主要用于递归集合,返回一个Iterator()对象

    6

    remove( )    从集合中去掉特定的对象

    7

    size( )        返回集合的大小

    Set接口 常用实现:HashSet  TreeSet

    TreeSet:会将里面的元素默认排序

    Set<Integer> test = new TreeSet<>();
    int a = 1;
    int b = 8;
    int c = 3;
     
    test.add(a);
    test.add(b);
    test.add(c);
     
     //遍历集合test   利用foreach遍历          //输出结果:1   3   8    
     for (Integer value : test) {
         System.out.print(value+" ");
     }    
    

    HashSet:

    添加元素:

    hashset.add(E e):返回boolean型,如果此 set 中尚未包含指定元素,则添加指定元素;如果此 set 已包含该元素,则该调用不更改 set 并返回 false。

    示例:if 中循环添加到hs中  元素不存在最终返回 false  遇到重复时 最终返回true

     1 import java.util.*;  
     2 public class FirstRepeat {  
     3     public static char findFirstRepeat(String A, int n) {  
     4       
     5     char[] a=A.toCharArray();  
     6     HashSet hs=new HashSet<>();  
     7     for(int i=0; i<n;i++)   
     8     {  
     9         if (!hs.add(a[i]))   
    10         {  
    11             return a[i];  
    12         }  
    13     }  
    14     return 0;  
    15     }  
    16   
    17     public static void main(String[] args)  
    18     {  
    19         System.out.println(findFirstRepeat("qywyer23tdd",11));  
    20     }  
    21 }  

    我们使用Set存储的是不重复的对象,对象重复与否是根据 hashCode 和 equals进行判断,所以Set存储的对象必须重写这两个方法

    1:只要重写equals,就必须重写hashCode

    2:如果自定义对象作为Map的键,那么该对象必须重写hashCode和equals

    (说明:String 重写了 hashCode 和 equals 方法,所以我们可以非常愉快地使用 String 对象 作为 key 来使用。)

    String 的源码如下

     public boolean equals(Object anObject) {
            if (this == anObject) {
                return true;
            }
            if (anObject instanceof String) {
                String anotherString = (String)anObject;
                int n = value.length;
                if (n == anotherString.value.length) {
                    char v1[] = value;
                    char v2[] = anotherString.value;
                    int i = 0;
                    while (n-- != 0) {
                        if (v1[i] != v2[i])
                            return false;
                        i++;
                    }
                    return true;
                }
            }
            return false;
        }
     public int hashCode() {
            int h = hash;
            if (h == 0 && value.length > 0) {
                char val[] = value;
    
                for (int i = 0; i < value.length; i++) {
                    h = 31 * h + val[i];
                }
                hash = h;
            }
            return h;
        }

    下面我们创建一个Bean来测试一下

    public class Bean {
        private int id;
        private String name;
        public Bean() {
            super();
        }
        public Bean(int id, String name) {
            super();
            this.id = id;
            this.name = name;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        @Override
        public boolean equals(Object obj) {
            if(!(obj instanceof Bean)) {
                return false;
            }
            Bean b = (Bean)obj;
            if(this.id == b.id) {
                return true;
            }
            return false;
        }
        
        @Override
        public int hashCode() {
            return id;
        }
    }
        @Test
        public void test01() {
            Set<Bean> set = new HashSet<>();
            Bean b1 = new Bean(1,"123");
            Bean b2 = new Bean(2,"456");
            set.add(b1);
            set.add(b2);
            Bean b3 = new Bean(1,"789");
            Bean b4 = new Bean(2,"012");
            set.add(b3);
            set.add(b4);
            System.out.println(set.size()+"元素不重复");
        }
        }

    运行 结果是  sizi 为 2  元素不重复   如果不重写 hashCode  和 equals 方法   set.size 就为4  元素是重复的

  • 相关阅读:
    Centos给php安装cassandra扩展
    树莓派配置文档 config.txt 说明(转)
    ubuntu远程桌面连接windows系统(转)
    Shell学习笔记
    linux终端terminal个性化配置(转)
    ubuntu14.04安装bodhi桌面系统后,unity启动界面改变,如何还原
    man curl_easy_perform(原创)
    man curl_easy_setopt(原创)
    树莓派 config.txt
    使用dd命令克隆整个系统(转)
  • 原文地址:https://www.cnblogs.com/Vegeta/p/8759262.html
Copyright © 2011-2022 走看看