Java集合

集合

  1. 对象的容器,实现对对象常用的操作。可实现数组的功能。

  2. 和数组的区别:

  3. 数组长度固定,集合长度不固定。

  4. 数组可以存储基本类型和引用类型,集合只能存储引用类型。

Collection父接口

代表一组任意类型的对象,无序、无下标、不能重复。

接口的使用

  1. 创建和添加元素。

    创建 Collection collection = new ArrayList();

    添加 collection.add();

  2. 删除元素。

    collection.remove();

  3. 遍历元素。

    增强for遍历,迭代器遍历

  4. 判断。

    collection.contains();


1、元素的创建和添加

public static void main(String[] args) {
    //1、创建元素
    Collection collection = new ArrayList();
    collection.add("苹果");
    collection.add("西瓜");
    collection.add("榴莲");
    collection.add("橘子");
    System.out.println("元素个数:"+collection.size());
    System.out.println(collection);
    
结果
元素个数:4
[苹果, 西瓜, 榴莲, 橘子]
删除之后:3   

2、删除元素

//2、删除元素
collection.remove("榴莲");
System.out.println("删除之后:"+collection.size());
//清空  collection.clear();

结果
-------使用增强for-------
苹果
西瓜
橘子

3、遍历元素

其中迭代器不能用collection.remove的方法

删除元素,只能用迭代器自带的it.remove();

//3、遍历元素
//使用增强for
System.out.println("-------使用增强for-------");
for (Object object:collection){
    System.out.println(object);
}
//使用迭代器(迭代器:专门用来遍历集合的一种方式)

//hasNext() 有没有下一个元素
//next()  获取下一个元素
//remove() 移除当前元素
System.out.println("-------使用迭代器------");
Iterator it = collection.iterator();
while (it.hasNext()){
    String s = (String) it.next();
    System.out.println(s);
    //迭代器使用中不能使用remove方法  collection.remove();
    //  it.remove();//迭代器的删除方法  遍历完也删除完了。
    System.out.println("元素个数:"+collection.size());
    
结果
-------使用增强for-------
苹果
西瓜
橘子
-------使用迭代器------
苹果
西瓜
橘子
元素个数:3

4、判断

//4、判断
System.out.println("---------判断--------");
System.out.println(collection.contains("西瓜"));
System.out.println(collection.isEmpty());//判断是否为空

结果
---------判断--------
true
false

collection的使用:保存学生信息。

student类

{
    private String name;
    private  int sge;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getSge() {
        return sge;
    }

    public void setSge(int sge) {
        this.sge = sge;
    }

    //重写ToString方法
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sge=" + sge +
                '}';
    }
}

在使用collecting时,先创建collection对象

public static void main(String[] args) {
        //新建collection对象
        Collection collection  = new ArrayList();
        Student s1 = new Student("a", 20);
        Student s2 = new Student("b", 24);
        Student s3 = new Student("c", 13);
        //1、添加数据
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println("元素个数:"+collection.size());
        System.out.println(collection.toString());
        //2、删除元素
        //collection.remove(s1);
        collection.remove(new Student("c",13));
        System.out.println("删除之后:"+collection.size());
        //3、遍历
        //增强for
        System.out.println("----------增强for----------");
        for (Object object: collection){
            Student s = (Student) object;
            System.out.println(s.toString());
        }
        //迭代器
        System.out.println("----------迭代器----------");
        Iterator it = collection.iterator();
        while (it.hasNext()){
            Student s = (Student) it.next();
            System.out.println(s.toString());
        }
        //4、判断
            System.out.println(collection.contains(s1));
            System.out.println(collection.isEmpty());

    }

结果
元素个数:3
[Student{name='a', sge=20}, Student{name='b', sge=24}, Student{name='c', sge=13}]
删除之后:3
----------增强for----------
Student{name='a', sge=20}
Student{name='b', sge=24}
Student{name='c', sge=13}
----------迭代器----------
Student{name='a', sge=20}
Student{name='b', sge=24}
Student{name='c', sge=13}
true
false

Process finished with exit code 0

List子接口

有序,有下表,元素可以重复。

首先创建List

List list = new ArrayList();

1、添加元素

//1、添加元素
list.add("小米");
list.add("苹果");
list.add("联想");
list.add(0,"华为");
list.add(0,"诺基亚");//0代表插入的位置
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());

