java-day08

  1. 1. 集合
    1. 1.1. 含义
    2. 1.2. 数组特点
    3. 1.3. 集合特点
    4. 1.4. 示意图
  2. 2. ArrayList
    1. 2.1. 含义
    2. 2.2. 底层
    3. 2.3. 体系结构
    4. 2.4. 常用方法
  3. 3. 泛型
    1. 3.1. 含义
    2. 3.2. 好处
    3. 3.3. 使用场景
    4. 3.4. 案例一
    5. 3.5. 案例二
    6. 3.6. 案例三
  4. 4. Vector
    1. 4.1. 含义
    2. 4.2. vs ArrayList
    3. 4.3. 体系结构
    4. 4.4. 常用方法
    5. 4.5. 案例
  5. 5. Stack
    1. 5.1. 含义
    2. 5.2. 体系
    3. 5.3. 底层
    4. 5.4. 常用方法
    5. 5.5. 案例
  6. 6. LinkedList
    1. 6.1. 含义
    2. 6.2. 示意图
    3. 6.3. 底层
    4. 6.4. 体系结构
    5. 6.5. 常用方法
    6. 6.6. 案例一
    7. 6.7. 案例二
    8. 6.8. vs ArrayList
  7. 7. 单向队列[了解]
    1. 7.1. 特点
    2. 7.2. 案例
  8. 8. 双向队列[了解]
  9. 9. HashMap 示意代码
  10. 10. HashMap
    1. 10.1. 含义
    2. 10.2. 底层
    3. 10.3. 示意图
    4. 10.4. 体系结构
    5. 10.5. 常用方法
    6. 10.6. 案例一
    7. 10.7. 案例二
    8. 10.8. 案例三
  11. 11. LinkedHashMap
    1. 11.1. 含义
    2. 11.2. 体系结构
    3. 11.3. 案例
  12. 12. Hashtable
    1. 12.1. 含义
    2. 12.2. 体系结构
    3. 12.3. 和HashMap的区别
    4. 12.4. 案例

集合

含义

容器,位于java.util包下面的类

数组特点

  • 大小固定[不会自动扩容]
  • 可以存基本数据、引用数据类型

集合特点

  • 大小不固定[自动扩容]
  • 只能存放引用数据类型

示意图

ArrayList

含义

是动态变长的数组

底层

是动态变长的数组

体系结构

  • Collection
    • List
      • ArrayList

常用方法

  • isEmpty
  • size
  • add
  • remove
  • set
  • contains
  • get
  • clear
  • toArray
  • addAll 将集合参数的元素逐个加入
  • removeAll 将集合参数中的元素中出现的逐个删除
  • containsAll 判断集合参数中的元素释放全部都存在
  • indexOf 获取元素第一次出现的位置
  • lastIndexOf 获取元素最后一次出现的位置
  • 等等

泛型

含义

参数化类型(将操作的数据类型放到<>中)

好处

  • 明确操作类型
  • 避免强制转换

使用场景

  • 类、接口
  • 方法

案例一

Animal.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Animal<T> {

private T content;

public void print(T content){
this.content = content;
System.out.println(content);
}

public void say(){
System.out.println(content);
}

public T getContent(){
return content;
}
}

GenericDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class GenericDemo {

//泛型
//含义:参数化类型
//将操作的类型放到<操作的数据类型>
//使用场景
//类、接口
//方法
//注意
//集合中大量使用
//好处
//明确操作的数据类型
//避免强制转换


public static void main(String[] args) {

Animal<String> animal = new Animal<String>();

animal.print("11");
animal.print("sdfsdf");


Animal<Integer> other = new Animal<Integer>();

other.print(1);

Integer xx = other.getContent();
}

}

案例二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class _02GenericDemo {

public static void main(String[] args) {

Integer result = getData(true);
System.out.println(result);

String otherReuslt =getData(false);
System.out.println(otherReuslt);


String data = getData("xxx");
}

public static <T> T getData(boolean sign){

if(sign == true){
return (T) new Integer(11);
}else{
return (T) "xx";
}
}


public static <T> T getData(T content){
return content;
}
}

案例三

User.java

1
2
3
public class User {

}

Student.java

1
2
3
public class Student extends User {

}

_03GenericDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class _03GenericDemo {

public static void main(String[] args) {

List<Integer> data = new ArrayList<Integer>();

data.add(11);

int result = data.get(0);

System.out.println(result);


List<String> otherData = new ArrayList<String>();
add(data);

add(otherData);


List<User> userData = new ArrayList<User>();
List<Student> studentData = new ArrayList<Student>();

append(userData);
append(studentData);


insert(userData);
insert(studentData);
}

//?不确定
public static void add(List<?> data){

}

//?不确定
//<? extends User>
//User的子类 或者 User 本身

public static void append(List<? extends User> data){

}

//?不确定
//<? super Student>
//Student的父类 或者 Student 本身
public static void insert(List<? super Student> data){

}
}

Vector

含义

