Java语法笔记(八):常用容器
List
接口:
java.util.List<>
实现:
java.util.ArrayList<>
:变长数组java.util.LinkedList<>
:双链表
函数:
add()
:在末尾添加一个元素输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package org.ep;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
System.out.println(list);
}
}1
[1, 2, 3]
clear()
:清空输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package org.ep;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.clear();
System.out.println(list);
}
}1
[]
size()
:返回长度输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package org.ep;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
System.out.println(list);
System.out.println(list.size());
}
}1
2[1, 2, 3]
3isEmpty()
:是否为空输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package org.ep;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
System.out.println(list.size());
System.out.println(list.isEmpty());
list.clear();
System.out.println(list.isEmpty());
}
}1
2
33
false
trueget(i)
:获取第i个元素输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package org.ep;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
System.out.println(list.get(0));
}
}1
1
set(i, val)
:将第i个元素设置为val输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package org.ep;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.set(2, 10);
System.out.println(list);
}
}1
[1, 2, 10]
栈
类:
java.util.Stack<>
函数:
push()
:压入元素1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package org.ep;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stk = new Stack<>();
stk.push(1);
stk.push(2);
stk.push(10);
System.out.println(stk);
}
}1
[1, 2, 10]
pop()
:弹出栈顶元素,并返回栈顶元素输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package org.ep;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stk = new Stack<>();
stk.push(1);
stk.push(2);
stk.push(10);
System.out.println(stk.pop());
}
}1
10
peek()
:返回栈顶元素输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package org.ep;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stk = new Stack<>();
stk.push(1);
stk.push(2);
stk.push(10);
System.out.println(stk.pop());
System.out.println(stk.peek());
}
}1
2
size()
:返回长度输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package org.ep;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stk = new Stack<>();
stk.push(1);
stk.push(2);
stk.push(10);
System.out.println(stk.pop());
System.out.println(stk.size());
}
}1
210
2empty()
:栈是否为空输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package org.ep;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stk = new Stack<>();
stk.push(1);
stk.push(2);
stk.push(10);
System.out.println(stk.empty());
}
}1
false
clear()
:清空输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package org.ep;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stk = new Stack<>();
stk.push(1);
stk.push(2);
stk.push(10);
System.out.println(stk.empty());
stk.clear();
System.out.println(stk.empty());
}
}1
2false
true
队列
接口:
java.util.Queue<>
实现:
java.util.LinkedList<>
:双链表java.util.PriorityQueue<>
:优先队列- 默认是小根堆,大根堆写法:
new PriorityQueue<>(Collections.reverseOrder())
- 默认是小根堆,大根堆写法:
函数:
add()
:在队尾添加元素输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package org.ep;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(1);
q.add(2);
q.add(10);
System.out.println(q);
}
}1
[1, 2, 10]
remove()
:删除并返回队头输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package org.ep;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(1);
q.add(2);
q.add(10);
System.out.println(q.remove());
System.out.println(q);
}
}1
21
[2, 10]isEmpty()
:是否为空输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package org.ep;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(1);
q.add(2);
q.add(10);
System.out.println(q.isEmpty());
}
}1
false
size()
:返回长度输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package org.ep;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(1);
q.add(2);
q.add(10);
System.out.println(q.size());
}
}1
3
peek()
:返回队头输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package org.ep;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(1);
q.add(2);
q.add(10);
System.out.println(q.peek());
}
}1
1
clear()
:清空输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package org.ep;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(1);
q.add(2);
q.add(10);
q.clear();
System.out.println(q.isEmpty());
}
}1
true
Set
接口:
java.util.Set<K>
实现:
java.util.HashSet<K>
:哈希表(无序)java.util.TreeSet<K>
:平衡树(有序)
函数:
add()
:添加元素contains()
:是否包含某个元素输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package org.ep;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(10);
System.out.println(set.contains(2));
System.out.println(set.contains(5));
}
}1
2true
falseremove()
:删除元素输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package org.ep;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(10);
set.remove(2);
System.out.println(set.contains(2));
System.out.println(set.contains(5));
}
}1
2false
falsesize()
:返回元素数输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package org.ep;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(10);
set.remove(2);
System.out.println(set.size());
}
}1
2
isEmpty()
:是否为空输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package org.ep;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(10);
System.out.println(set.isEmpty());
}
}1
false
clear()
:清空输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package org.ep;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(10);
System.out.println(set.isEmpty());
set.clear();
System.out.println(set.isEmpty());
}
}1
2false
true
java.util.TreeSet
多的函数:
ceiling(key)
:返回大于等于key的最小元素,不存在则返回null输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package org.ep;
import sun.reflect.generics.tree.Tree;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
set.add(123);
set.add(231);
set.add(321);
System.out.println(set.ceiling(100));
}
}1
123
floor(key)
:返回小于等于key的最大元素,不存在则返回null输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package org.ep;
import sun.reflect.generics.tree.Tree;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>();
set.add(123);
set.add(231);
set.add(321);
System.out.println(set.floor(100));
}
}1
null
Map
接口:
java.util.Map<K, V>
实现:
java.util.HashMap<K, V>
:哈希表java.util.TreeMap<K, V>
:平衡树
函数:
put(key, value)
:添加关键字和其对应的值get(key)
:返回关键字对应的值输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package org.ep;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("123", 2);
map.put("321", 3);
map.put("21", 10);
System.out.println(map.get("123"));
System.out.println(map.get("12"));
}
}1
22
nullcontainsKey(key)
:是否包含关键字输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package org.ep;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("123", 2);
map.put("321", 3);
map.put("21", 10);
System.out.println(map.containsKey("1234"));
}
}1
false
remove(key)
:删除关键字输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package org.ep;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("123", 2);
map.put("321", 3);
map.put("21", 10);
map.remove("123")
System.out.println(map.containsKey("123"));
}
}1
false
size()
:返回元素数isEmpty()
:是否为空clear()
:清空输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package org.ep;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("123", 2);
map.put("321", 3);
map.put("21", 10);
System.out.println(map.size());
map.clear();
System.out.println(map.isEmpty());
}
}1
23
trueentrySet()
:获取Map中的所有对象的集合Map.Entry<K, V>
:Map中的对象类型getKey()
:获取关键字getValue()
:获取值输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package org.ep;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("123", 2);
map.put("321", 3);
map.put("21", 10);
for (Map.Entry<String, Integer> entry: map.entrySet())
System.out.printf("%s %d\n", entry.getKey(), entry.getValue());
}
}1
2
3123 2
321 3
21 10
java.util.TreeMap<K, V>
多的函数:
ceilingEntry(key)
:返回大于等于key的最小元素,不存在则返回nullfloorEntry(key)
:返回小于等于key的最大元素,不存在则返回null
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 EpisodeXIII!