元素个数:5
[诺基亚, 华为, 小米, 苹果, 联想]

2、删除元素

//2、删除元素
list.remove("联想");
list.remove(0);
System.out.println("删除之后:"+list.size());
System.out.println(list.toString());

删除之后:3
[华为, 小米, 苹果]

3、遍历

for

System.out.println("-----------for----------");
for (int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i));
}

-----------for----------
华为
小米
苹果

增强for

System.out.println("-----------增强for---------");
for (Object object:list){
    System.out.println(object);
}

-----------增强for---------
华为
小米
苹果

迭代器

System.out.println("------------迭代器-----------");
for (Object o : list) {
    System.out.println(o);
}

------------迭代器-----------
华为
小米
苹果

列表迭代器:从前往后

//列表迭代器  和Iterator的区别:
//listIterator可以向后,向前遍历。可以添加,删除和修改元素。
System.out.println("-----------列表迭代器----------");
System.out.println("---------从前往后-----------");
ListIterator lit = list.listIterator();
while (lit.hasNext()){
    System.out.println(lit.nextIndex()+":"+lit.next());
}

-----------列表迭代器----------
---------从前往后-----------
0:华为
1:小米
2:苹果

列表迭代器:从后往前

把从前往后的next关键字换为previous关键字

System.out.println("---------从后往前-----------");
while (lit.hasPrevious()){
    System.out.println(lit.previousIndex()+":"+lit.previous());
}

---------从后往前-----------
2:苹果
1:小米
0:华为

4、判断

//4、判断
System.out.println("------------判断----------");
System.out.println(list.contains("苹果"));
System.out.println(list.isEmpty());

------------判断----------
true
false

5、获取位置

//5、获取位置
System.out.println("----------获取位置--------");
System.out.println(list.indexOf("华为"));

----------获取位置--------
0

{   //创建集合
   List list = new ArrayList();
        //1、添加数字数据
        list.add(20);
        list.add(30);
        list.add(40);
        list.add(50);
        list.add(60);
        list.add(70);
System.out.println("元素个数:"+list.size());
    System.out.println(list.toString());
    //2、删除操作
    //list.remove(0);   加的是下角标
    list.remove(new Integer(20));
    System.out.println("删除元素:"+list.size());
    System.out.println(list.toString());

    //3、补充方法sublist,返回子集和,含头含尾
    List list1 = list.subList(0, 3);
    System.out.println(list1.toString());

}

List实现类

ArrayList

数据结构实现,查询快、增删慢。

JDK1.2,运行速率快,线程不安全。

Vector

数组结构实现,查询快、增删慢。

Jdk1.0版本,运行速率慢、线程安全。

LinkedList

链表结构实现,增删快,查询快。

Arraylist(重点)

相当于一个数组

DEFAULT_CAPACITY=10;默认容量。

如果集合中没添加元素,默认容量是0。

每次扩容到原来的1.5倍。

elementDate存放元素的数组。

size实际的元素个数。

add添加元素。

