Logo

dev-resources.site

for different kinds of informations.

Java 9 Factory Method for Collections: List, Set, Map

Published at
4/22/2021
Categories
java9
factory
collections
Author
loizenai
Categories
3 categories in total
java9
open
factory
open
collections
open
Author
8 person written this
loizenai
open
Java 9 Factory Method for Collections: List, Set, Map

https://grokonez.com/java/java-9/java-9-factory-method-for-immutable-collections-list-set-map

Java 9 Factory Method for Collections: List, Set, Map

Java 9 provides new static factory methods for creating instances of collections and maps conveniently with small number of elements. In this tutorial, we're gonna look at how to create List, Set, Map with Java 9 Factory Method for Collections.

I. List

To create a List, we use those static methods:


// for empty list
static  List of()
// for list containing one element
static  List of(E e1)
// for list containing two element
static  List of(E e1, E e2)
// ...
// for list containing an arbitrary number of elements
static  List of(E... elements)

For example:


List immutableList = List.of();
immutableList = List.of("one", "two", "three");

If we try to create list with null element, a java.lang.NullPointerException will be thrown:


List immutableList = List.of("one", "two", "three", null);

Exception in thread "main" java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:221)
    at java.base/java.util.ImmutableCollections$ListN.(ImmutableCollections.java:233)
    at java.base/java.util.List.of(List.java:859)

Because the list created with static factory method is immutable, so if we try to add an element to list, it also throws an java.lang.UnsupportedOperationException

More at:

https://grokonez.com/java/java-9/java-9-factory-method-for-immutable-collections-list-set-map

Java 9 Factory Method for Collections: List, Set, Map

factory Article's
25 articles in total
Favicon
Clojure is Awesome!!!
Favicon
Understanding the Factory and Factory Method Design Patterns
Favicon
Factory Design Pattern
Favicon
Things You Want to Ensure When Factory Resetting Your HP Laptop Without a Password
Favicon
The Factory Pattern in C#: Creating Objects the Smart Way
Favicon
Enhance Your Factory Car Radio's Bass with Affordable Upgrades
Favicon
Page Object Model and Page Factory in Selenium
Favicon
Factory functions with private variables in JavaScript
Favicon
Evoluindo nosso Projeto Rails: Integrando o Padrão Factory para Maior Flexibilidade e Organização
Favicon
Javascript Factory Design Pattern
Favicon
Dart Abstract and Factory Keywords
Favicon
Azure Data Factory Overview For Beginners
Favicon
Improve your factories in Symfony
Favicon
Reduzindo a quantidade de Branchs na criação de Objetos com uma estrutura plugável
Favicon
Java 9 Factory Method for Collections: List, Set, Map
Favicon
How to implement simple Factory Pattern in Node.js
Favicon
Factory Design Pattern (simple example implementation in PHP)
Favicon
Spring's FactoryBean Interface
Favicon
Custom Validators for Angular Reactive Forms
Favicon
Design Patterns: Factory
Favicon
Creating objects dynamically with factory pattern in javascript
Favicon
Is this the real life, is this just fantasy?
Favicon
Design Patterns: Factory Pattern, Part 2
Favicon
Patterns in Kotlin: Abstract Factory
Favicon
SUT Factory

Featured ones: