目录
    • java集合体系结构:
    • 1、list
      • 1)arraylist
      • 2)linkedlist
    • 1)hashset
      • 2)treeset

      一、概述

      集合是一种长度可变,存储数据的数据结构多样,存储对象多样的一种数据容器。java中集合可分为:list集合、set集合、hashmap集合,等。

      java集合体系结构:

      二、collection

      collection是java中所有值存储集合的顶级接口,因此它的所有直接或者间接实现类都有它的非私有方法,我们可以从它的方法开始了解这个体系的功能实现。

       boolean add(e e) 
                确保此 collection 包含指定的元素。 
       boolean addall(collection<? extends e> c) 
                将指定 collection 中的所有元素都添加到此 collection 中。 
       void clear() 
                移除此 collection 中的所有元素。 
       boolean contains(object o) 
                如果此 collection 包含指定的元素,则返回 true。 
       boolean containsall(collection<?> c) 
                如果此 collection 包含指定 collection 中的所有元素,则返回 true。 
       boolean equals(object o) 
                比较此 collection 与指定对象是否相等。 
       int hashcode() 
                返回此 collection 的哈希码值。 
       boolean isempty() 
                如果此 collection 不包含元素,则返回 true。 
       iterator<e> iterator() 
                返回在此 collection 的元素上进行迭代的迭代器。 
       boolean remove(object o) 
                从此 collection 中移除指定元素的单个实例,如果存在的话)。 
       boolean removeall(collection<?> c) 
                移除此 collection 中那些也包含在指定 collection 中的所有元素。 
       boolean retainall(collection<?> c) 
                仅保留此 collection 中那些也包含在指定 collection 的元素。 
       int size() 
                返回此 collection 中的元素数。 
       object[] toarray() 
                返回包含此 collection 中所有元素的数组。 
      <t> t[] 
       toarray(t[] a) 
                返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。 
      

      1、list

      list,是单列集合,存储的是一组插入有序的数据,并且数据可以重复。

      list集合

      • linkedlist
      • arraylist

      1)arraylist

      示例:

      public class collectiontest {
          public static void main(string[] args) {
              list list = new arraylist();
              //添加元素,boolean add(e e) 确保此 collection 包含指定的元素
              list.add("张三");
              list.add(1);
              list.add('a');
              system.out.println(list);//[张三, 1, a]
              //boolean addall(collection<? extends e> c)
              //          将指定 collection 中的所有元素都添加到此 collection 中
              list list1 = new arraylist();
              list.add("java");
              list.add("mysql");
              list.addall(list1);
              system.out.println(list);//[张三, 1, a, java, mysql]
              //boolean contains(object o)
              //          如果此 collection 包含指定的元素,则返回 true。
              system.out.println(list.contains("java"));//true
              //boolean remove(object o)
              //          从此 collection 中移除指定元素的单个实例,如果存在的话)。
              system.out.println(list.remove("java"));//true
              // int size()
              //          返回此 collection 中的元素数。
              system.out.println(list.size());//4
              //set(int index, e element)
              //          用指定的元素替代此列表中指定位置上的元素。
              //并返回被修改的值
              system.out.println(list.set(1, "李四"));
              //get(int index) 
              //          返回此列表中指定位置上的元素。
              system.out.println(list.get(1));
              // iterator<e> iterator()
              //          返回在此 collection 的元素上进行迭代的迭代器。
              //集合的遍历
              iterator iterator = list.iterator();
              while (iterator.hasnext()){
                  system.out.println(iterator.next());
              }
          }
      

      说明:arraylist底层是使用数组的形式创建集合的,因此基于数组的特性,此集合对数据的查找很快速,但是在删除或移动大量数据操作上会显得缓慢。它适合用于快速查找,但不适合做删除多的操作。

      2)linkedlist

      linkedlist:双向链表,内部没有声明数组,而是定义了node类型的first 和last,用于记录首末元素。同时,定义内部类node,作为linkedlist中 保存数据的基本结构。node除了保存数据,还定义了两个变量:

      • prev变量记录前一个元素的位置
      • next变量记录下一个元素的位置

      特点:

      • 数据有序
      • 底层结构为链表

      arraylist比较:

      • linkedlist的添加元素速度比arraylist快;
      • linkedlist的查询速度比arraylist慢;
      • 底层数据结构不同:linkedlist用的是链表结构,而arraylist底层使用 的是数组结构;

      说明:linkedlist一般用于添加频繁的操作,arraylist一般用于频繁查询 的操作。

      示例:

      public class stack {
          private linkedlist data = null;
          public stack(){
              data = new linkedlist();
          }
          // 添加元素
          public boolean push(object element) {
              data.addfirst(element);
              return true;
          }
          // 获取元素
          public object pop() {
              return data.pollfirst();
          }
          // 判断集合是否为空
          public boolean isempty() {
              return data.isempty();
          }
          // 迭代元素
          public void list() {
              iterator it = data.iterator();
              while(it.hasnext()){
                  system.out.println(it.next());
              }
          }
      }
      public class mystack {
          public static void main(string[] args) {
              stack stack = new stack();
              stack.push("张三");
              stack.push("李四");
              stack.push("王五");
              stack.list();
              system.out.println("-------------");
              object pop = stack.pop();
              system.out.println(pop);
          }
      }
      

      2、set

      1)hashset

      hashset 是 set 接口的典型实现,大多数时候使用 set 集合时都使用 这个实现类。

      • hashset 按 hash 算法来存储集合中的元素,因此具有很好的存取、 查找、删除性能。
        • hashset 具有以下特点:不能保证元素的排列顺序
        • hashset 不是线程安全的
        • 集合元素可以是 null
        • 不能添加重复元素
      • hashset 集合判断两个元素相等的标准:两个对象通过 hashcode() 方法比较相等,并且两个对象的 equals() 方法返回值也相等。
      • 对于存放在set容器中的对象,对应的类一定要重写equals()和 hashcode(object obj)方法,以实现对象相等规则。即:“相等的对象必须具有相等的散列码”。

      示例:

       public static void main(string[] args) {
              set set = new hashset();
              // 添加
              // boolean add(e e) :把指定的元素添加到集合中
              set.add("hello");
              set.add("world");
              set.add("world");
              set.add(null);
              system.out.println(set);
              // 注:set集合中元素是无序,并且不能重复
              // boolean addall(collection<? extends e> c) :把指定的集合添加到集合中
              set set1 = new hashset();
              set1.add("aaa");
              set1.add("linux");
              ;
              set.addall(set1);
              system.out.println(set);
              // boolean remove(object o) :从集合中删除指定元素
              set.remove("hello");
              system.out.println(set);
              // boolean removeall(collection<?> c) :从集合中删除指定集合中的所有元素
              set1.add("aaa");
              set1.add("linux");
              set.removeall(set1);
              system.out.println(set);
              // void clear() :清空集合中所有元素
              set.clear();
              system.out.println(set);
              // int size() :获取集合的元素个数
              int size = set.size();
              system.out.println(size);
              // boolean contains(object o) :判断集合中是否包含指定元素,包含为true,否则为false;
              system.out.println(set.contains("aaa"));
      
              // boolean isempty() :判断集合是否为空
              system.out.println(set.isempty());
          }
      

      说明:在hashset添加元素时,会首先比较两个元素的hashcode值是不相等,如 果不相等则直接添加;如果相等再判断两个元素的equals的值是否相等, 如果相等则不添加,如果不相等则添加。

      2)treeset

      • treeset和treemap采用红黑树的存储结构
      • 特点:有序,查询速度比list快

      使用treeset集合是,对象必须具有可比较性。而要让对象具有可比较性有 两种方式:

      第一种:实现comparable接口,并重写compareto()方法:

      第二种:写一个比较器类,让该类去实现comparator接口,并重写 comare()方法。

      示例:

      1.实体类

      public class student implements comparable<student>{
          private string name;
          private int age;
          private string sex;
          private int height;
      
          public student() {
          }
          public student(string name, int age, string sex, int height) {
              this.name = name;
              this.age = age;
              this.sex = sex;
              this.height = height;
          }
          public string getname() {
              return name;
          }
          public void setname(string name) {
              this.name = name;
          }
          public int getage() {
              return age;
          }
          public void setage(int age) {
              this.age = age;
          }
          public string getsex() {
              return sex;
          }
          public void setsex(string sex) {
              this.sex = sex;
          }
          public int getheight() {
              return height;
          }
          public void setheight(int height) {
              this.height = height;
          }
          @override
          public boolean equals(object o) {
              if (this == o) return true;
              if (o == null || getclass() != o.getclass()) return false;
              student student = (student) o;
              return age == student.age &&
                      height == student.height &&
                      objects.equals(name, student.name) &&
                      objects.equals(sex, student.sex);
          }
          @override
          public int hashcode() {
              return objects.hash(name, age, sex, height);
          }
          @override
          public string tostring() {
              return "student{" +
                      "name='" + name + '\'' +
                      ", age=" + age +
                      ", sex='" + sex + '\'' +
                      ", height=" + height +
                      '}';
          }
      
          @override
          public int compareto(student stu) {
              if (stu.getage() > this.getage()){
                  return 1;
              }
              if (stu.getage() < this.getage()){
                  return -1;
              }
              return stu.getname().compareto(this.getname());
          }
      }
      

      2.测试类:

      public class treesettest {
          public static void main(string[] args) {
      
              treeset treeset = new treeset();
              student student1 = new student("张三", 20, "男", 165);
              student student2 = new student("李四", 21, "男", 170);
              student student3 = new student("王五", 19, "女", 160);
              student student4 = new student("赵六", 18, "女", 165);
              student student5 = new student("田七", 20, "男", 175);
              treeset.add(student1);
              treeset.add(student2);
              treeset.add(student3);
              treeset.add(student4);
              treeset.add(student5);
              system.out.println(treeset);
          }
      }
      

      3.实体类

      public class teacher {
          private string name;
          public teacher(){}
          public teacher(string name){
              this.name = name;
          }
          public string getname() {
              return name;
          }
          public void setname(string name) {
              this.name = name;
          }
          @override
          public string tostring() {
              return "teacher{" +
                      "name='" + name + '\'' +
                      '}';
          }
      }
      

      4.测试类

      public class treesettest2 {
          public static void main(string[] args) {
              teacher teacher1 = new teacher("11");
              teacher teacher2 = new teacher("12");
              teacher teacher3 = new teacher("13");
              teacher teacher4 = new teacher("14");
              teacher teacher5 = new teacher("15");
              treeset treeset1 = new treeset(new  comparator() {
                  @override
                  public int compare(object o1, object o2) {
                      return o1.hashcode() - o2.hashcode();
                  }
              });
              treeset1.add(teacher1);
              treeset1.add(teacher2);
              treeset1.add(teacher3);
              treeset1.add(teacher4);
              treeset1.add(teacher5);
              system.out.println(treeset1);
          }
      }
      

      说明:hashset去重是依靠hashcodeequals()方法,而treeset去重则 依靠的是比较器。

      总结

      本篇文章就到这里了,希望能对你有所帮助,也希望您能够多多关注www.887551.com的更多内容!