public static void main(String[] args) {
    //创建集合
    ArrayList arrayList = new ArrayList<>();
    //1、添加元素
    Student s1 = new Student("小明",20);
    Student s2 = new Student("小白",10);
    Student s3 = new Student("小刚",25);
    Student s4 = new Student("小黑",34);
    arrayList.add(s1);
    arrayList.add(s2);
    arrayList.add(s3);
    System.out.println("元素个数:"+arrayList.size());
    System.out.println(arrayList.toString());
    //2、删除元素
    arrayList.remove(s1);
    //arrayList.remove(new Student("小白",10)); 不能用(改Student中的equals方法后才可以用)
    System.out.println("删除之后:"+arrayList.size());
    //3、遍历元素【重点】
    System.out.println("-------遍历元素--------");
        //迭代器
    System.out.println("--------迭代器--------");
    Iterator it = arrayList.iterator();
    while (it.hasNext()){
        Student s = (Student) it.next();
        System.out.println(s.toString());
    }
        //列表迭代器
    System.out.println("----------列表迭代器-------");
    ListIterator lit = arrayList.listIterator();
    while (lit.hasNext()){
        Student s = (Student) lit.next();
        System.out.println(s.toString());
    }
        //逆序遍历
    System.out.println("--------逆序遍历-------");
    while (lit.hasPrevious()){
        Student s = (Student) lit.previous();
        System.out.println(s.toString());
    }
    //4、查找
    System.out.println(arrayList.contains(new Student("小白",10)));
    System.out.println(arrayList.isEmpty());
    //5、判断
    System.out.println(arrayList.indexOf(new Student("小白",10)));

}

结构
元素个数:3
[Student{name='小明', sge=20}, Student{name='小白', sge=10}, Student{name='小刚', sge=25}]
删除之后:2
-------遍历元素--------
--------迭代器--------
Student{name='小白', sge=10}
Student{name='小刚', sge=25}
----------列表迭代器-------
Student{name='小白', sge=10}
Student{name='小刚', sge=25}
--------逆序遍历-------
Student{name='小刚', sge=25}
Student{name='小白', sge=10}
true
false
0

vector

public static void main(String[] args) {
    //创建集合
    Vector vector = new Vector();
    //1、添加元素
    vector.add("aa");
    vector.add("bb");
    vector.add("cc");
    vector.add("dd");
    vector.add("ee");
    //2、删除
    vector.remove(0);
    vector.remove("bb");
    System.out.println("个数:"+vector.size());
    //3、遍历
    //使用枚举器
    Enumeration en = vector.elements();
    while (en.hasMoreElements()){
        String o = (String) en.nextElement();
        System.out.println(o);
    }
    //4、判断
    System.out.println(vector.contains("dd"));
    System.out.println(vector.isEmpty());
    //5、其他方法
    System.out.println(vector.firstElement());
    System.out.println(vector.lastElement());

}

结果
个数:3
cc
dd
ee
true
false
cc
ee

Process finished with exit code 0

LinkedList

相当于一个双向链表

public static void main(String[] args) {
    //创建集合
    LinkedList linkedList = new LinkedList();
    //1、添加元素
    Student s1 = new Student("aaa",10);
    Student s2 = new Student("bbb",20);
    Student s3 = new Student("ccc",30);
    linkedList.add(s1);
    linkedList.add(s2);
    linkedList.add(s3);
    //linkedList.add(s3); 可以重复添加
    System.out.println("元素个数:"+linkedList.size());
    System.out.println(linkedList.toString());
    //2、删除
    linkedList.remove(s1);
    System.out.println(linkedList.size());
    //清空  linkedList.clear();
    //3、遍历
        //for遍历
    System.out.println("--------for--------");
    for (int i = 0; i < linkedList.size(); i++) {
        System.out.println(linkedList.get(i));
    }
        //增强for遍历
    System.out.println("--------增强for-------");
    for (Object object:linkedList){
        Student s = (Student)object;
        System.out.println(s.toString());
    }
        //迭代器
    System.out.println("----------迭代器---------");
    Iterator it = linkedList.iterator();
    while (it.hasNext()){
        Student s = (Student) it.next();
        System.out.println(s.toString());
    }
        //
    ListIterator listIterator = linkedList.listIterator();
    while (listIterator.hasNext()){
        Student s = (Student) listIterator.next();
        System.out.println(s.toString());
    }
    //4、判断
    System.out.println("--------判断------");
    System.out.println(linkedList.contains(s1));
    System.out.println(linkedList.isEmpty());
    //5、获取
    System.out.println("--------获取--------");
    System.out.println(linkedList.indexOf(s2));
}