线程安全的ArrayList,使用是上和ArrayList使用差不多

vs ArrayList

  • 都实现List接口
  • 底层都是采用可变数组存储
  • vector是线程安全的

体系结构

  • Collection
    • List
      • Vector

常用方法

  • add addAll
  • remove removeAll
  • contains containsAll
  • set
  • get
  • isEmpty
  • size
  • indexOf lastIndexOf
  • toArray
  • clear
  • 等等

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class VectorDemo {

//含义
//线程安全的ArrayList

//VS ArrayList
//vector几乎都有synchronized[同步,也就是线程安全的]

//底层
//可变数组

//体系结构
//Collection
//List
//Vector
//常用方法
//add addAll
//remove removeAll
//contains containsAll
//set
//get
//isEmpty
//size
//indexOf lastIndexOf
//toArray
//clear
//等等


public static void main(String[] args) {

Vector<String> data = new Vector<>();

data.add("11");


System.out.println(data.get(0));

}
}

Stack

含义

栈数据结构

体系

  • Collection
    • List
      • Vector
        • Stack

底层

本质采用可变数组

常用方法

  • empty()
  • push
  • pop
  • peek

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class StackDemo {

//Stack
//含义:栈数据结构
//体系
//Collection
//List
//Vector
//Stack
//底层:可变数组

//常用方法
//顶部加、获取

public static void main(String[] args) {

Stack<String> stack = new Stack<>();

stack.push("11");
stack.push("22");
stack.push("33");

//返回栈顶元素,不会移除
System.out.println(stack.peek());

System.out.println(stack.pop());

System.out.println(stack.empty());
System.out.println(stack);
}
}

LinkedList

含义

JDK提供的类,该类实现双向链表数据结构

示意图

底层

双向链表

体系结构

  • Collection
    • List
      • LinkedList

常用方法

和ArrayList差不多

案例一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class _01LinkedListDemo {

//LinkedList
//含义:双向链表【循环吗???】

//体系结构
//Collection
//List
//LinkedList
//常用的方法
//add addAll
//remove removeAll
//contains containsAll
//isEmpty
//size
//toArray
//set
//get
//clear
//iterator
//等等


//ArrayList
//有下标说法,因为底层采用数组
//LinkedList
//没有下标


public static void main(String[] args) {

LinkedList<String> data = new LinkedList<String>();
data.add("11"); //data.addLast
data.add("22");
data.add("33");

System.out.println(data.get(0));
}
}

案例二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class _02Compare {


public static void main(String[] args) {

add(100000);
}


public static void add(int max){

long start,end;

List<Integer> arrayList = new ArrayList<>();

start = System.currentTimeMillis();
for(int i=0;i<max;i++){
arrayList.add(0,i);
}
end = System.currentTimeMillis();

System.out.println("arrayList的插入时间:"+(end-start));


List<Integer> linkedList = new LinkedList<>();

start = System.currentTimeMillis();
for(int i=0;i<max;i++){
linkedList.add(0,i);
}
end = System.currentTimeMillis();

System.out.println("linkedList的插入时间:"+(end-start));
}


//查询
//下标 ArrayList 块
//下标(位置) LinkedList 慢

//元素
//差不多

//更新
//依赖于查询

//删除
//TODO
}

vs ArrayList

ArrayList LinkedList
数据结构 数组 链表
是否有索引 没有
根据索引查询 慢(只能遍历后比较位置)
更新 快(通过索引快速找到更新) 慢(只能遍历找到后更新)
删除 慢(需要移动位置) 快(不需要移动位置)
添加 慢(需要移动位置) 快(不需要移动位置)

单向队列[了解]

特点

FIFO\LILO

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class QueueDemo {

//含义:单向队列
//特点
//FIFO LILO
//自己实现
//数组
//链表
//JDK已经使用链表实现了队列


public static void main(String[] args) {

Queue<Integer> queue = new LinkedList<>();


//add(e) offer(e) 作用向队列的尾部添加元素

queue.add(1); //1
queue.offer(2); //2
queue.offer(3); //3

System.out.println(queue);

queue.remove(); //删除并返回队列的头[如果队列是空的会抛出异常]
System.out.println(queue);

queue.poll(); //删除并返回队列的头[如果队列是空的返回null]
System.out.println(queue);


int result = queue.element(); //返回队列的头[如果队列是空的会抛出异常]
result = queue.peek(); //返回队列的头[如果队列是空的返回null]

System.out.println(result);



//常用的操作
//头部移除元素(获取)
//尾部添加元素
}
}

双向队列[了解]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class DequeDemo {

//含义
//双向队列


//常用的操作
//往两边添加、删除
public static void main(String[] args) {

Deque<Integer> data = new LinkedList<>();

//TODO

}
}

HashMap 示意代码

