本文整理了Java
语言的常用数据结构,方便刷题/面试时使用。
容器
对应数据结构
List
变长数组
Queue
队列
PriorityQueue
堆
Stack
栈
Deque
双端队列
Set
集合
Map
键值表
Pair
元素对
List 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 public static void testArrayList () { System.out.println("------ArrayList Start------" ); List<Integer> a = new ArrayList <>(); List<Integer> b = new ArrayList <>(3 ); ArrayList<Integer> c = new ArrayList <>(Arrays.asList(1 , 2 , 3 )); for (int i = 0 ; i < c.size(); i++) System.out.print(c.get(i) + " " ); System.out.println(); for (int x : c) System.out.print(x + " " ); System.out.println(); System.out.println(c.get(0 )); System.out.println(c.get(c.size() - 1 )); c.add(4 ); c.remove(c.size() - 1 ); c.remove(c.size() - 1 ); System.out.println(c.size()); c.clear(); System.out.println(c.size()); System.out.println("------ArrayList End------" ); }
Queue 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public static void testQueue () { System.out.println("------Queue Start------" ); Queue<Integer> q = new LinkedList <>(); q.offer(1 ); q.offer(2 ); q.offer(3 ); q.offer(4 ); q.poll(); System.out.println(q.peek()); System.out.println(q.size()); for (int i : q) { System.out.println(i); } q.clear(); System.out.println(q.isEmpty()); System.out.println("------Queue End------" ); }
PriorityQueue 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 public static void testPriorityQueue () { System.out.println("------PriorityQueue Start------" ); Queue<Integer> q1 = new PriorityQueue <>(); Queue<Integer> q2 = new PriorityQueue <>(((o1, o2) -> o2 - o1)); class Rec implements Comparable <Rec> { int x, y; Rec(int x, int y) { this .x = x; this .y = y; } @Override public int compareTo (Rec o) { return this .x - o.x; } @Override public String toString () { return "Rec{" + "x=" + x + ", y=" + y + '}' ; } } Queue<Rec> q3 = new PriorityQueue <>(); q3.offer(new Rec (1 , 2 )); q3.offer(new Rec (6 , 7 )); q3.offer(new Rec (5 , 4 )); q3.offer(new Rec (3 , 9 )); q3.poll(); System.out.println("堆顶: " + q3.peek()); for (Rec i : q3) { System.out.println(i); } System.out.println("------PriorityQueue End------" ); }
Stack 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public static void testStack () { System.out.println("------Stack Start------" ); Deque<Integer> stk = new ArrayDeque <>(); stk.push(1 ); stk.push(2 ); stk.push(3 ); stk.push(4 ); stk.pop(); System.out.println(stk.peek()); System.out.println(stk.size()); for (int i : stk) { System.out.println(i); } stk.clear(); System.out.println(stk.isEmpty()); System.out.println("------Stack End------" ); }
Deque 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public static void testDeque () { System.out.println("------Deque Start------" ); Deque<Integer> d = new ArrayDeque <>(); d.addLast(1 ); d.addLast(2 ); d.addFirst(3 ); d.addFirst(4 ); d.removeFirst(); System.out.println(d.getFirst()); System.out.println(d.getLast()); System.out.println(d.size()); d.clear(); System.out.println(d.isEmpty()); System.out.println("------Deque End------" ); }
Set 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 63 64 65 public static void testSet () { System.out.println("------Set Start------" ); LinkedHashSet<Integer> ls = new LinkedHashSet <>(); ls.add(4 ); ls.add(5 ); ls.add(1 ); for (int i : ls) { System.out.print(i + " " ); } System.out.println(); TreeSet<Integer> ts = new TreeSet <>(); ts.add(4 ); ts.add(5 ); ts.add(1 ); for (int i : ts) { System.out.print(i + " " ); } System.out.println(); System.out.println(ts.first()); System.out.println(ts.last()); System.out.println(ts.contains(3 )); System.out.println(ts.ceiling(4 )); System.out.println(ts.higher(4 )); System.out.println(ts.floor(4 )); System.out.println(ts.lower(4 )); HashSet<Integer> hs = new HashSet <>(); hs.add(12 ); hs.add(4 ); hs.add(5 ); hs.add(69 ); hs.add(9 ); hs.remove(5 ); for (int i : hs) { System.out.print(i + " " ); } System.out.println(); System.out.println("------Set End------" ); }
Map 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 public static void testMap () { System.out.println("------Map Start------" ); TreeMap<String, Integer> tm = new TreeMap <>(); tm.put("tm4" , 4 ); tm.put("tm3" , 3 ); tm.put("tm2" , 2 ); tm.put("tm1" , 1 ); tm.remove("tm2" ); for (String key : tm.keySet()) { System.out.println(tm.get(key)); } System.out.println(tm.firstEntry()); System.out.println(tm.lastEntry()); System.out.println(tm.containsKey("tm1" )); System.out.println(tm.containsValue(1 )); System.out.println(); HashMap<String, Integer> hm = new HashMap <>(); hm.put("hm4" , 4 ); hm.put("hm3" , 3 ); hm.put("hm2" , 2 ); hm.put("hm1" , 1 ); hm.remove("hm2" ); for (String key : hm.keySet()) { System.out.println(hm.get(key)); } System.out.println(hm.containsKey("hm1" )); System.out.println("------Map End------" ); }
Pair 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 public static void testPair () { System.out.println("------Pair Start------" ); class Pair implements Comparable <Pair> { int x, y; Pair(int x, int y) { this .x = x; this .y = y; } @Override public int compareTo (Pair o) { if (x != o.x) return x - o.x; else return y - o.y; } @Override public String toString () { return "x: " + x + " y: " + y; } } Pair a = new Pair (1 , 2 ); Pair b = new Pair (1 , 3 ); Pair c = new Pair (2 , 4 ); List<Pair> pairList = new ArrayList <>(); pairList.add(c); pairList.add(b); pairList.add(a); for (Pair p : pairList) { System.out.println(p); } Collections.sort(pairList); System.out.println(); for (Pair p : pairList) { System.out.println(p); } System.out.println("------Pair End------" ); }