[Java] Create a collection with only one element

The Collections class provides methods to create collections that have only one element.

Returns Set, List, and Map with one element for each.

import java.util.Collections;
import java.util.Set;
import java.util.List;
import java.util.Map;

import java.util.Collections.ImmutableSet;

class CollectionsSingleton {
    public static void main(String args[]) {
        Set<String> s = Collections.singleton("a");
        // s.add("b"); // UnsupportedOperationException
        System.out.println(s);

        List<String> l = Collections.singletonList("a");
        System.out.println(l);

        Map<Integer, String> m = Collections.singletonMap(0, "a");
        System.out.println(m);
    }
}

The returned collection is immutable. An UnsupportedOperationException is thrown when trying to add an element or the like.

reference

Recommended Posts

[Java] Create a collection with only one element
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
Let's create a timed process with Java Timer! !!
[Java] Create a filter
[Beginner] Create a competitive game with basic Java knowledge
[Note] Create a java environment from scratch with docker
Getting Started with Java Collection
Create a java method [Memo] [java11]
[Java] Create a temporary file
Create a playground with Xcode 12
What is a Java collection?
I tried to create a java8 development environment with Chocolatey
Create a SlackBot with AWS lambda & API Gateway in Java
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Build a Java project with Gradle
[Java] Element existence check with Stream
Create a Java project using Eclipse
[Java] How to create a folder
Create exceptions with a fluid interface
Create a Maven project with a command
Create a simple web server with the Java standard library com.sun.net.httpserver
[Java] Adding an element to the Collection causes a compile error
I can't create a Java class with a specific name in IntelliJ
Sorting a list with an int array as an element (Java) (Comparator)
[Java] Create and apply a slide master
[Java] Create an executable module with Gradle
Create a jar file with the command
Create a simple web application with Dropwizard
Create a simple on-demand batch with Spring Batch
☾ Java / Collection
Create a GUI JSON Viewer with Ruby/GTK3
[Rails withdrawal] Create a simple withdrawal function with rails
[Java] Create a jar file with both compressed and uncompressed with the jar command
Create a MySQL environment with Docker from 0-> 1
Let's create a Java development environment (updating)
I want to create a dark web SNS with Jakarta EE 8 with Java 11
Create a simple bar chart with MPAndroidChart
Java9 collection
Create a TODO app in Java 7 Create Header
Create a temporary class with new Object () {}
How to create a validator that allows only input to any one field
[Rails 5] Create a new app with Rails [Beginner]
Split a string with ". (Dot)" in Java
Deserialize XML into a collection with spring-boot
Let's go with Watson Assistant (formerly Conversation) ⑤ Create a chatbot with Watson + Java + Slack
Create a java web application development environment with docker for mac part2
Create Java applications with IBM Cloud Functions
[Java] How to convert one element of a String type array to an Int type
Let's make a calculator application with Java ~ Create a display area in the window
[Java] [POI] Create a table in Word and start a new line in one cell
[Java] Implemented a strict line feed stream reader that readsLine only with CrLf.
Create a website with Spring Boot + Gradle (jdk1.8.x)
[Memo] Create a CentOS 8 environment easily with Docker
Create a simple search app with Spring Boot
[Rails] rails new to create a database with PostgreSQL
Create a Lambda Container Image based on Java 15
[Java] Create something like a product search API
Try to create a bulletin board in Java
Let's create a super-simple web framework in Java
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]