Skip to content

Instantly share code, notes, and snippets.

@albihasani94
Last active October 19, 2019 20:28
Show Gist options
  • Save albihasani94/2448fed86f1df71fbfdddc2d0b0b4138 to your computer and use it in GitHub Desktop.
Save albihasani94/2448fed86f1df71fbfdddc2d0b0b4138 to your computer and use it in GitHub Desktop.
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