MyHashMap.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class MyHashMap<K,V> {

private final int INIT_CAPACITY=1000;


private Node<K,V> [] container = new Node[INIT_CAPACITY];

public MyHashMap(){

}


public void put(K key,V value){

//以key的hascode作为下标
int code = key.hashCode()>>2;

//根据hashcode从数组中查询
Node<K,V> oldNode = container[code];

//没有构建一个节点并且放入到数组中
if(oldNode == null){
Node<K,V> node = new Node<K,V>();
node.hashCode = code;
node.key=key;
node.value = value;
container[code] = node;
}else{
//hash冲突??

K oldKey = oldNode.key;
if(oldKey.equals(key)){
//替换
oldNode.value=value;
}else{

Node<K,V> node = new Node<K,V>();
node.hashCode = code;
node.key=key;
node.value = value;

oldNode.next=node;

}
}
}


public Object get(K key){
int code = (key.hashCode()>>2);
return container[code].value;
}
}

class Node<K,V>{
int hashCode;
K key;
V value;

Node<K,V> next;
}

Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.neu.day08._06map;

public class Test {

//HashMap
//底层:数组 + 单向链表
//特点
//查找速度快

//1000
public static void main(String[] args) {

//[null,{one,xx,next={two,yy}},null,,,,,,,,,,,,,,]

MyHashMap<User, String> data = new MyHashMap<>();

User one = new User(100);

data.put(one, "xx");


User two = new User(100);

data.put(two, "yy");

}

}

class User{
int num;

public User(int num){
this.num = num;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + num;
return result;
}
}

HashMap

含义

key-value形式

底层

数组+链表

示意图

体系结构

  • Map
    • HashMap

常用方法

  • size
  • put
  • putAll
  • get
  • size
  • isEmpty
  • clear
  • containsKey
  • containsValue
  • remove
  • 等等

案例一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class _01HashMapDemo {

//含义:key-value形式??

//体系结构
//Map
//HashMap

//常用方法
//size
//put
//get
//isEmpty
//....


public static void main(String[] args) {

HashMap<String,String> data = new HashMap<>();

data.put("name","zs");
data.put("age","10");
data.put("address","guangzhou");

System.out.println(data.get("name")); //zs

data.remove("age");
System.out.println(data);

System.out.println(data.containsKey("name"));//true
System.out.println(data.containsValue("zs"));//true



//data.clear();

System.out.println(data);

Map<String,String> temp = new HashMap<>();
temp.put("color", "red");
temp.put("size", "xl");
data.putAll(temp);

System.out.println(data);

System.out.println(data.size());

//遍历
}
}

案例二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class _02HashMapDemo {

public static void main(String[] args) {

HashMap<String,String> data = new HashMap<>();

data.put("name","zs");
data.put("age","10");
data.put("address","guangzhou");

//遍历

System.out.println("方式一................");
Set<String> keys = data.keySet();
for(String key : keys){
System.out.println(key+"="+data.get(key));
}

System.out.println("方式二................");

Collection<String> values = data.values();
for(String value : values){
System.out.println(value);
}

System.out.println("方式三................");
Set<Entry<String, String>> entrySet = data.entrySet();

for(Entry<String, String> entry : entrySet){
System.out.println(entry.getKey()+"="+entry.getValue());
}

}
}

案例三

验证HashMap是否有序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class _03HashMapDemo {

public static void main(String[] args) {

Map<String,String> data = new HashMap<>();

data.put("33","33");
data.put("hhhhhh","33");
data.put("aa","aaaa");
data.put("xx","xxxx");
data.put("yy","yyyy");
data.put("yy","1000");
data.put("11","11");
data.put("22","22");

System.out.println(data);

}
}

LinkedHashMap

含义

有序的hashMap

体系结构

  • Map
    • HashMap
      • LinkedHashMap

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class _04LinkedHashMap {

//LinkedHashMap
//含义:有序的hashMap
//体系结构
//Map
//HashMap
//LinkedHashMap

public static void main(String[] args) {


Map<String,String> data = new LinkedHashMap<>();


data.put("33","33");
data.put("hhhhhh","33");
data.put("aa","aaaa");
data.put("xx","xxxx");
data.put("yy","yyyy");
data.put("yy","1000");
data.put("11","11");
data.put("22","22");

System.out.println(data);
}
}

Hashtable

含义

重量级HashMap,用法和HashMap类似

体系结构

  • Map
    • Hashtable

和HashMap的区别

  • Hashtable的方法几乎都有synchronized【线程安全】
  • HashMap的key和value都是可以为null,Hashtable的key和value都不可以为空

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class _05HashtableDemo {

//含义:
//和HashMap类似
//体系
//Map
//Hashtable
//区别
//Hashtable的方法几乎都有synchronized【线程安全】
//HashMap的key和value都是可以为null,Hashtable的key和value都不可以为空


public static void main(String[] args) {

Hashtable<String,String> data = new Hashtable<>();

data.put("xx", "xxxx");
data.put("yy", "yyyy");
data.put("zz", "zzzz");

//data.put(null,null);
System.out.println(data);


HashMap<String,String> xx = new HashMap<>();
xx.put(null,null);
}
}