Skip to content

Commit 7a3efc5

Browse files
committed
JEP 431: Sequenced Collections
1 parent c448bb3 commit 7a3efc5

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This repository contains Java examples that are designed to track and document t
77

88
* [Java 21](java-21) (September, 2023)
99
* [JEP 430](https://openjdk.org/jeps/430): String Templates
10+
* [JEP 431](https://openjdk.org/jeps/431): Sequenced Collections
1011

1112
* [Java 16](java-16/) (March, 2021)
1213
* [JEP 395](https://openjdk.java.net/jeps/395): Records
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.ibrahimatay;
2+
3+
import java.util.*;
4+
5+
/*
6+
* JEP 431: Sequenced Collections
7+
* https://openjdk.org/jeps/431
8+
* */
9+
public class JEP431SequencedCollections {
10+
public static void main(String[] args) {
11+
/*
12+
interface SequencedSet<E> extends SequencedCollection<E>, Set<E> {
13+
SequencedSet<E> reversed();
14+
}
15+
* */
16+
17+
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>(List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
18+
19+
System.out.println(STR."Fist item: \{linkedHashSet.getFirst()} in LinkedHashSet"); // Fist item: 1=One
20+
System.out.println(STR."Last item: \{linkedHashSet.getLast()} in LinkedHashSet"); // Last item: 3=Three
21+
22+
linkedHashSet.addFirst(0);
23+
linkedHashSet.addLast(4);
24+
25+
System.out.println(linkedHashSet.reversed());
26+
27+
/*
28+
interface SequencedMap<K,V> extends Map<K,V> {
29+
// New Methods
30+
SequencedMap<K,V> reversed();
31+
32+
SequencedSet<K> sequencedKeySet();
33+
SequencedCollection<V> sequencedValues();
34+
SequencedSet<Entry<K,V>> sequencedEntrySet();
35+
36+
V putFirst(K, V);
37+
V putLast(K, V);
38+
39+
40+
// Promoted Methods from NavigableMap<K, V>
41+
42+
Entry<K, V> firstEntry();
43+
Entry<K, V> lastEntry();
44+
45+
Entry<K, V> pollFirstEntry();
46+
Entry<K, V> pollLastEntry();
47+
}
48+
* */
49+
50+
LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
51+
52+
map.put(1, "One");
53+
map.put(2, "Two");
54+
map.put(3, "Three");
55+
56+
System.out.println(STR."Fist item: \{map.firstEntry()}"); // Fist item: 1=One
57+
System.out.println(STR."Last item: \{map.lastEntry()}"); // Last item: 3=Three
58+
59+
System.out.println(map); // {1=One, 2=Two, 3=Three}
60+
61+
Map.Entry<Integer, String> firstMap = map.pollFirstEntry();
62+
System.out.println(firstMap); // 1=One
63+
Map.Entry<Integer, String> lastMap = map.pollLastEntry();
64+
System.out.println(lastMap); // 3=Three
65+
66+
System.out.println(map.reversed());
67+
68+
/*
69+
interface SequencedCollection<E> extends Collection<E> {
70+
// New Method
71+
72+
SequencedCollection<E> reversed();
73+
74+
// Promoted methods from Deque<E>
75+
76+
void addFirst(E);
77+
void addLast(E);
78+
79+
E getFirst();
80+
E getLast();
81+
82+
E removeFirst();
83+
E removeLast();
84+
}
85+
* */
86+
87+
ArrayList<Integer> arrayList = new ArrayList<>();
88+
89+
arrayList.add(1);
90+
arrayList.addFirst(0);
91+
arrayList.addLast(2);
92+
93+
System.out.println(STR."Fist item: \{arrayList.getFirst()} in ArrayList"); // Fist item: 0 in ArrayList
94+
System.out.println(STR."Last item: \{arrayList.getLast()} in ArrayList"); // Last item: 2 in ArrayList
95+
}
96+
}

0 commit comments

Comments
 (0)