Java 高级复习(类集框架)

类集框架

类集:Java中一套动态数组的实现方案。在开发之中没有任何一项开发可以离开数组,传统数组实现起来非常的繁琐且长度是传统数组的致命伤。

类集主要就是对常见的数据结构进行完整的实现包装,并且提供一系列的接口与实现子类来帮助用户减少数据结构所带来的开发困难。

类集的核心接口:

Collection、List、Set、Map、Iterator、Enumeration、Queue、ListIterator。

Collection

collection是单值集合操作的最大父接口,在该接口中定义所有的单值数据的处理操作。

collection接口中的方法:

序号 方法名称 描述
1 public boolean add(Object o); 向集合保存单个数据(添加)
2 public boolean addAll(Collection c); 向集合添加指定集合(一组数据)
3 public void clear(); 清空集合,让根节点为空,同时执行GC处理
4 public boolean contains(Object o); 查询数据是否存在,需要equals()方法支持
5 public boolean containsAll(Object o); 集合中是否包含指定集合的所有元素
6 public boolean remove(Object o); 删除数据,需要equals()方法支持
7 public boolean removeAll(Collection c); 删除两个集合的交集
8 public boolean retainAll(Collection c); 保留两个集合的交集
9 public int size(); 获取数据长度
10 Object[] toArray(); 将集合变为对象数组返回
11 public boolean isEmpty(); 集合中是否包含对象
12 public boolean equal(Object o); 比较集合和指导对象o是否相等
13 public Iterator iterator(); 将集合变为Iterator接口返回(输出)
import java.util.*;

public class Demo8 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add("hello");
        collection.add(new Date());
        System.out.println(collection.size());
        System.out.println(collection);
    }
}

List

允许重复的List接口。

序号 方法名称 描述
1 public E get(int index); 获取指定索引上的数据
2 public E set(int index, E element); 修改指定索引上的数据
3 public E add(int index, E element); 添加指定索引上的数据
4 public E remove(int index); 删除指定位置的元素
5 public ListIterator listLterator(); 返回ListIterator接口对象
import java.util.*;

public class Demo09 {
    public static void main(String args[]) {
        List li = new ArrayList();
        for (int i = 0; i < 10; i++) {
            li.add("a");
        }
        System.out.println(li);
        li.add(3, "a20");
        System.out.println(li);
        System.out.println((String) li.get(2));
        li.remove(0);
        System.out.println(li);
    }
}

List本身依然属于一个接口,要想使用则一定要通过其子类完成。

ArrayList、Vector、LinkedList

ArrayList

import java.util.List;
import java.util.ArrayList;

public class Demo10 {
    public static void main(String[] args) {
		// 为list父接口进行实例化
        List<String> all = new ArrayList<String>();
        all.add("Hello");
		// 重复数据
        all.add("Hello");
        all.add("World");
        all.add("CSDN");
        System.out.println(all);
    }
}

List的存储特征:

  1. 保存(增加)顺序就是存储顺序。
  2. List集合允许重复数据

利用for-each方法输出:

import java.util.List;
import java.util.ArrayList;

public class Demo10 {
    public static void main(String[] args) {
//        为list父接口进行实例化
        List<String> all = new ArrayList<String>();
        all.add("Hello");
        all.add("Hello");
        all.add("World");
        all.add("CSDN");
        all.forEach((str) -> {
            System.out.println(str + "、");
        });
    }
}
import java.util.List;
import java.util.ArrayList;

public class Demo10 {
    public static void main(String[] args) {
//        为list父接口进行实例化
        List<String> all = new ArrayList<String>();
        System.out.println("集合是否为空?" + all.isEmpty());
        System.out.println("集合元素个数?" + all.size());
        all.add("Hello");
        all.add("Hello");
        all.add("World");
        all.add("CSDN");

        // 删除元素
        all.remove("Hello");

        System.out.println("集合是否为空?" + all.isEmpty());
        System.out.println("集合元素个数?" + all.size());
        all.forEach((str) -> {
            System.out.println(str + "、");
        });
    }
}

实现自定义类的保存

import java.util.List;
import java.util.ArrayList;

class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 重新toString方法
    @Override
    public String toString() {
        return "姓名:" + this.name + '、' + "年龄:" + this.age;
    }
}


public class Demo11 {
    public static void main(String[] args) {
        List<Person> all = new ArrayList<Person>();
        all.add(new Person("张三", 20));
        all.add(new Person("张四", 18));
        all.add(new Person("张五", 20));
        all.add(new Person("小米", 33));
//        代替消费型接口
        all.forEach(System.out::println);
    }
}

在使用List保存自定义类对象的时候,如果需要使用到contains()、remove()方法进行查询与删除操作时,必须保证类中成功的复写了equals()方法。

import java.util.List;
import java.util.ArrayList;
import java.util.Objects;

