Skip to content

Commit b3a73ae

Browse files
committed
附录:集合主题 翻译更新至 队列
1 parent 458ca5b commit b3a73ae

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

docs/book/Appendix-Collection-Topics.md

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,199 @@ two
20402040
<!-- Queues -->
20412041
## 队列
20422042

2043+
有许多 **Queue** 实现,其中大多数是为并发应用程序设计的。许多实现都是通过排序行为而不是性能来区分的。这是一个涉及大多数 **Queue** 实现的基本示例,包括基于并发的队列。队列将元素从一端放入并从另一端取出:
2044+
2045+
```java
2046+
// collectiontopics/QueueBehavior.java
2047+
// Compares basic behavior
2048+
import java.util.*;
2049+
import java.util.stream.*;
2050+
import java.util.concurrent.*;
2051+
2052+
public class QueueBehavior {
2053+
static Stream<String> strings() {
2054+
return Arrays.stream(
2055+
("one two three four five six seven " +
2056+
"eight nine ten").split(" "));
2057+
}
2058+
static void test(int id, Queue<String> queue) {
2059+
System.out.print(id + ": ");
2060+
strings().map(queue::offer).count();
2061+
while(queue.peek() != null)
2062+
System.out.print(queue.remove() + " ");
2063+
System.out.println();
2064+
}
2065+
public static void main(String[] args) {
2066+
int count = 10;
2067+
test(1, new LinkedList<>());
2068+
test(2, new PriorityQueue<>());
2069+
test(3, new ArrayBlockingQueue<>(count));
2070+
test(4, new ConcurrentLinkedQueue<>());
2071+
test(5, new LinkedBlockingQueue<>());
2072+
test(6, new PriorityBlockingQueue<>());
2073+
test(7, new ArrayDeque<>());
2074+
test(8, new ConcurrentLinkedDeque<>());
2075+
test(9, new LinkedBlockingDeque<>());
2076+
test(10, new LinkedTransferQueue<>());
2077+
test(11, new SynchronousQueue<>());
2078+
}
2079+
}
2080+
/* Output:
2081+
1: one two three four five six seven eight nine ten
2082+
2: eight five four nine one seven six ten three two
2083+
3: one two three four five six seven eight nine ten
2084+
4: one two three four five six seven eight nine ten
2085+
5: one two three four five six seven eight nine ten
2086+
6: eight five four nine one seven six ten three two
2087+
7: one two three four five six seven eight nine ten
2088+
8: one two three four five six seven eight nine ten
2089+
9: one two three four five six seven eight nine ten
2090+
10: one two three four five six seven eight nine ten
2091+
11:
2092+
*/
2093+
```
2094+
2095+
**Deque** 接口也继承自 **Queue** 。 除优先级队列外,**Queue** 按照元素的插入顺序生成元素。 在此示例中,**SynchronousQueue** 不会产生任何结果,因为它是一个阻塞队列,其中每个插入操作必须等待另一个线程执行相应的删除操作,反之亦然。
2096+
2097+
<!-- Priority Queues -->
2098+
### 优先级队列
2099+
2100+
考虑一个待办事项列表,其中每个对象包含一个 **String** 以及主要和次要优先级值。通过实现 **Comparable** 接口来控制此待办事项列表的顺序:
2101+
2102+
```java
2103+
// collectiontopics/ToDoList.java
2104+
// A more complex use of PriorityQueue
2105+
import java.util.*;
2106+
2107+
class ToDoItem implements Comparable<ToDoItem> {
2108+
private char primary;
2109+
private int secondary;
2110+
private String item;
2111+
ToDoItem(String td, char pri, int sec) {
2112+
primary = pri;
2113+
secondary = sec;
2114+
item = td;
2115+
}
2116+
@Override
2117+
public int compareTo(ToDoItem arg) {
2118+
if(primary > arg.primary)
2119+
return +1;
2120+
if(primary == arg.primary)
2121+
if(secondary > arg.secondary)
2122+
return +1;
2123+
else if(secondary == arg.secondary)
2124+
return 0;
2125+
return -1;
2126+
}
2127+
@Override
2128+
public String toString() {
2129+
return Character.toString(primary) +
2130+
secondary + ": " + item;
2131+
}
2132+
}
2133+
2134+
class ToDoList {
2135+
public static void main(String[] args) {
2136+
PriorityQueue<ToDoItem> toDo =
2137+
new PriorityQueue<>();
2138+
toDo.add(new ToDoItem("Empty trash", 'C', 4));
2139+
toDo.add(new ToDoItem("Feed dog", 'A', 2));
2140+
toDo.add(new ToDoItem("Feed bird", 'B', 7));
2141+
toDo.add(new ToDoItem("Mow lawn", 'C', 3));
2142+
toDo.add(new ToDoItem("Water lawn", 'A', 1));
2143+
toDo.add(new ToDoItem("Feed cat", 'B', 1));
2144+
while(!toDo.isEmpty())
2145+
System.out.println(toDo.remove());
2146+
}
2147+
}
2148+
/* Output:
2149+
A1: Water lawn
2150+
A2: Feed dog
2151+
B1: Feed cat
2152+
B7: Feed bird
2153+
C3: Mow lawn
2154+
C4: Empty trash
2155+
*/
2156+
```
2157+
2158+
这展示了通过优先级队列自动排序待办事项。
2159+
2160+
<!-- Deque -->
2161+
### 双端队列
2162+
2163+
**Deque** (双端队列)就像一个队列,但是可以从任一端添加和删除元素。 Java 6**Deque** 添加了一个显式接口。以下是对实现了 **Deque** 的类的最基本的 **Deque** 方法的测试:
2164+
2165+
```java
2166+
// collectiontopics/SimpleDeques.java
2167+
// Very basic test of Deques
2168+
import java.util.*;
2169+
import java.util.concurrent.*;
2170+
import java.util.function.*;
2171+
2172+
class CountString implements Supplier<String> {
2173+
private int n = 0;
2174+
CountString() {}
2175+
CountString(int start) { n = start; }
2176+
@Override
2177+
public String get() {
2178+
return Integer.toString(n++);
2179+
}
2180+
}
2181+
2182+
public class SimpleDeques {
2183+
static void test(Deque<String> deque) {
2184+
CountString s1 = new CountString(),
2185+
s2 = new CountString(20);
2186+
for(int n = 0; n < 8; n++) {
2187+
deque.offerFirst(s1.get());
2188+
deque.offerLast(s2.get()); // Same as offer()
2189+
}
2190+
System.out.println(deque);
2191+
String result = "";
2192+
while(deque.size() > 0) {
2193+
System.out.print(deque.peekFirst() + " ");
2194+
result += deque.pollFirst() + " ";
2195+
System.out.print(deque.peekLast() + " ");
2196+
result += deque.pollLast() + " ";
2197+
}
2198+
System.out.println("\n" + result);
2199+
}
2200+
public static void main(String[] args) {
2201+
int count = 10;
2202+
System.out.println("LinkedList");
2203+
test(new LinkedList<>());
2204+
System.out.println("ArrayDeque");
2205+
test(new ArrayDeque<>());
2206+
System.out.println("LinkedBlockingDeque");
2207+
test(new LinkedBlockingDeque<>(count));
2208+
System.out.println("ConcurrentLinkedDeque");
2209+
test(new ConcurrentLinkedDeque<>());
2210+
}
2211+
}
2212+
/* Output:
2213+
LinkedList
2214+
[7, 6, 5, 4, 3, 2, 1, 0, 20, 21, 22, 23, 24, 25, 26,
2215+
27]
2216+
7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20
2217+
7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20
2218+
ArrayDeque
2219+
[7, 6, 5, 4, 3, 2, 1, 0, 20, 21, 22, 23, 24, 25, 26,
2220+
27]
2221+
7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20
2222+
7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20
2223+
LinkedBlockingDeque
2224+
[4, 3, 2, 1, 0, 20, 21, 22, 23, 24]
2225+
4 24 3 23 2 22 1 21 0 20
2226+
4 24 3 23 2 22 1 21 0 20
2227+
ConcurrentLinkedDeque
2228+
[7, 6, 5, 4, 3, 2, 1, 0, 20, 21, 22, 23, 24, 25, 26,
2229+
27]
2230+
7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20
2231+
7 27 6 26 5 25 4 24 3 23 2 22 1 21 0 20
2232+
*/
2233+
```
2234+
2235+
我只使用了 **Deque** 方法的“offer”和“poll”版本,因为当 **LinkedBlockingDeque** 的大小有限时,这些方法不会抛出异常。请注意, **LinkedBlockingDeque** 仅填充到它的限制大小为止,然后忽略额外的添加。
20432236

20442237
<!-- Understanding Maps -->
20452238
## 理解Map

0 commit comments

Comments
 (0)