Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JShell transcript with static factory methods for lists and sets in Java 9
List<String> animals = new ArrayList<>();
animals.add("Dog");
animals.add("Lemur");
animals.add("Duck");
System.out.println(animals);
Set<String> animals = new HashSet<>(Arrays.asList("Dog", "Lemur", "Duck"));
System.out.println(animals);
Set<String> animals = new HashSet<>(){{add("Dog"); add("Lemur"); add("Duck");}};
System.out.println(animals);
List<String> animals = List.of("Dog", "Lemur", "Duck");
System.out.println(animals);
animals.add("Elephant");
System.out.println(animals.getClass());
List<Integer> singleElementList = List.of(1);
singleElementList.getClass()
List<Integer> twoElementsList = List.of(1, 2);
twoElementsList.getClass()
List<Integer> mustBeABiggerList = List.of(1, 2, 3);
mustBeABiggerList.getClass()
Map<String, Integer> wordToNumber = Map.of("one", 1, "two", 2, "three", 3)
wordToNumber.getClass()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.