Java操作蓝图:常用数据结构与方法
这是为了可以快速使用各种常用数据结构和方法的蓝图集合,通过给出一个空框架,提高效率。
最近刷题陷入了迷茫,往往不知道该用什么数据结构,所以我决定写一个蓝图,方便以后使用。
目录
- 数组(Array)
- 列表(List/ArrayList)
- 链表(LinkedList)
- 栈(Stack)
- 队列(Queue)
- 优先队列(PriorityQueue)
- 集合(Set/HashSet)
- 有序集合(TreeSet)
- 映射(Map/HashMap)
- 有序映射(TreeMap)
- 字符串操作(String)
- 工具类(Collections/Arrays)
接下来我对以上12个常用数据结构的用途做下简单的描述
数组:
- 用途:固定大小的连续内存空间,存储相同类型元素
- 解决问题:快速随机访问,内存紧凑
列表:
- 用途:动态数组,自动扩容
- 解决问题:需要动态调整大小的数组
链表:
- 用途:双向链表实现,高效插入删除
- 解决问题:频繁在列表中间进行插入/删除操作
栈:
- 用途:后进先出(LIFO)结构
- 解决问题:需要后进先出逻辑的场景,如撤销操作、括号匹配
队列:
- 用途:先进先出(FIFO)结构
- 解决问题:任务调度、广度优先搜索等
优先队列:
- 用途:元素按照优先级排序的队列
- 解决问题:需要按照优先级处理元素的场景
集合:
- 用途:存储唯一元素的集合
- 解决问题:去重、查找
有序集合:
- 用途:元素按照自然顺序或自定义顺序排序的集合
- 解决问题:需要元素有序的场景
映射:
- 用途:键值对存储
- 解决问题:根据键快速查找值
有序映射:
- 用途:键值对按照键的自然顺序或自定义顺序排序
- 解决问题:需要键值对有序的场景
字符串操作:
- 用途:处理文本数据
- 解决问题:字符串拼接、查找、替换
工具类:
- 用途:提供各种常用操作的静态方法
- 解决问题:简化常见操作,如排序、查找、比较等
数组(Array)
用途:固定大小的连续内存空间,存储相同类型元素
解决问题:快速随机访问,内存紧凑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| int[] arr = new int[10]; int[] arr2 = {1, 2, 3};
arr[0] = 5; int length = arr.length;
for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
for (int num : arr) { System.out.println(num); }
int[][] matrix = new int[3][3]; matrix[0][0] = 1;
|
列表(List/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
| List<String> list = new ArrayList<>();
list.add("Apple"); list.add(0, "Banana");
String fruit = list.get(0);
list.set(0, "Orange");
list.remove(0); list.remove("Orange");
int size = list.size(); boolean isEmpty = list.isEmpty();
boolean contains = list.contains("Apple");
for (String item : list) { System.out.println(item); }
Iterator<String> it = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); }
String[] array = list.toArray(new String[0]);
|
链表(LinkedList)
用途:双向链表实现,高效插入删除
解决问题:频繁在列表中间进行插入/删除操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| List<String> linkedList = new LinkedList<>();
linkedList.add("A"); linkedList.addFirst("B"); linkedList.addLast("C");
String first = linkedList.getFirst(); String last = linkedList.getLast();
linkedList.removeFirst(); linkedList.removeLast();
Queue<String> queue = new LinkedList<>(); queue.offer("A"); queue.poll();
|
栈(Stack)
用途:后进先出(LIFO)结构
解决问题:需要后进先出逻辑的场景,如撤销操作、括号匹配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Deque<Integer> stack = new ArrayDeque<>();
stack.push(1); stack.push(2);
int top = stack.pop();
int peek = stack.peek();
boolean empty = stack.isEmpty();
|
队列(Queue)
用途:先进先出(FIFO)结构
解决问题:任务调度、广度优先搜索等
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Queue<String> queue = new LinkedList<>();
queue.offer("A"); queue.add("B");
String item = queue.poll(); String item2 = queue.remove();
String head = queue.peek(); String head2 = queue.element();
|
优先队列(PriorityQueue)
用途:按优先级出队的队列
解决问题:需要按特定顺序处理元素的场景,如任务调度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| PriorityQueue<Integer> pq = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
pq.offer(5); pq.add(3);
int min = pq.poll();
int peek = pq.peek();
|
集合(Set/HashSet)
用途:不包含重复元素的集合
解决问题:去重、快速成员检查
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
| Set<String> set = new HashSet<>();
set.add("Apple"); set.add("Banana");
boolean hasApple = set.contains("Apple");
set.remove("Apple");
int size = set.size();
for (String item : set) { System.out.println(item); }
Set<String> otherSet = new HashSet<>(Arrays.asList("Banana", "Orange"));
set.retainAll(otherSet); set.addAll(otherSet); set.removeAll(otherSet);
|
有序集合(TreeSet)
用途:保持元素有序的集合
解决问题:需要有序遍历或范围查询的场景
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| Set<Integer> treeSet = new TreeSet<>();
Set<Integer> descSet = new TreeSet<>((a, b) -> b - a);
treeSet.add(3); treeSet.add(1); treeSet.add(2);
int first = ((TreeSet<Integer>) treeSet).first(); int last = ((TreeSet<Integer>) treeSet).last();
Set<Integer> subset = ((TreeSet<Integer>) treeSet).subSet(1, 3);
|
映射(Map/HashMap)
用途:键值对存储结构
解决问题:快速键值查找、关联数据存储
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
| Map<String, Integer> map = new HashMap<>();
map.put("Apple", 10); map.put("Banana", 20);
int appleCount = map.get("Apple");
boolean hasApple = map.containsKey("Apple");
boolean has10 = map.containsValue(10);
map.remove("Apple");
for (String key : map.keySet()) { System.out.println(key); }
for (Integer value : map.values()) { System.out.println(value); }
for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); }
int count = map.getOrDefault("Orange", 0);
map.merge("Apple", 1, Integer::sum);
|
有序映射(TreeMap)
用途:保持键有序的映射
解决问题:需要按键顺序遍历或范围查询的场景
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Map<String, Integer> treeMap = new TreeMap<>();
Map<String, Integer> descMap = new TreeMap<>(Comparator.reverseOrder());
treeMap.put("Banana", 20); treeMap.put("Apple", 10);
String firstKey = ((TreeMap<String, Integer>) treeMap).firstKey(); String lastKey = ((TreeMap<String, Integer>) treeMap).lastKey();
Map<String, Integer> subMap = ((TreeMap<String, Integer>) treeMap).subMap("A", "C");
|
字符串操作(String)
用途:文本处理和操作
解决问题:各种字符串处理需求
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
| String str = "Hello, World!";
int length = str.length(); char ch = str.charAt(0); String sub = str.substring(0, 5);
int index = str.indexOf("World"); int lastIndex = str.lastIndexOf("o");
String newStr = str.replace("World", "Java");
String[] parts = str.split(",");
String joined = String.join("-", "A", "B", "C");
String formatted = String.format("Name: %s, Age: %d", "Alice", 25);
String upper = str.toUpperCase(); String lower = str.toLowerCase();
String trimmed = " hello ".trim();
StringBuilder sb = new StringBuilder(); sb.append("Hello"); sb.append(", "); sb.append("World!"); String result = sb.toString();
|
工具类(Collections/Arrays)
用途:提供集合和数组的常用工具方法
解决问题:简化集合和数组操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| int[] nums = {3, 1, 2};
Arrays.sort(nums); int index = Arrays.binarySearch(nums, 2); int[] copy = Arrays.copyOf(nums, nums.length); boolean equal = Arrays.equals(nums, new int[]{1, 2, 3}); String str = Arrays.toString(nums);
List<Integer> list = new ArrayList<>(Arrays.asList(3, 1, 2));
Collections.sort(list); Collections.reverse(list); Collections.shuffle(list); int max = Collections.max(list); int min = Collections.min(list); Collections.fill(list, 0); Collections.swap(list, 0, 1);
List<Integer> unmodifiable = Collections.unmodifiableList(list); Set<Integer> singleton = Collections.singleton(1);
|
这个蓝图集合涵盖了Java中最常用的数据结构和工具方法,可以作为快速参考指南。每个部分都包含了基本操作和典型用法,帮助你快速实现各种数据操作需求。