结果
元素个数:3
[Student{name='aaa', sge=10}, Student{name='bbb', sge=20}, Student{name='ccc', sge=30}]
2
--------for--------
Student{name='bbb', sge=20}
Student{name='ccc', sge=30}
--------增强for-------
Student{name='bbb', sge=20}
Student{name='ccc', sge=30}
----------迭代器---------
Student{name='bbb', sge=20}
Student{name='ccc', sge=30}
Student{name='bbb', sge=20}
Student{name='ccc', sge=30}
--------判断------
false
false
--------获取--------
0

Process finished with exit code 0

泛型

本质是参数化类型,把类型作为参数传递。

常见形式有泛型类、泛型接口、泛型方法。

好处:提高代码的重用性。防止类型转换异常,提高代码的安全性。

泛型类

类名

T是占位符,表示一种引用类型。编写多个使用逗号隔开。

public class Demo1<T> {
    //使用泛型T
    //1、创建变量
    T t;
    //2、添加方法
    public void show(T t){
        System.out.println(t);
    }
    //3、泛型作为方法的返回值
    public T getT(){
        return t;
    }
}

注意:

泛型只能使用引用类型。不同的泛型类型对象之间不能相互复制。

public static void main(String[] args) {
    //使用泛型类创建对象
    //注意:泛型只能使用引用类型。不同泛型类型对象之间不能相互复制
    Demo1<String> demo1 = new Demo1<String>();
    demo1.t = "hello";
    demo1.show("大家好");
    String t = demo1.getT();
    System.out.println(t);

    Demo1<Integer> demo2 = new Demo1<Integer>();
    demo2.t=100;
    demo2.show(200);
    Integer integer = demo2.getT();

}

大家好
hello
200

泛型接口

接口

public interface Demo2<T> {
    String name = "张三";
    T server(T t);

}

1、无法改变类型

public class Test2 implements Demo2<String> {
    @Override
    public String server(String s) {
        System.out.println(s);
        return s;
    }
}

2、可以在创建对象的时候改变类型

public class Test2_2<T> implements Demo2<T>{
    @Override
    public T server(T t) {
        System.out.println(t);
        return null;
    }
}

main

Test2 test2 = new Test2();
test2.server("aaaaa");

Test2_2<Integer> test21 = new Test2_2<>();
test21.server(1000);

结果
aaaaa
1000

泛型方法

public <T> T show(T t){
    System.out.println("泛型方法"+t);
    return t;
}

类型可以根据所给类型转变

Demo3 demo3 = new Demo3();
demo3.show("aa");
demo3.show(100);

结果
泛型方法aa
泛型方法100

泛型集合

object 强转运行会报错

ArrayList arrayList = new ArrayList();
arrayList.add("aa");
arrayList.add("dd");
arrayList.add(10);
arrayList.add(20);
for(Object object:arrayList){
    String str = (String) object;
    System.out.println(object);
}

泛型集合在编译时,类型不对情况下便会显示错误。

ArrayList<String> arrayList2 = new ArrayList<>();
arrayList2.add("aa");
arrayList2.add("dd");
//arrayList2.add(11);  编译就报错
for (String s:arrayList2){
    System.out.println(s);
}

其他类调用,标明泛型类型

ArrayList<Student> arrayList1 = new ArrayList<>();
Student s1 = new Student("小明",20);
Student s2 = new Student("小白",10);
Student s3 = new Student("小刚",25);
Student s4 = new Student("小黑",34);
arrayList1.add(s1);
arrayList1.add(s2);
arrayList1.add(s3);
arrayList1.add(s4);
Iterator<Student> it = arrayList1.iterator();
while (it.hasNext()){
    Student s = it.next();
    System.out.println(s.toString());
}

Set子接口

无序,无下标,元素不可重复。

全部继承自collection

Set实现类

HashSet,TreeSet

//创建集合
Set<String> set = new HashSet<>();
//1、添加数据
set.add("aaa");
set.add("bbb");
set.add("ccc");
set.add("ddd");
System.out.println("个数:"+set.size());
System.out.println(set.toString());

//2、删除
set.remove("aaa");
//3、遍历
    //增强for
System.out.println("-----增强for-----");
for (String string:set
     ) {
    System.out.println(string);
}
    //迭代器
