HashSet

简介: HashSet

集合(Set)

HashSet

简介:本文旨在用最简洁的篇幅,快速熟悉使用HashMap。

声明方式

public static void main(String[] args) {
    // 方式一 子类实现方式创建
    Set<Integer> set = new HashSet<Integer>();
    set.add(1);
    set.add(3);
    set.add(1);
    set.add(2);
    set.add(4);
    set.add(100);
    set.add(-100);
    set.add(2);
    System.out.println(set);
    // 这个结果无序 且去重
    /*[1, 2, 3, -100, 4, 100]*/
    // 方式二 同一等级类的方式创建
    HashSet<Integer> set = new HashSet<Integer>();
    set.add(1);
    set.add(3);
    set.add(1);
    set.add(2);
    set.add(4);
    set.add(100);
    set.add(-100);
    set.add(2);
    System.out.println(set);
    // 这个结果无序 且去重
    /*[1, 2, 3, -100, 4, 100]*/
  }

常用方法

boolean add()添加字符 可以通过这个方法来判断一个字符是否在哈希表中存在

public static void main(String[] args) {
    HashSet<Integer> set = new HashSet<Integer>();
    // 第一次添加成功 第二次添加失败
    System.out.println(set.add(1));
    /*true*/
    System.out.println(set.add(1));
    /*false*/
  }

void clear() 清空集合

public static void main(String[] args) {
    HashSet<Integer> set = new HashSet<Integer>();
    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);
    System.out.println(set);
    /*[1, 2, 3, 4]*/
    set.clear();
    System.out.println(set);
    /*[]*/
  }

Object clone 克隆函数 浅拷贝

public static void main(String[] args) {
    HashSet<Integer> set = new HashSet<Integer>();
    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);
    System.out.println(set);
    /*[1, 2, 3, 4]*/
    // 克隆的时候要注意 就是需要 加上对应的强制转换
    HashSet<Integer> set2 = (HashSet<Integer>)set.clone();
    System.out.println(set2);
    /*[1, 2, 3, 4]*/
  }

boolean isEmpty() add(),isEmpty(),size,(),iterator(),clear(),clone() 这几个函数基本是所有容器都有的而且作用还是一样的

后面就不会一直写了

boolean contains(Object o) 判断是否包含这个元素

public static void main(String[] args) {
    HashSet<Integer> set = new HashSet<Integer>();
    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);
    System.out.println(set.contains(1));
    /*true*/
  }

boolean remove(Object o) 删除指定元素 返回是否删除成功 也可以用来判断元素是否在集合里面

public static void main(String[] args) {
    HashSet<Integer> set = new HashSet<Integer>();
    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);
    System.out.println(set.remove(1));    
    /*true*/
    System.out.println(set);
    /*[2, 3, 4]*/
  }

遍历方式

public static void main(String[] args) {
    HashSet<Integer> set = new HashSet<Integer>();
    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);
    // 方法一    
    for(Integer it : set)
    {
      System.out.print(it + " ");
    }
    /*1 2 3 4 */
    System.out.println();
    // 方法二
    Iterator<Integer> it = set.iterator();
    while(it.hasNext())
    {
      System.out.print(it.next() + " ");
    }
    /*1 2 3 4 */
  }

排序

HashSet不能直接进行排序 需要借助其他容易来实现 这里引出JavaSTL 中的一个很厉害的特性 容器之间的相互转换

public static void main(String[] args) {
    HashSet<Integer> set = new HashSet<>();
    set.add(-1);
    set.add(56464);
    set.add(3);
    set.add(2);
    set.add(4);
    set.add(0);
    System.out.println(set);
    /*[-1, 56464, 0, 2, 3, 4]*/
    System.out.println();
    // 转换成List类型之后排序
    List<Integer> list = new ArrayList<Integer>(set);
    Collections.sort(list);
    System.out.println(list);
    /*[-1, 0, 2, 3, 4, 56464]*/
  }


相关文章
|
28天前
|
存储
HashSet的使用
HashSet的使用
26 2
|
2月前
|
存储
HashSet和LinkedHashSet使用
HashSet和LinkedHashSet使用
|
存储 算法 安全
|
存储 对象存储
HashSet、TreeSet、LinkedHashSet的区别
HashSet、TreeSet、LinkedHashSet的区别
82 0
|
存储 算法
TreeSet 和 HashSet 的区别
TreeSet 和 HashSet 的区别
56 0
|
存储 Java
使用Set集合及HashSet,TreeSet
使用Set集合及HashSet,TreeSet
58 0
|
存储 安全
HashSet和HashMap
HashSet和HashMap
114 0
|
存储 安全 容器
HashSet详解
HashSet详解
161 0
HashSet详解
|
存储 安全 API
TreeSet详解
TreeSet详解
139 0
TreeSet详解
|
存储
Set集合和其之类HashSet、LinkedHashSet
Set集合和其之类HashSet、LinkedHashSet