ArrayList嵌套

定义

在集合中存放集合,和二维数组类似

public static void main(String[] args) {

//集合中的元素还是集合

ArrayList<ArrayList<Student>> school = new ArrayList<>();

ArrayList<Student> clas1 = new ArrayList<>();

clas1.add(new Student("小红",18));

clas1.add(new Student("小明", 20));

school.add(clas1);

ArrayList<Student> clas2 = new ArrayList<>();

clas2.add(new Student("小张",18));

clas2.add(new Student("小李", 20));

school.add(clas2);

ArrayList<Student> clas3 = new ArrayList<>();

clas3.add(new Student("tom",18));

clas3.add(new Student("jack", 20));

school.add(clas3);

System.out.println(school);

}

Set集合

定义

Set集合存数的元素是无序的, 而且不允许储存重复的元素, 每当有新的元素存入的时候,Set集合会先去过滤, 如果发现和集合中现有元素出现重复, 就不在允许添加

应用场景

当我们不想让集合中出现重复的元素时,使用Set集合

当我们需要排除重复数据时,使用Set集合

演示

public static void main(String[] args) {

//集合中的元素还是集合

HashSet<Student> s = new HashSet<>();

Student student = new Student("小红", 18);

Student student2 = new Student("小红", 18);

s.add(student);

s.add(student2);

Iterator<Student> it = s.iterator();

while (it.hasNext()) {

Student student3 = it.next();

System.out.println(student3);

}

}

HashSet

定义

HashSet集合中的元素是通过hash值来比较是否相同

集合通过元素的hashCode和equals方法来比较两个元素是否相同, 不同就存入, 相同不存入

元素存入的位置未知,和存入的顺序无关

存储原理

HashSet最后还是存入数组中, 只是根据元素的Hash值来确定存入的角标位置

元素的hash值 ^ (元素的hash值 >>> 16) & (数组的长度-1)

HashSet中不是直接存入我们给定的元素, 而是用集合中的一个内部类封装我们存入的元素,所以当我们存入null的时候,不会因为和数组中角标位上默认值null起冲突

如果两个不同的元素根据不同的Hash值计算出了同一个角标值,那么都能存进去

构造方法

HashSet()    构造出一个新的集合, 底层数组默认的初始容量是16(扩容一倍),加载因子是0.75

加载因子: 集合中的数组可用的

角标值得可选范围越小,计算出重复角标值得概率就越高

HashSet(Collection<? extends E> c) 构造一个包含指定collection中元素的新set 

常用方法

boolean add( E e)    如果此set集合中尚未包含指定元素,则添加指定元素

boolean remove(Object o)    移除某个元素 []

int size()    获取集合的长度

演示

需求: 去除ArrayList集合中的重复元素

publicstaticvoidmain(String[]args) {

    ArrayList<String>al=newArrayList<>();

    al.add("a");

    al.add("s");

    al.add("x");

    al.add("a");

    al.add("s");


    HashSet<String>hs=newHashSet<>(al);

    System.out.println(hs);

}

public static void main(String[] args) {

ArrayList<String> al = new ArrayList<>();

al.add("a");

al.add("s");

al.add("x");

al.add("a");

al.add("s");

HashSet<String> hs = new HashSet<>(al);

System.out.println(hs);

}

HashSet集合中元素保证唯一性

原理解析

HashMap是通过调用元素的hashCode方法来比较两个元素是否形同的,所有如果要保证引用数据类型逻辑上的唯一性,就必须重写hashCode方法和equals方法

演示

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + age;

result = prime * result + ((name == null) ? 0 : name.hashCode());

return result;

}

prime = 31 的原因

别人选的, 没有原因

是质数, 和别的值计算得出的数重复的概率低

大小适中, 不会出现太大导致结果无法使用的问题

便于计算 2<<5 -1

引用数据类型除重

重写hashCode和equals方法,自定义比较内容

LinkedHashSet

定义

兼顾了linked的有序性和HashSet的元素唯一性

public static void main(String[] args) {

HashSet<String> hashSet = new HashSet<>();

hashSet.add("b");

hashSet.add("c");

hashSet.add("a");

hashSet.add("d");

System.out.println(hashSet); //结果 : [a, b, c, d]

LinkedHashSet<String> lhs = new LinkedHashSet<>();

lhs.add("b");

lhs.add("c");

lhs.add("a");

lhs.add("d");

System.out.println(lhs); //结果: [b, c, a, d]

}



TreeSet集合

定义

TreeSet是一种顺序的集合, 记住, 这里的顺序是指集合中的元素有顺序, 她是通过比较元素的大小来存放的, 大的存右边,小的存左边

存入的元素必须实现Comparable接口,并且重写comparTo方法

最后存入的元素会形成一个树状结构

构造方法

TreeSet()    构造一个新的空set, 该set根据其元素的自然顺序进行排序

TreeSet(Comparator <? super E> comparator)  构建一个空的TreeSet, 他根据指定比较器进行排序

常用方法                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         

add(E e)    将指定元素添加到此set

first()    返回此set中当前第一元素

last()    返回此set中当前最后一个元素

floor()    返回此set中小于等于给定元素的最大元素,不存在则返回null

higher()    返回此set中严格大于给定元素的最小元素,不存在则返回null

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 四、集合框架 1:String类:字符串(重点) (1)多个字符组成的一个序列,叫字符串。生活中很多数据的描述都采...
    佘大将军阅读 771评论 0 2
  • 17.01_集合框架(HashSet存储字符串并遍历) A:Set集合概述及特点通过API查看即可 B:案例演示H...
    dreamkong阅读 488评论 0 0
  • 导语: 最近学习了集合,认为有必要做一下总结。希望能够帮助自己的同时,帮助到更多人。本文内容较长,如有错误,还请指...
    我是小徐同学阅读 637评论 0 8
  • 面向对象主要针对面向过程。 面向过程的基本单元是函数。 什么是对象:EVERYTHING IS OBJECT(万物...
    sinpi阅读 1,082评论 0 4
  • 1.import static是Java 5增加的功能,就是将Import类中的静态方法,可以作为本类的静态方法来...
    XLsn0w阅读 1,261评论 0 2