System.out.println("--------迭代器-------");
Iterator<String> it = set.iterator();
while (it.hasNext()){
    System.out.println(it.next());
}
//4、判断
System.out.println(set.contains("bbb"));
System.out.println(set.isEmpty());

HashSet

哈希表(数组+链表+红黑树)

不重复,无序

//新建集合
HashSet<String> hashSet = new HashSet<>();
//1、添加元素
hashSet.add("aaa");
hashSet.add("bbb");
hashSet.add("ccc");
hashSet.add("ddd");
hashSet.add("ddd");
System.out.println("个数:"+hashSet.size());
System.out.println(hashSet.toString());
//2、删除元素
hashSet.remove("aaa");
System.out.println("个数"+hashSet.size());
//3、遍历
System.out.println("-------增强for-------");
for (String string : hashSet) {
    System.out.println(string);
}

System.out.println("-------迭代器--------");
Iterator<String> it = hashSet.iterator();
while (it.hasNext()){
    System.out.println(it.next());
}
//4、判断
System.out.println(hashSet.contains("ccc"));
System.out.println(hashSet.isEmpty());

结果
个数:4
[aaa, ccc, bbb, ddd]
个数3
-------增强for-------
ccc
bbb
ddd
-------迭代器--------
ccc
bbb
ddd
true
false

Process finished with exit code 0

存储过程

  • 1、根据hashcode计算保存的位置。如果为空则直接保存。否则继续执行下面的
  • 2、再执行equals方法,如果equals为true,则认为重复。否则形成列表。

TreeSet

存储结构:红黑树

顺序存储,不会重复。

//1、创建集合
TreeSet<String> treeSet = new TreeSet<>();
//2、添加元素
treeSet.add("aaa");
treeSet.add("bbb");
treeSet.add("ccc");
treeSet.add("ddd");
System.out.println("元素个数:"+treeSet.size());
System.out.println(treeSet.toString());
//3、删除
treeSet.remove("aaa");
System.out.println("个数:"+treeSet.size());
//3、遍历
System.out.println("---------增强for-------");
for (String string : treeSet) {
    System.out.println(string);
}

System.out.println("---------迭代器--------");
Iterator<String> it = treeSet.iterator();
while (it.hasNext()){
    System.out.println(it.next());
}
//4、判断
System.out.println(treeSet.contains("aaa"));

结果
元素个数:4
[aaa, bbb, ccc, ddd]
个数:3
---------增强for-------
bbb
ccc
ddd
---------迭代器--------
bbb
ccc
ddd
false

Process finished with exit code 0

TreeSet保存数据

红黑树

要求元素必须要实现Comparable方法

CompareTo方法的返回为0,认为是重复,不添加。

Person类对CompareTo方法的实现

@Override
public int compareTo(Person o) {
    //先比较姓名,再比较年龄
    int n1 = this.getName().compareTo(o.getName());
    int n2 = this.age-o.getAge();

    return n1==0?n2:n1;

main方法

因为CompareTo是比较名字和年龄是否相同的。

所以即使是没有对象名的对象依然可以进行添加和比较。即,可以使用new出的进行添加和比较。

//创建
TreeSet<Person> person = new TreeSet<>();
//1、添加
Person p1 = new Person("aaa",10);
Person p2 = new Person("bbb",50);
Person p3 = new Person("ccc",15);
Person p4 = new Person("ddd",20);
person.add(p1);
person.add(p2);
person.add(p3);
person.add(p4);
System.out.println("个数:"+person.size());
System.out.println(person.toString());
//2、删除
person.remove(p1);

//比较的是是否重复,所以可以删除。
person.remove(new Person("bbb",50));
System.out.println(person.size());
//3、遍历
System.out.println("--------增强for---------");
for (Person p : person) {
    System.out.println(p.toString());
}
System.out.println("--------迭代器--------");
Iterator<Person> it = person.iterator();
while (it.hasNext()){
    System.out.println(it.next());
}
//4、判断
System.out.println(person.contains(p3));

//比较名字年龄是否相同,可以比较
System.out.println(person.contains(new Person("ccc",15)));

结果
个数:4
[Person{name='aaa', age=10}, Person{name='bbb', age=50}, Person{name='ccc', age=15}, Person{name='ddd', age=20}]
2
--------增强for---------
Person{name='ccc', age=15}
Person{name='ddd', age=20}
--------迭代器--------
Person{name='ccc', age=15}
Person{name='ddd', age=20}
true
true

Process finished with exit code 0

compare

认为,当返回值为负数时,左边小于右边,左边排在右边的上面。

案例

public static void main(String[] args) {
    //定制比较器
    TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            int n1 = o1.length()-o2.length();
            int n2 = o1.compareTo(o2);

            return n1==0?n2:n1;
        }
    });
    treeSet.add("helloworld");
    treeSet.add("zhang");
    treeSet.add("lisi");
    treeSet.add("wangwu");
    treeSet.add("beijing");
    treeSet.add("xian");
    treeSet.add("nanjing");
    System.out.println(treeSet.toString());

}