class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 重新toString方法
    @Override
    public String toString() {
        return "姓名:" + this.name + '、' + "年龄:" + this.age;
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object == null) {
            return false;
        }
        if (!(object instanceof Person)) {
            return false;
        }
        Person person = (Person) object;
        return this.name.equals(person.name) && this.age == person.age;
    }

}


public class Demo11 {
    public static void main(String[] args) {
        List<Person> all = new ArrayList<Person>();
        all.add(new Person("张三", 20));
        all.add(new Person("张四", 18));
        all.add(new Person("张五", 20));
        all.add(new Person("小米", 33));
        System.out.println(all.contains(new Person("小米", 33)));
        all.remove(new Person("小米", 33));
//        代替消费型接口
        all.forEach(System.out::println);
    }
}

LinkedList

List类基于链表的实现

序号 方法名称 描述
1 public void addFirst(Object o); 在列表首部添加元素
1 public void addLast(Object o); 在列表尾部添加元素
1 public Object getFirst(Object o); 返回列表首部第一个元素
1 public Object getLast(Object o); 返回列表尾部最后一个元素
1 public Object removeFirst(Object o); 删除并返回列表首部第一个元素
1 public Object removeLast(Object o); 删除并返回列表尾部最后一个元素

LinkedList和ArrayList有什么区别?

  1. ArrayList是数组实现的集合操作,而LinkedList是链表实现的集合操作;
  2. 在使用get()方法获取数据是,ArrayList的时间复杂度为O(1),而LinkedList的时间复杂度为O(n)(n为集合长度)。
  3. ArrayList在使用的时候默认的初始化对象大小长度为10,如果空间不足则会采用倍数的形式进行容量的扩充。如果保存大量数据的时候可能会造成垃圾的产生以及性能的下降,但是这时可以用LinkedList子类保存。

Vector

Vector类如果使用无参的构造函数,则一定默认会开辟一个10个长度的数组,其余的实现操作都与ArrayList是相同的。

Vector类之中的操作方法采用的都是synchronized同步处理,而ArrayList并没有进行同步处理,Vector在多线程访问的时候属于线程安全的,但是性能不如ArrayList高。

Set

不允许重复元素的Set接口。

Set集合并不像List集合那样扩充了很多新方法,所以无法使用List集合中提供的get()方法,即:无法实现索引数据的获取。

HashSet子类

操作特点:

  1. 不允许保存重复元素(Set接口定义)
  2. 数据保存是无序的

TreeSet子类

操作特点:

  1. 不允许保存重复元素(Set接口定义)
  2. 数据保存是有序的,数据都将安装数据的升序进行自动的排序处理。

Iterator迭代器

Iterator(称作迭代器)接口主要提供了对容器中元素进行遍历的方法。

所有实现了Collection接口的类都有一个iterator()方法来返回一个实现了Iterator接口的类的对象。

序号 方法名称 描述
1 public boolean hasNext(); 判断是否有下一个元素
2 public Object next(); 返回要访问的下一个元素
import java.util.*;

public class Demo12 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add("hello world");
        collection.add(new Integer(100));
        collection.add(new Float(99.9f));
        
        // 实例化Iterator接口对象
        Iterator iterator = collection.iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println(obj);
            iterator.remove();
        }
        System.out.println(collection);
    }
}
import java.util.*;

class Student {
    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // 重写toString()
    @Override
    public String toString() {
        return "姓名:" + this.name + "、" + "年龄:" + this.age;
    }
}

public class Demo13 {
    public static void main(String[] args) {
        List<Student> studentList = new ArrayList<Student>();
        studentList.add(new Student("张三", 20));
        studentList.add(new Student("张四", 18));
        studentList.add(new Student("张五", 20));
        studentList.add(new Student("小米", 33));
//        // 代替消费型接口
//        studentList.forEach(System.out::println);

        // 实例化Iterator接口对象
        Iterator iterator = studentList.iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println(obj);
            iterator.remove();
        }
    }
}

泛型

泛型存在的主要目的是为了解决ClassCastException的问题,在进行对象的向下转型的同时永远都可能存在安全隐患,Java希望可以通过泛型慢慢解决掉此问题。

Map接口

Map接口主要用于存储二元偶对象(key=value),二元偶对象的核心意义在于通过key去获取value的值。

Collection集合保存数据的目的是为了输出;

Map集合保存数据的目的是为了进行key的查找。

Map接口是进行二元偶对象保存的最大父接口。

序号 方法名称 描述
1 public V put(K key,V value); 向集合之中保存数据
2 public V get(Object key); 根据key查询数据
3 public Set<Map.Entry<K,V>>entrySet(); 将Map集合转为set集合
4 public boolean containKey(Object key); 查询指定的Key是否存在
5 public Set keySet(); 将Map集合中的key转化为Set集合
6 public V remove(Object key); 根据key删除掉指定的数据

猜你喜欢

转载自blog.csdn.net/qq_29537269/article/details/106951027