Skip to content

Commit 5acbb7a

Browse files
committed
Add PriorityQueue And Shuffle Demo
1 parent be78a2b commit 5acbb7a

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/MapDemo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static void main(String[] args) {
1818
map.values()) {
1919
System.out.println(value);
2020
}
21+
2122
//第二种
2223
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
2324
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
@@ -31,6 +32,9 @@ public static void main(String[] args) {
3132
) {
3233
System.out.println("key=" + entry.getKey() + " and value=" + entry.getValue());
3334
}
35+
//第四种:尤其推荐
36+
map.forEach((k, v) -> System.out.println("Key=" + k + ",Value=" + v));
37+
3438
Vector vector = new Vector();
3539
vector.add("11");
3640

src/PriorityQueueDemo.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.time.LocalDate;
2+
import java.util.PriorityQueue;
3+
4+
public class PriorityQueueDemo {
5+
public static void main(String[] args) {
6+
PriorityQueue<LocalDate> pq = new PriorityQueue<>();
7+
pq.add(LocalDate.of(1906, 12, 9));
8+
pq.add(LocalDate.of(1815, 12, 10));
9+
pq.add(LocalDate.of(1903, 12, 3));
10+
pq.add(LocalDate.of(1910, 6, 22));
11+
System.out.println("Iterating over elements...");
12+
for (LocalDate date : pq) {
13+
System.out.println(date);
14+
}
15+
System.out.println("Removing elements....");
16+
while (!pq.isEmpty()) {
17+
System.out.println(pq.remove());
18+
}
19+
}
20+
}

src/ShuffleDemo.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
4+
5+
//随机的混排列表中元素的顺序
6+
public class ShuffleDemo {
7+
public static void main(String[] args) {
8+
List<Integer> numbers = new ArrayList<>();
9+
for (int i = 0; i <= 49; i++) {
10+
numbers.add(i);
11+
}
12+
Collections.shuffle(numbers);
13+
List<Integer> winningCombination = numbers.subList(0, 6);
14+
Collections.sort(winningCombination);
15+
System.out.println(winningCombination);
16+
}
17+
}

0 commit comments

Comments
 (0)