结果
[lisi, xian, zhang, wangwu, beijing, nanjing, helloworld]

Map

用于存储任意键值对。

键:无序、无下标、不允许重复。

值:无序、无下标、允许重复。

添加元素用map.put

public static void main(String[] args) {
    //创建Map集合
    Map<String,String> map = new HashMap<>();
    //1、添加元素
    map.put("cn","中国");
    map.put("uk","英国");
    map.put("usa","美国");
    map.put("cn","中国");
    System.out.println("元素个数:"+map.size());
    System.out.println(map.toString());
    //2、删除
    map.remove("usa");
    System.out.println("个数:"+map.size());
    //3、遍历
    System.out.println("--------keyset--------");
    //Set<String> keyset = map.keySet();
    for (String key:map.keySet()) {
        System.out.println(key+"----"+map.get(key));
    }

    //entry是映射对     效率更高
    System.out.println("-------entrySet-----");
    Set<Map.Entry<String,String>> entries = map.entrySet();
    for (Map.Entry<String, String> entry : entries) {
        System.out.println(entry.getKey()+"----"+entry.getValue());
    }

    //4、判断
    System.out.println(map.containsKey("cn"));
    System.out.println(map.containsValue("英国"));

}

结果
元素个数:3
{usa=美国, uk=英国, cn=中国}
个数:2
--------keyset--------
uk----英国
cn----中国
-------entrySet-----
uk----英国
cn----中国
true
true

Process finished with exit code 0

HashMap

存储结构:哈希表(数组+链表+红黑树)

添加的时候,编译从上往下添加,打印从下往上打印。

key重复会覆盖Value的值,即使没有对象名的对象依然可以覆盖有对象名的对象的Value。

值(Value)可以重复。key不可以重复。

不重写hashcode和equals的话,根据对象名判断是否重复,此时可以用new创建新的对象。重写hashcode和equals后根据内容判断是否重复。

刚创建hashmap集合时,没有添加元素table=null

size = 0。

public static void main(String[] args) {
    //创建集合
    HashMap<Student,String> students = new HashMap<Student,String>();
    //添加对象:从下往上添加
    //key重复会覆盖Value值,即使是无对象名的对象也可以把有对象名的对象覆盖
    Student s1=new Student("aa",10);
    Student s2=new Student("bb",20);
    Student s3=new Student("cc",30);
    students.put(s1,"北京");
    students.put(s2,"北京");//值可以重复
    students.put(s3,"杭州");//key不可以重复
    students.put(s3,"天津");
    //判断是否相同通过对象名判断,而不通过内容。若想相同的元素不加上来重写equals方法
    students.put(new Student("cc",30),"南京");
    System.out.println("个数:"+students.size());
    System.out.println(students.toString());
    //删除
    students.remove(s1);
    System.out.println("个数:"+students.size());
    //遍历
    System.out.println("--------keySet------");
    for (Student key : students.keySet()){
        System.out.println(key.toString()+"----"+students.get(key));
    }
    System.out.println("-------entrySet------");
    for (Map.Entry<Student,String> entry:students.entrySet()) {
        System.out.println(entry.getKey()+"----"+entry.getValue());
    }

    //判断
    System.out.println(students.containsKey(s1));
    System.out.println(students.containsValue("南京"));

}

