이상을 꿈꾸는 몽상가.. 프로그래밍을 좋아함..


How to initialize Java variables - Array, List, Set, Map

This post describes how to initialize a variable at the same time as declaring that.





Array

public class Sample {

    private final String[] values0 = new String[]{ "1", "2", "3" };

    // only available when declaring an array
    private final String[] values1 = { "1", "2", "3" };
}

List

Mutable List

public class Sample {
    private final List<String> values0 = new ArrayList<String>() {
        {
            values.add("1");
            values.add("2");
            values.add("3");
        }
    };

    private final List<String> values1 = new ArrayList<>(Arrays.asList(new String[]{ "1", "2", "3" }));

    // available in JDK 5 or later
    private final List<String> values2 = new ArrayList<>(Arrays.asList("1", "2", "3"));
}

Immutable List

Follows are the how to make immutable List.
If something attempts to change an immutable List, UnsupportedOperationException will be thrown.

public class Sample {

    private final List<String> values0 = Arrays.asList(new String[]{ "1", "2", "3" });

    private final List<String> values1 = Collections.unmodifiableList(new ArrayList<>(values0));

    // available in JDK 5 or later
    private final List<String> values2 = Arrays.asList("1", "2", "3");

    // available in JDK 9 or later
    private final List<String> values3 = List.of("1", "2", "3");
}

Set

Mutable Set

public class Sample {
    private final Set<String> values = new HashSet<>(Arrays.asList("1", "2", "3"));
}

Immutable Set

Follows are the how to make immutable Set.
If something attempts to change an immutable Set, UnsupportedOperationException will be thrown.

public class Sample {

    private final Set<String> values0 = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("1", "2", "3")));

    // available in JDK 9 or later
    private final Set<String> values1 = Set.of("1", "2", "3");
}

Map

Mutable Map

public class Sample {
    private final Map<String, String> map = new HashMap<>() {
        {
            map.put("key01", "val01");
            map.put("key02", "val02");
            map.put("key03", "val03");
        }
    };
}

Immutable Map

Follows are the how to make immutable Map.
If something attempts to change an immutable Map, UnsupportedOperationException will be thrown.

Collections is a utility class for the Collection interface.
Although the Map interface does not extend the Collection interface, some feature of Collections class support Collection iterface.

public class Sample {
    private final Map<String, String> map0 = Collections.unmodifiableMap(new HashMap<>() {
        {
            map0.put("key01", "val01");
            map0.put("key02", "val02");
            map0.put("key03", "val03");
        }
    });

    // available in JDK 9 or later
    // only 10 keys can be available
    Map<String, String> map1 = Map.of("key01","value01", "key02", "value02");
}

Empty Immutable Collection

Follows are the how to make empty immutable List, Set, and Map.
These empty immutable things are very efficient from resources standpoint because only one exists in the process.

If something attempts to change these, UnsupportedOperationException will be thrown.

public class Sample {

    // available in JDK 5 or later
    private final List<String> emptyList0 = Collections.emptyList();
    private final Set<String> emptySet0 = Collections.emptySet();
    private final Map<String, String> emptyMap0 = Collections.emptyMap();

    // available in JDK 9 or later
    private final List<String> emptyList1 = List.of();
    private final Set<String> emptySet1 = Set.of();
    private final Map<String, String> emptyMap1 = Map.of();
}


Break Time ~ ❤

I hope this article has been helpful to you.
I thought you had a hard time reading a long post, so I prepared a short break :)

Please respond to the ads, even if you do not purchase.
It help me know that you have been interested in this post and have read this post to the end.
It make me cheer up. Thank you!




Associated Posts

I have gathered articles with the same tag so that you can explore related topics. Please tap the title.

i