day15-day16

1. 异常

1.1 try...catch异常处理

try catch的异常处理的格式写法 :

try{
    被检测的代码
    可能发生异常的代码
}catch(异常类的类名  变量名){
    异常的处理方式 : 写什么都可以
    定义变量,创建对象,调用方法,循环,判断...
    只要写了catch,异常就被处理掉了
}
public static void main(String[] args) {
        int[] arr = {1};
        //try  catch异常处理
        try {
            int i = getNum(arr);
            System.out.println("i = " + i);
        }catch (Exception ex){
            System.out.println("异常被处理掉");
        }
        System.out.println(111);
    }

    public static int getNum(int[] arr){
        return arr[1] + 10;
    }

1.2 多catch并行处理

异常处理的代码中 : try 可以跟随多个catch

好处 : 不同的异常,可以区别对待,分开处理

public static void main(String[] args) {
    /**
    *   myExec出现2个异常
    *   写2个catch分别捕获异常
    */
    try {
            myExec(0);
        }catch (NullPointerException ex){
            System.out.println("处理空指针异常");
        }catch (ArrayIndexOutOfBoundsException ex){
            System.out.println("处理越界异常");
        }
    }

    /**
    * 定义方法,目的引发异常
    * 传递参数 : 对参数进行判断
    */
    public static void  myExec(int i){
    if ( i == 0){
        //引发空指针异常
        String s = null;
        int len = s.length();
    }else {
        //引发越界异常
        int[] arr = {};
        int a = arr[0];
    }
}

多个catch处理异常的时候,写法特别注意 : 如果catch中的异常类没有关系,先写后写没有区别, catch中的异常类有继承关系,父类写在最下面

1.3 throw和throws 关键字的使用

  • throw关键字 : 只能写在方法内部, 关键字的后面跟随对象的创建
  • throws关键字 : 只能写在方法的定义上,关键字后面跟随异常类名