结果
个数:3
{Student{name='bb', stuNo=20}=北京, Student{name='aa', stuNo=10}=北京, Student{name='cc', stuNo=30}=南京}
个数:2
--------keySet------
Student{name='bb', stuNo=20}----北京
Student{name='cc', stuNo=30}----南京
-------entrySet------
Student{name='bb', stuNo=20}----北京
Student{name='cc', stuNo=30}----南京
false
true

Process finished with exit code 0

1、hashmap刚创建,table是null,为节省空间,当添加第一个元素时,table容量调整为16。

2、当元素阈值(16*.75=12)时,会进行扩容,扩容后大小为原来的2倍。目的是减少调整元素的个数。

3、jdk1.8 当每个链表长度大于8,并且元素个数大于等于64时,会调整红黑树,目的是提高效率

4、jdk1.8 当链表长度小于16,调整链表

5、jdk1.8之前是链表头插插入,jdk108之后是链表尾插入

Colletions工具类

reverse 反转集合中元素的顺序。

shuffle 随机重置集合元素的顺序。

sort 升序排序(元素类型必须实现Comparable接口)

List<Integer> list = new ArrayList<>();
list.add(20);
list.add(22);
list.add(50);
list.add(51);
list.add(64);
list.add(82);
list.add(14);
//sort排序
System.out.println("sort排序之前:"+list.toString());
Collections.sort(list);
System.out.println("sort排序之后:"+list.toString());
//bineraySearch 二分查找
int i = Collections.binarySearch(list,50);
System.out.println(i);
//copy复制
List<Integer> dest = new ArrayList<>();
for (int i1=0;i1<list.size();i1++){
    dest.add(0);
}
Collections.copy(dest,list);
System.out.println(dest.toString());

//reverse反转
Collections.reverse(list);
System.out.println("反转之后:"+list);

//shuffle打乱
Collections.shuffle(list);
System.out.println("打乱之后:"+list);


结果
sort排序之前:[20, 22, 50, 51, 64, 82, 14]
sort排序之后:[14, 20, 22, 50, 51, 64, 82]
3
[14, 20, 22, 50, 51, 64, 82]
反转之后:[82, 64, 51, 50, 22, 20, 14]
打乱之后:[20, 50, 14, 51, 64, 22, 82]

list转成数组

数组长度小于list数组。输出数组长度等于list长度。若大于则多出的为null。

// 数组长度小于list数组。输出数组长度等于list长度。若大于则多出的为null
Integer[] arr = list.toArray(new Integer[0]);
System.out.println(arr.length);
System.out.println(Arrays.toString(arr));

结果
7
[20, 50, 14, 51, 64, 22, 82]

数组转成集合

数组转成的集合是一个受限集合,不能添加和删除。

把基本类型数组转成集合时,需要修改为包装类型。

String[] names = {"张三","李四","王五"};
//数组转成的集合是一个受限集合,不能添加和删除
List<String> list1 = Arrays.asList(names);
System.out.println(list1);

//把基本类型数组转成集合时,需要修改为包装类型
Integer[] nums = {100,200,300,500,600,800};
List<Integer> list2= Arrays.asList(nums);
System.out.println(list2);

结果
[张三, 李四, 王五]
[100, 200, 300, 500, 600, 800]

总结

集合:对象的容器,和数据类似。定义了多个对象进行操作的常用方法。

list集合:有序,有下标,元素可以重复。

ArrayList,LinkedList,Vector。

Set集合:无序,无下标,元素不可重复。

HashSet(数组+链表+红黑树),TreeSet(红黑树)。

Map集合:存储一对数据,无序,无下标,键不可重复,值可以重复。

HashMap(哈希表),HashTable,TreeMap(红黑树)。

Collections:集合工具类,定义了除啦存取以外的集合常用方法。

posted @ 2021-10-23 22:25  星星淮  阅读(48)  评论(0)    收藏  举报