interfaceSequencedCollection<E> extendsCollection<E> { // new method SequencedCollection<E> reversed(); // methods promoted from Deque voidaddFirst(E); voidaddLast(E); E getFirst(); E getLast(); E removeFirst(); E removeLast(); }
下面是使用这些方法的一些代码片段:
1 2 3 4 5 6 7 8
List<String> sc = Stream.of("Alpha", "Bravo", "Charlie", "Delta").collect(Collectors.toCollection(ArrayList::new)); System.out.println("Initial list: " + sc); System.out.println("Reversed list: " + sc.reversed()); System.out.println("First item: " + sc.getFirst()); System.out.println("Last item: " + sc.getLast()); sc.addFirst("Before Alpha"); sc.addLast("After Delta"); System.out.println("Added new first and last item: " + sc);
输出是:
Initial list: [Alpha, Bravo, Charlie, Delta] Reversed list: [Delta, Charlie, Bravo, Alpha] First item: Alpha Last item: Delta Added new first and last item: [Before Alpha, Alpha, Bravo, Charlie, Delta, After Delta]
Initial list: [Alpha, Bravo, Charlie, Delta] Reversed list: [Delta, Charlie, Bravo, Alpha] First item: Alpha Last item: Delta addFirst is not supported addLast is not supported
== Initial Map == 1 Alpha 2 Bravo 3 Charlie 4 Delta == Reversed Map == 4 Delta 3 Charlie 2 Bravo 1 Alpha First item: 1=Alpha Last item: 4=Delta == Added new first and last item == 5 Before Alpha 1 Alpha 2 Bravo 4 Delta 3 After Delta
privatestaticvoidpatternMatchingSwitch(Object obj) { switch(obj) { case Integer i -> System.out.println("Object is an integer:" + i); case String s -> System.out.println("Object is a string:" + s); case Point p -> System.out.println("Object is a point: " + p); default -> System.out.println("Object is not recognized"); } }
privatestaticvoidpatternMatchingSwitch(Object obj) { switch(obj) { case Integer i -> System.out.println("Object is an integer:" + i); case String s -> System.out.println("Object is a string:" + s); case Point p -> System.out.println("Object is a point: " + p); casenull -> System.out.println("Object is null"); default -> System.out.println("Object is not recognized"); } }
case细化
现在可以在case中加入进一步的判断语句来细化想要匹配的目标,使用when关键字,称为guard。
1 2 3 4 5 6 7 8 9 10 11 12 13
privatestaticvoidpatternMatchingSwitch(Object obj) { switch (obj) { case String s -> System.out.println("Object is a string:" + s); case Color c when(c == Color.BLACK) -> { System.out.println("Object is an apple"); } case Color c when(c == Color.WHITE) -> { System.out.println("Object is an avocado"); } casenull -> System.out.println("Object is null"); default -> System.out.println("Object is not recognized"); } }
枚举常量
枚举类型可以在switch中使用,但只限于某个枚举的常量,现在可以支持多个枚举类型的常量。
1 2 3 4 5 6 7 8 9 10 11 12
privatestaticvoidpatternMatchingSwitch(Object obj) { switch (obj) { case String s -> System.out.println("Object is a string:" + s); case FruitType.APPLE -> System.out.println("Object is an apple"); case FruitType.AVOCADO -> System.out.println("Object is an avocado"); case FruitType.PEAR -> System.out.println("Object is a pear"); case FruitType.ORANGE -> System.out.println("Object is an orange"); case CarType.CABRIO -> System.out.println("Object is a cabrio"); casenull -> System.out.println("Object is null"); default -> System.out.println("Object is not recognized"); } }