public static void main(String[] args) {
       /**
         *   getArea()调用方法,方法上有异常
         *   只能处理,不处理编译失败
         *   在main的方法上加throws 异常没有处理,交给JVM处理
         *   try catch处理
         */
    try {
        int area = getArea(-10);
        System.out.println(area);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
     * 功能: 计算正方形的面积
     * 需要参数 : 边长
     * 语法 : 方法的内部出现了异常,必须在方法定义上暴露
     */
public static int getArea(int length) throws  Exception{
    if (length <= 0)
        //数据错误,导致后面的计算不能进行
        //内部出现问题
        throw new Exception("边长不存在");
    return length * length;
}

1.4 finally代码块

finally代码块跟随try ... catch使用,也有跟随try使用

finally代码块里面的程序,无论是否出现异常,都会执行,必须执行

结束JVM了,finally不执行.

主要用于释放资源

public static void main(String[] args) {
    try {
        int[] arr = {1};
        System.out.println(arr[0]);
    }catch (Exception ex){
        ex.printStackTrace();
    }finally {
        //后期用于资源的释放
        System.out.println("这里的代码,必须执行");
    }
}

1.5 RuntimeException异常

异常的父类是Exception,Exception类的子类RuntimeException,凡是RuntimeException和他的所有子类,都称为运行异常,非子类的称为编译异常

  • 编译异常 : 方法出现编译异常,调用者必须处理,否则编译失败.处理方式可以是try catch或者是throws都可以
  • 运行异常 : 方法出现运行异常,方法的定义上,不需要throws声明,调用者也不需要处理这个异常

不要处理运行异常 : 程序一旦发生运行异常,请程序人员修改源码

  • 常见的运行异常
    • NullPointerException 空指针
    • IndexOutOfBoundsException 越界异常
    • ClassCastException 类型强制
    • IllegalArgumentException 无效的参数异常

1.6 自定义异常

Java官方已经定义了大量的异常类,但是依然不够,以后做项目的时候,会出现的异常,在JDK中没有定义的,需要我们自己定义异常

  • 自定义异常,入伙,继承Exception或者RuntimeException
    • 只有Exception和他的子类,才具有可抛出性
  • 自定义的类中,构造方法,super调用父类构造方法,传递异常信息
/**
 *  自定义的异常类
 *    成绩负数的异常
 *    继承哪个父类呢
 *
 *    自定义异常信息 : 继承父类 RuntimeException 带有String类型的构造方法 (String 异常信息)
 */
public class ScoreException extends RuntimeException{
    public ScoreException(String s){
        super(s);
    }
}
    public static void main(String[] args) {
       // int[] arr = {1};
        //System.out.println(arr[2]);
        int avg = getAvg(-100,2);
        System.out.println("avg = " + avg);
    }

    /**
     * 计算成绩的平均分
     */
    public static int getAvg(int math,int chinese){
        //判断成绩的数值
        if ( math < 0 || chinese < 0)
            //手动抛出,自己定义的异常
            throw new ScoreException("成绩不存在");

        return  (math + chinese) / 2;
    }

2. 集合框架

2.1 集合框架由来

JDK1.2版本后,出现这个集合框架,到JDK1.5后,大幅度优化.

  • 集合本质上是存储对象的容器
  • 数组也能存储对象,数组弊端就是定长
  • 解决数组的问题,开发出来集合框架,集合框架无需考虑长度
  • 集合和数组的区别与共同点
    • 集合,数组都是容器,都可以存储数据
    • 集合只存储引用数据类型,不存储基本数据类型
    • 数组可以存储基本类型,也可以存储引用类型
    • 数组定长,集合容器变成

牢记 : 数据多了存数组,对象多了存集合

  • 集合学习的关键点
    • 怎么存储数据
    • 怎么取出数据
    • 选择哪种容器

2.2 集合框架的继承体系

  • Collection (集合) 接口 单列集合,单身狗
    • List (列表) 接口
      • ArrayList (数组列表) 实现类
      • LinkedList (链表) 实现类
      • Vector(数组列表) 实现类,过时了
    • Set (集) 接口
      • HashSet(哈希表) 实现类
        • LinkedHashSet(链表哈希表) 实现类,继承HashSet
      • TreeSet(红黑树) 实现类
  • Map (映射键值对) 接口 双列集合 虐狗的
    • HashMap(哈希表) 实现类
      • LinkedHashMap(链表哈希表) 实现类,继承HashMap
    • TreeMap(红黑树) 实现类
    • Hashtable(哈希表) 实现类,过时
      • Properties(哈希表)实现类, 继承Hashtable
    • ConCurrentHashMap (哈希表) 线程相关
  • Iterator迭代器接口
  • 泛型 Generic
    • 写法
    • 泛型类,泛型方法,泛型接口,泛型限定,泛型通配符
  • for(:)循环

2.3 Collection接口

是所有单列集合的顶级接口,任何单列集合都是他的子接口,或者是实现类, 该接口中定义的方法,是所有单列集合的共性方法.

使用接口Collection的实现类ArrayList,创建对象.

Collection 尖括号就是泛型,E我们要写,集合存储的数据类型

2.3.1 Collection接口的常用方法

方法的定义方法作用
boolean add(E)元素添加到集合
void clear()清空集合容器中的元素
boolean contains(E)判断元素是否在集合中
boolean isEmpty()判断集合的长度是不是0,是0返回true
int size()返回集合的长度,集合中元素的个数
boolean remove(E)移除集合中指定的元素,移除成功返回true
T[] toArray(T[] a)集合转成数组

add(E)

/**
*  boolean add(E) 元素添加到集合中
*  返回值,目前都是true
*/
public static void collectionAdd(){
    //接口多态创建集合容器对象,存储的数据类型是字符串
    Collection<String> coll = new ArrayList<>();
    //集合对象的方法add添加元素
    coll.add("hello");
    coll.add("world");
    coll.add("java");
    coll.add("money");
    coll.add("wife");
    /**
    *  输出语句中,输出集合对象,调用的是方法toString()
    *  看到的内容是一个完整的字符串, 不叫遍历
    */
    System.out.println(coll);
}

void clear(), int size(), boolean isEmpty()

    /**
     *  void clear() 清空集合中的所有元素
     *  int size() 集合的长度
     */
    public static void collectionClear(){
        Collection<Integer> coll = new ArrayList<>();
        coll.add(1);
        coll.add(2);
        coll.add(3);
        System.out.println(coll);
        System.out.println("集合的长度::"+ coll.size());//长度
        coll.clear();
        System.out.println(coll);
        System.out.println("集合的长度::"+ coll.size());
        System.out.println("集合是空吗?" + coll.isEmpty());//长度=0,isEmpty()返回true
    }

boolean contains(), boolean remove()

/**
     *  boolean contains(E) 判断是否包含
     *  boolean remove(E) 移除元素
     */
public static void collectionContains(){
    //接口多态创建集合容器对象,存储的数据类型是字符串
    Collection<String> coll = new ArrayList<>();
    //集合对象的方法add添加元素
    coll.add("hello");
    coll.add("wife");
    coll.add("world");
    coll.add("java");
    coll.add("money");
    coll.add("wife");
    //判断集合中是否包含某个元素
    boolean b = coll.contains("world");
    System.out.println("b = " + b);

    //移除集合中的元素
    //删除成功返回true,如果有多个相同的对象,删除最先遇到的那个
    boolean b1 = coll.remove("wife");
    System.out.println("b1 = " + b1);
    System.out.println(coll);
}

2.4 Iterator接口

迭代器接口 Iterator , 为集合进行遍历的. 迭代器技术是所有Collection集合的通用遍历形式.

2.4.1 Iterator接口的抽象方法

  • boolean hasNext() 判断集合中是否有下一个可以遍历的元素,如果有返回true
  • E next() 获取集合中下一个元素
  • void remove() 移除遍历到的元素

2.4.2 获取迭代器接口实现类

迭代器就是为了遍历集合而产生. 集合的顶层接口Collection中定义了方法: 方法的名字就是 iterator() ,返回值是Iterator接口类型, 返回的是Iterator接口实现类的对象

Collection接口中的方法摘要 :
  public Iterator iterator() ; 返回迭代器接口实现类的对象

使用的对象ArrayList,实现接口Collection,重写方法iterator();
public static void main(String[] args) {
    //迭代器遍历集合
    //接口多态创建集合容器对象,存储的数据类型是字符串
    Collection<String> coll = new ArrayList<>();
    //集合对象的方法add添加元素
    coll.add("hello");
    coll.add("world");
    coll.add("java");
    coll.add("money");
    coll.add("wife");
    //1 遍历 集合对象,调用方法iterator() 获取迭代器接口的实现类对象
    Iterator<String> it = coll.iterator();
    //2 迭代器对象的方法,判断集合是否有下元素
    //boolean b = it.hasNext();
    //System.out.println(b);
    //3 迭代器对象的方法,取出元素
    //String str = it.next();
    //System.out.println(str);
    //条件,集合中有下一个元素就可以
    while ( it.hasNext() ){
        String str =  it.next();
        System.out.println(str);
    }
}

2.4.3 迭代器的实现原理

每个集合容器,内部结构不同,但是迭代器都可以进行统一的遍历实现

结论 : 迭代器是隐藏在集合的内部的, 提供公共的访问方式, Iterator接口

interface Iterator{
    boolean hasNext();
    E next();
    void remove();
}

public class ArrayList {
    public Iterator iterator(){
        return  new Itr();
    }

    private class Itr implements Iterator{
         boolean hasNext(); //重写
         E next(); //重写
         void remove(); //重写
    }

}

2.4.4 并发修改异常

如何不发生这个异常

异常的产生原因 : 在迭代器遍历集合的过程中,使用了集合的功能,改变了集合的长度造成

public static void main(String[] args) {
    //迭代器遍历集合
    //接口多态创建集合容器对象,存储的数据类型是字符串
    Collection<String> coll = new ArrayList<>();
    //集合对象的方法add添加元素
    coll.add("hello");
    coll.add("world");
    coll.add("java");
    coll.add("money");
    coll.add("wife");
    //迭代器遍历集合
    Iterator<String> it = coll.iterator();
    while ( it.hasNext() ){
        String str = it.next();
        //判断,遍历到的集合元素是不是java
        if (str.equals("java")){
            //添加元素 出现并发修改异常
            coll.add("add");
        }
        System.out.println(str);
    }
}

2.4.5 集合存储自定义对象并迭代

public static void main(String[] args) {
    //创建集合,存储自定义的对象
    Collection<Person> coll = new ArrayList<>();
    //集合的方法add存储Person对象
    coll.add( new Person("张三",21) );
    coll.add( new Person("李四",22) );
    coll.add( new Person("王五",23) );
    //迭代器遍历集合

    Iterator<Person> iterator = coll.iterator();
    while (iterator.hasNext()){
        Person person = iterator.next();
        System.out.println(person);
        System.out.println(person.getName());
    }
}
/**
 *  定义私有成员
 *  get set方法
 *  无参数构造方法
 *
 *  满足以上的三个条件 ,这个类,换一个名字,叫JavaBean
 */
public class Person   {
    private String name;
    private int age;
    public Person(){}

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

    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;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2.5 List接口

List接口,继承Collection接口,是单列集合, Collection接口中的方法不需要在讲解了

2.5.1 List接口的特点

  • 这个接口的集合都具有索引
  • 这个接口中的元素允许重复
  • 这个接口中的元素是有序
    • 元素不会排序 ,有序指的是 ,元素存储和取出的顺序是一致的

List接口的所有实现类,都具有以上三个特征

2.5.2 List接口自己的方法 (带有索引)

add(int index ,E e)

/**
* List接口的方法 add(int index, E e)
* 指定的索引位置,添加元素
*
*   IndexOutOfBoundsException 集合越界异常  长度是size()
*     StringIndexOutOfBoundsException 字符串越界异常  长度是 length()
*     ArrayIndexOutOfBoundsException 数组越界异常  长度是 length
*/
public static void listAdd(){
    List<String> list = new ArrayList<>();
    list.add("a") ;//集合的尾部添加
    list.add("b");
    list.add("c");
    list.add("d");
    list.add("e");
    System.out.println(list);
    //指定的索引上,添加元素 ,3索引添加元素
    list.add(3,"QQ");
    System.out.println(list);
}

get(int index)

    /**
     *  List接口的方法 E get(int index)
     *  返回指定索引上的元素
     *  List集合可以使用for循环像数组一样的方式遍历
     */
    public static void listGet(){
        List<String> list = new ArrayList<>();
        list.add("a") ;//集合的尾部添加
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");
        //List接口方法get取出元素
        //String s = list.get(3);
        //System.out.println(s);
        for(int i = 0 ; i < list.size() ; i++){
            System.out.println(list.get(i));
        }
    }

set(int index,E e),remove(int index)

/**
     * List接口方法
     *  E set (int index , E e) 修改指定索引上的元素,返回被修改之前的元素
     *  E remove(int index) 移除指定索引上的元素, 返回被移除之前的元素
     */
    public static void listSetRemove(){
        List<String> list = new ArrayList<>();
        list.add("a") ;//集合的尾部添加
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");
        System.out.println(list);
        //修改指定索引上的元素,3索引
        String str = list.set(3,"https://www.baidu.com");
        System.out.println(list);
        System.out.println(str);
        //删除指定索引上的元素,删除3索引
        str = list.remove(3);
        System.out.println(list);
        System.out.println(str);
    }

2.5.3 List集合的特有迭代器

List接口中的方法 listIterator() 返回迭代器,迭代器的接口是ListIterator,集合的专用迭代器.

  • ListIterator迭代器接口的方法
    • boolean hasNext()
    • E next()
    • boolean hasPrevious() 判断集合中是否有上一个元素,反向遍历
    • E previous() 取出集合的上一个元素
    /**
     * List接口的方法:
     *   listIterator() List集合的特有迭代器
     *   反向遍历
     */
    public static void iterator(){
        List<String> list = new ArrayList<>();
        list.add("a") ;//集合的尾部添加
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");
        //获取特有迭代器接口实现类对象
        ListIterator<String> lit = list.listIterator();
        //先要正向遍历
        while (lit.hasNext()){
            String s = lit.next();
            System.out.println(s);
        }
        System.out.println("=============");
        //判断上一个元素
        while (lit.hasPrevious()){
            //取出元素
            String s = lit.previous();
            System.out.println(s);
        }
    }

2.6 List接口的实现类的数据结构

链表结构

  • 数组 :
    • 有索引,数组中元素的地址是连续,查询速度快
    • 数组的长度为固定,新数组创建,数组元素的复制,增删的效率慢
  • 链表
    • 链表没有索引,采用对象之间内存地址记录的方式存储
    • 查询元素,必须通过第一个节点依次查询,查询性能慢
    • 增删元素,不会改变原有链表的结构,速度比较快

3. ArrayList

3.1 ArrayList集合的特点

ArrayList类实现接口List,ArrayList具备了List接口的特性 (有序,重复,索引)

  • ArrayList集合底层的实现原理是数组,大小可变 (存储对象的时候长度无需考虑).

  • 数组的特点 : 查询速度快,增删慢.

  • 数组的默认长度是10个,每次的扩容是原来长度的1.5倍.

  • ArrayList是线程不安全的集合,运行速度快.

3.2 ArrayList源码解析

3.2.1 ArrayList类成员变量

 private static final int DEFAULT_CAPACITY = 10; //默认容量
 private static final Object[] EMPTY_ELEMENTDATA = {};//空数组
transient Object[] elementData; //ArrayList集合中的核心数组
private int size; //记录数组中存储个数
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; //数组扩容的最大值

3.2.2 ArrayList集合类的构造方法

//无参数构造方法
public ArrayList() {
    this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; //数组没有长度
//有参数的构造方法
public ArrayList(int 10) {
    if (initialCapacity > 0) {
        //创建了10个长度的数组
        this.elementData = new Object[10];
    } else if (initialCapacity == 0) {
        this.elementData = EMPTY_ELEMENTDATA;
    } else {
        throw new IllegalArgumentException("Illegal Capacity: "+
    initialCapacity);
    }
}

3.2.3 ArrayList集合类的方法add()

new ArrayList<>().add("abc"); //集合中添加元素
public boolean add("abc") {
    //检查容量  (1)
    ensureCapacityInternal(size + 1); 
    //abc存储到数组中,存储数组0索引,size计数器++
    elementData[size++] = "abc";//数组扩容为10
    return true;
}
//检查集合中数组的容量, 参数是1
private void ensureCapacityInternal(int minCapacity = 1) {
    //calculateCapacity 计算容量,方法的参是数组 , 1
    // ensureExplicitCapacity (10) 扩容的
    ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
//计算容量方法, 返回10
private static int calculateCapacity(Object[] elementData, int minCapacity = 1) {
    //存储元素的数组 == 默认的空的数组  构造方法中有赋值
    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
        //返回最大值   max(10,1)
        return Math.max(DEFAULT_CAPACITY, minCapacity);
    }
    return minCapacity;
}
//扩容 
private void ensureExplicitCapacity(int minCapacity = 10) {
    modCount++;
   // 10 - 数组的长度0 > 0
    if (minCapacity - elementData.length > 0)
        //grow方法(10) 数组增长的
        grow(minCapacity);
}
//增长的方法,参数是(10)
 private void grow(int minCapacity = 10) {
     //变量oldCapacity保存,原有数组的长度  = 0
     int oldCapacity = elementData.length; // 0
     //新的容量 = 老 + (老的 / 2)
     int newCapacity = oldCapacity + (oldCapacity >> 1);// 0
     // 0 - 10 < 0 新容量-计算出的容量
     if (newCapacity - minCapacity < 0)
         newCapacity = minCapacity; //新容量 = 10
     //判断是否超过最大容量
     if (newCapacity - MAX_ARRAY_SIZE > 0)
     newCapacity = hugeCapacity(minCapacity);
     // minCapacity is usually close to size, so this is a win:
//数组的赋值,原始数组,和新的容量
     elementData = Arrays.copyOf(elementData, newCapacity);
 }

4. LinkedList集合使用

4.1 LinkedList集合的特点

LinkedList类实现接口List,LinkedList具备了List接口的特性 (有序,重复,索引)

  • LinkedList底层实现原理是链表,双向链表
  • LinkedList增删速度快
  • LinkedList查询慢
  • LinkedList是线程不安全的集合,运行速度快

4.2 LinkedList集合特有方法

集合是链表实现,可以单独操作链表的开头元素和结尾元素

  • void addFirst(E e) 元素插入到链表开头
  • void addLast(E e) 元素插入到链表结尾
  • E getFirst() 获取链表开头的元素
  • E getLast() 获取链表结尾的元素
  • E removeFirst() 移除链表开头的元素
  • E removeLast() 移除链表结尾的元素
  • void push(E e)元素推入堆栈中
  • E pop()元素从堆栈中弹出
public static void main(String[] args) {
    linkedPushPop();
}
//- void push(E e)元素推入堆栈中
//- E pop()元素从堆栈中弹出

public static void linkedPushPop(){
    LinkedList<String> linkedList = new LinkedList<String>();
    //元素推入堆栈中
    linkedList.push("a"); //本质就是addFirst() 开头添加
    linkedList.push("b");
    linkedList.push("c");
    System.out.println("linkedList = " + linkedList);

    String pop = linkedList.pop(); // removeFirst()移除开头
    System.out.println(pop);
    System.out.println("linkedList = " + linkedList);
}

//- E removeFirst() 移除链表开头的元素
//- E removeLast() 移除链表结尾的元素
public static void linkedRemove(){
    LinkedList<String> linkedList = new LinkedList<String>();
    linkedList.add("a"); //结尾添加
    linkedList.add("b"); //结尾添加
    linkedList.add("c"); //结尾添加
    linkedList.add("d"); //结尾添加
    System.out.println("linkedList = " + linkedList);
    //移除开头元素,返回被移除之前
    String first = linkedList.removeFirst();
    //移除结尾元素,返回被移除之前的
    String last = linkedList.removeLast();
    System.out.println("first = " + first);
    System.out.println("last = " + last);
    System.out.println("linkedList = " + linkedList);
}

//- E getFirst() 获取链表开头的元素
//- E getLast() 获取链表结尾的元素
public static void linkedGet(){
    LinkedList<String> linkedList = new LinkedList<String>();
    linkedList.add("a"); //结尾添加
    linkedList.add("b"); //结尾添加
    linkedList.add("c"); //结尾添加
    linkedList.add("d"); //结尾添加
    System.out.println("linkedList = " + linkedList);
    //获取开头元素
    String first = linkedList.getFirst();
    //获取结尾元素
    String last = linkedList.getLast();
    System.out.println("first = " + first);
    System.out.println("last = " + last);
    System.out.println("linkedList = " + linkedList);
}

// void addFirst(E e) 元素插入到链表开头
// void addLast(E e) 元素插入到链表结尾
public static void linkedAdd(){
    LinkedList<String> linkedList = new LinkedList<String>();
    linkedList.add("a"); //结尾添加
    linkedList.add("b"); //结尾添加
    linkedList.add("c"); //结尾添加
    linkedList.add("d"); //结尾添加
    System.out.println("linkedList = " + linkedList);
    //结尾添加
    linkedList.addLast("f");
    linkedList.add("g");

    //开头添加
    linkedList.addFirst("e");
    System.out.println("linkedList = " + linkedList);
}

4.3 LinkedList源码解析

4.3.1 LinkedList集合的成员变量

transient int size = 0; //集合中存储元素个数计数器
transient Node<E> first; //第一个元素是谁
transient Node<E> last; //最后一个元素是谁

4.3.2 LinkedList集合的成员内部类Node (节点)

//链表中,每个节点对象
private static class Node<E> {
        E item; //我们存储的元素
        Node<E> next; // 下一个节点对象
        Node<E> prev; // 上一个节点对象
    //构造方法,创建对象,传递上一个,下一个,存储的元素
    Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
}

4.3.4 LinkedList集合的方法add()添加元素

//添加元素 e 存储元素 abc
//再次添加元素 e
void linkLast(E "abc") {
    //声明新的节点对象 = last
    final Node<E> l = last; // l = null  l "abc"节点
    //创建新的节点对象,三个参数, 最后一个对象,"abc", 上一个对象null
    final Node<E> newNode = new Node<>(l, e, null);
    //新节点赋值给最后一个节点
    last = newNode;
    if (l == null)
        //新存储的几点赋值给第一个节点
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}

4.3.5 LinkedList集合的方法get()获取元素

//集合的获取的方法
//index是索引, size 长度计数器
Node<E> node(int index) {
    //索引是否小于长度的一半,折半思想
    if (index < (size >> 1)) {
        Node<E> x = first;
        for (int i = 0; i < index; i++)
        x = x.next;
        return x;
    } else {
            Node<E> x = last;
        for (int i = size - 1; i > index; i--)
        x = x.prev;
        return x;
    }
}

5. Set集合

Set集合,是接口Set,继承Collection接口. Set集合不存储重复元素

Set接口下的所有实现类,都会具有这个特性.

Set接口的方法,和父接口Collection中的方法完全一样

5.1 Set集合存储和遍历

public static void main(String[] args) {
    //Set集合存储并迭代
    Set<String> set = new HashSet<String>();
    //存储元素方法 add
    set.add("a");
    set.add("b");
    set.add("c");
    set.add("d");
    set.add("d");
    System.out.println("set = " + set);

    Iterator<String> it = set.iterator();
    while (it.hasNext()){
        System.out.println(it.next());
    }
}

5.2 Set接口实现类HashSet类

  • HashSet集合类的特点 :
    • 实现Set接口,底层调用的是HashMap集合
    • HashSet的底层实现原理是哈希表
    • HashSet不保证迭代顺序,元素存储和取出的顺序不一定
    • 线程不安全,运行速度快

5.3 对象的哈希值

每个类继承Object类,Object类定义方法 :

public native int hashCode(); // C++语言编写,不开源

方法使用没有区别 : 方法返回int类型的值,就称为哈希值

哈希值的结果不知道是怎么计算的,调用toString()方法的时候,返回的十六进制数和哈希值是一样的, @1b6d3586叫哈希值 (根本和内存地址是无关的)

public static void main(String[] args) {
    Person p = new Person();
    int code = p.hashCode();
    // int 变量 460141958 (是什么,无所谓, 数字就是对象的哈希值)
    System.out.println(code);
    // com.atguigu.hash.Person@1b6d3586
    System.out.println(p.toString());
 }
   /**
     * 重写父类的方法
     * 返回int值
     */
    public int hashCode(){
        return 9527;
    }

5.4 String类的哈希值

字符串类重写方法hashCode(),自定义了哈希值,哈希值的计算方法是 :

h = 31 * 上一次的计算结果 + 字符数组中元素的ASCII码值

*31 的目的,减少相同哈希值的计算

String类的哈希值

    //字符串String对象的哈希值
    private static void stringHash(){
        String s1 ="abc";
        String s2 ="abc";
        System.out.println(s1 == s2); //T
        //String类继承Object,可以使用方法hashCode
        System.out.println(s1.hashCode() == s2.hashCode()); //T
        /**
         * String类继承Object类
         * String类重写父类的方法 hashCode() 自己定义了哈希值
         */
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        System.out.println("=============");

        /**
         *  字符串内容不一样,有没有可能计算出相同的哈希值
         *    String s1 ="abc";
         *    String s2 ="abc";
         */
        String s3 = "通话";
        String s4 = "重地";
        //1179395
        //1179395
        System.out.println(s3.hashCode());
        System.out.println(s4.hashCode());

        System.out.println(s3.equals(s4));
    }

5.5 哈希值的相关问题

问题 : 两个对象A,B 两个对象哈希值相同,equals方法一定返回true吗?

​ 两个对象A,B 两个对象equals方法返回true,两个对象的哈希值一定相同吗

结论 : 两个对象的哈希值相同,不要求equals一定返回true. 两个对象的equals返回true,两个对象的哈希值必须一致

Sun 公司官方规定 : 上面的结论

5.6 哈希表的数据结构

数组 + 链表的组合体

class Node{
    E element; //存储的元素
    Node next; //下一个元素
}
main(){
    Node[] node = new Node[5];
}
  • 哈希表的底层数组长度默认是16个,扩容为原来长度的2倍
  • 加载因子默认是0.75F,数组中存储元素的个数达到长度的75%,扩容

5.7 哈希表存储对象的过程

public static void main(String[] args) {
    Set<String> set = new HashSet<String>();
    //存储对象
    set.add("abc");
    set.add("bbc");
    set.add(new String("abc"));
    set.add("通话");
    set.add("重地");
    System.out.println("set = " + set);
}

5.8 哈希表存储自定义的对象

public class Student {
    private int age;
    private String name;

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result = age;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
public static void main(String[] args) {
    Set<Student> set = new HashSet<Student>();
    //存储Student的对象
    set.add(new Student("a1",201));
    set.add(new Student("a2",202));
    set.add(new Student("a2",202));
    set.add(new Student("a3",203));
    set.add(new Student("a4",204));
    System.out.println("set = " + set);
}

5.9 哈希表源码

HashSet集合本身不具备任何功能,内部调用了另一个集合对象HashMap

  • 构造方法无参数

    public HashSet() {
        map = new HashMap<>();
    }
  • HashMap类的成员变量

    //哈希表数组的初始化容量,16
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // 16
    static final int MAXIMUM_CAPACITY = 1 << 30; //最大容量
    static final float DEFAULT_LOAD_FACTOR = 0.75f;//价值因子
    static final int TREEIFY_THRESHOLD = 8;//阈值,转红黑树
    static final int UNTREEIFY_THRESHOLD = 6;//阈值,解除红黑树
    static final int MIN_TREEIFY_CAPACITY = 64;//阈值,转红黑树
  • HashMap内部类Node

    //节点
    static class Node<K,V> implements Map.Entry<K,V> {
            final int hash; //对象哈希值
            final K key; //存储的对象
            V value; //使用Set的集合,value没有值
            Node<K,V> next; //链表的下一个节点
    }
  • Set集合存储方法add(),调用的是HashMap集合的方法put()

//HashMap存储对象的方法put,Key存储的元素,V是空的对象
public V put(K key, V value) {
    //存储值,传递新计算哈希值,要存储的元素
    return putVal(hash(key), key, value, false, true);
}
 //传递存储的对象,再次计算哈希值
 //尽量降低哈希值的碰撞
 static final int hash(Object key) { 
   int h;
   return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
//存储值,重写计算的哈希值,要存储值
final V putVal(int hash, K key, V value, boolean false,
               boolean true) {
    //Node类型数组,     Node类型数组     n, i
     Node<K,V>[] tab; Node<K,V> p; int n, i;
     //tab =Node[]=null
    if ((tab = table) == null || (n = tab.length) == 0){
        //n=赋值为 tab数组=resize()方法返回数组,默认长度的数组16
          n = (tab = resize()).length;// 16
        //数组的长度-1 & 存储对象的哈希值,确定存储的位置
        //判断数组的索引上是不是空的
         if ((p = tab[i = (n - 1) & hash]) == null)
             //数组索引 赋值新的节点对象,传递计算的哈希值,存储的对象
             tab[i] = newNode(hash, key, value, null);
        else{
            //数组的索引不是空,要存储的对象,已经有了
            //判断已经存在的对象,和要存储对象的哈希值和equals方法
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                //遍历该索引下的链表,和每个元素比较hashCode和equals
        }
    }
}
 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
                newThr = oldThr << 1; 
 

5.10 哈希表面试问题

JDK7版本和JDK8版本的哈希表的区别

  • JDK7没有转红黑树
  • JDK8转成红黑树
    • 转成树的两个参数
      • 当一个数组中存储的链表长度>=8 转树
      • 数组的整体长度超过64
    • 树转回链表
      • 链表的长度 <=6
  • JDK7元素采用头插法,JDK8元素采用尾插法

6. 红黑树

红黑树(Red-Black-Tree)

  • 二叉树,本质就是链表

    • 查询速度快
    • 每个一个节点,只有两个子节点,左和右
    • 树长偏了
  • 自然平衡二叉树

    • 二叉树的基础上,改进,保证树是平衡的
  • 红黑树

    • 每个节点有颜色,要么红,要么是黑
    • 根节点必须是黑色
    • 叶子节点必须是黑色
    • 变量表示颜色,true黑色,false红色

6.1 TreeSet集合使用

TreeSet集合,底层是红黑树结构,依赖于TreeMap的实现

红黑树特点查找速度快,线程不安全

可以对存储到红黑树的元素进行排序,元素的自然顺序 abcd.. 字典顺序

   public static void treeSetString(){
       Set<String> set = new TreeSet<>();
       //存储元素
       set.add("abcd");
       set.add("ccdd");
       set.add("z");
       set.add("wasd");
       set.add("bbaa");
       System.out.println("set = " + set);
   }

6.2 TreeSet存储自定义对象

/**
* TreeSet集合存储Student对象
*/
public static void treeSetStudent(){
    Set<Student> set = new TreeSet<Student>();
    set.add(new Student("a",10));
    set.add(new Student("b",20));
    System.out.println("set = " + set);
}

程序出现了异常,类型的转换异常 ClassCastException

异常原因,Student类不能进行类型的转换,有接口没有实现java.lang.Comparable.

类实现接口Comparable,这个类就具有了自然顺序

  • Student类具有自然顺序
    • 实现接口Comparable,重写方法compareTo
    /**
     * 重写方法compareTo
     * 返回int类型
     * 参数 : 要参与比较的对象
     * this对象和student对象
     *
     * 红黑树,后来的对象是this,原有的对象是参数
     */
    public int compareTo(Student student){
        return this.age - student.age;
    }
  • 自定义比较器
    • java.util.Comparator接口
/**
 * 自定义的比较器
 * 实现接口,重写方法
 */
public class MyCom implements Comparator<Student> {
    @Override
    /**
     * TreeSet集合自己调用方法
     * 传递参数
     * Student o1, Student o2
     * o1是后来的对象
     * o2是已经有的对象
     */
    public int compare(Student o1, Student o2) {
        return o1.getAge() - o2.getAge();
    }
}
 Set<Student> set = new TreeSet<Student>( new MyCom());

7. LinkedHashSet

底层的数据结构是哈希表,继承HashSet

LinkedHashSet数据是双向链表, 有序的集合,存储和取出的顺序一样

public static void main(String[] args) {
    Set<String> set = new LinkedHashSet<>();
    set.add("b");
    set.add("e");
    set.add("c");
    set.add("a");
    set.add("d");
    System.out.println("set = " + set);
}

8. Collections工具类

  • java.util.Collection 集合的顶级接口
  • java.util.Collections 操作集合的工具类
    • 工具类的方法全部静态方法,类名直接调用
    • 主要是操作Collection系列的单列集合,少部分功能可以操作Map集合
/**
 *  集合操作的工具类
 *  Collections
 *  工具类有组方法: synchronized开头的
 *
 *  传递集合,返回集合
 *  传递的集合,返回后,变成了线程安全的集合
 */
public class CollectionsTest {
    public static void main(String[] args) {
        sort2();
    }
    //集合元素的排序,逆序
    public static void sort2(){
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(15);
        list.add(5);
        list.add(20);
        list.add(9);
        list.add(25);
        System.out.println("list = " + list);
        //Collections.reverseOrder() 逆转自然顺序
        Collections.sort(list,Collections.reverseOrder());
        System.out.println("list = " + list);
    }
    //集合元素的排序
    public static void sort(){
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(15);
        list.add(5);
        list.add(20);
        list.add(9);
        list.add(25);
        System.out.println("list = " + list);
        Collections.sort(list);
        System.out.println("list = " + list);
    }

    //集合元素的随机交换位置
    public static void shuffle(){
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(15);
        list.add(5);
        list.add(20);
        list.add(9);
        list.add(25);
        System.out.println("list = " + list);
        Collections.shuffle(list);
        System.out.println("list = " + list);

    }

    //集合的二分查找
    public static void binarySearch(){
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(5);
        list.add(9);
        list.add(15);
        list.add(20);
        list.add(25);
        int index = Collections.binarySearch(list, 15);
        System.out.println(index);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值