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

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

Static factory methods for creating instances of collections and maps conveniently with small number of elements. Java 9 Factory Method for Collections.

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

java9 factory collections

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python Collections Module: Counter, ChainMap, Deque & Tuple | upGrad blog

By the end of this tutorial, you’ll have the knowledge of the following: What is the collections module? Python Collections Module: Counter, ChainMap, Deque & Tuple.

Python Collections Module: Counter, ChainMap, Deque & Tuple

Python Collections module offers a set of container data types that extend the features of stock containers like Lists, Tuples, Sets, and Dictionaries. With these special containers, will be of great help for you. Read now

What is Collective (CO2) | What is Collective token | What is CO2 token

The Collective brings you Professional Community Management services. We are a highly responsive team of independent contributors, who deliver a broad range of content and community support during an ITO to build awareness and provide answers to prospective supporters. We provide our clients with guidance on strategy, partnerships, influencers, and other factors vital to realize a successful community driven blockchain company. What is Collective (CO2) | What is Collective token | What is CO2 token

Outsource Settlement Services & Solutions | DK Business Patron

Outsource Settlement Services | DK Business Patron offers settlement services empower business/ organization sucess by reducing risk and increase liquidity. Call us at +91 9853535812

How To Find Factorial in Python [With Coding Examples]

How To Find Factorial in Python [With Coding Examples]. Everyone one of us must be familiar with the word factorial as we all got introduced to that in our primary school in Mathematics subject. But you already know how to implement it in Python. Read this article.