Can Java declare a HashMap constant?

I am writing a simple program to convert a number into a word representing that number (13 => "thirteen").

I understand that I could get some words with a constant array of String as follows:

private static final String[] tensNames = {"", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" };

... and access it with an index, but I wanted to try it using HashMap as follows:

final HashMap<Integer, String> tensNumberConversion = new HashMap<Integer, String>();
    tensNumberConversion.put(2, "twenty");
    tensNumberConversion.put(3, "thirty");
    tensNumberConversion.put(4, "forty");
    tensNumberConversion.put(5, "fifty");
    tensNumberConversion.put(6, "sixty");
    tensNumberConversion.put(7, "seventy");
    tensNumberConversion.put(8, "eighty");
    tensNumberConversion.put(9, "ninety");

I was instructed by the teacher to make these constants. Can a HashMap be a constant? As a beginner, it was not entirely clear how the terms “permanent” and “static” and “final” are related, and what exactly makes a constant. (static only? final one? static final together?).

Hashmap, IntelliJ ( 'private' ... "static" ).

. HashMap , ? !

+7
5

:

  • , . final; , , .
  • , . , Map, put/remove/... .

, , "" , :

, .

Collections.unmodifiableMap(), , .

, : , , . , , .

: " " (, ) - . , , Object... ; , - , - , . , . , , ...

: Java9 , - "" , . , .

+11

, . HashMap :

class <class name> {
    private static final HashMap<Integer, String> tensNumberConversion = new HashMap<>();

    static {
        tensNumberConversion.put(2, "twenty");
        tensNumberConversion.put(3, "thirty");
        tensNumberConversion.put(4, "forty");
        tensNumberConversion.put(5, "fifty");
        tensNumberConversion.put(6, "sixty");
        tensNumberConversion.put(7, "seventy");
        tensNumberConversion.put(8, "eighty");
        tensNumberConversion.put(9, "ninety");
    }
}

. tensNumberConversion - , :

tensNumberConversion = new HashMap<>(); // won't compile
tensNumberConversion.put(9, "one"); // succeeds

, , HashMap :

class <class name> {
    private static final Map<Integer, String> tensNumberConversion = initMap();

    private static Map<Integer, String> initMap() {
        Map<Integer, String> map = new HashMap<>();
        map.put(2, "twenty");
        map.put(3, "thirty");
        map.put(4, "forty");
        map.put(5, "fifty");
        map.put(6, "sixty");
        map.put(7, "seventy");
        map.put(8, "eighty");
        map.put(9, "ninety");
        return Collections.unmodifiableMap(map);
    }
}
+13

BTW, Java 9 Map, ,

Map<String,String> m = Map.of("k1", "v1",
                              "K2", "v2");

10 :

import static java.util.Map.entry;
Map<String, String> m = Map.ofEntries(
    entry("k1", "v1"), entry("k2", "v2"), …);
+6

HashMap HashMap, HashMap.put(), ...

class MyMap extends java.util.HashMap{
@Override
public V put (K key, V value){
System.out.println("Operation Not Supported");
return null;
}
}

Please note that this is not the only method, but one of many possible ways.

0
source

I somehow missed the simplest form for directly initializing a HashMap here

private static final Map<Integer, String> tensNumberConversion_internal = new HashMap<Integer, String>()
{
    {
        put(2, "twenty");
        put(3, "thirty");
        put(4, "forty");
        put(5, "fifty");
        put(6, "sixty");
        put(7, "seventy");
        put(8, "eighty");
        put(9, "ninety");
    };
};

as for making it immutable, you already have Collections.unmodifiableMapfrom the accepted answer, so

public static final Map<Integer, String> tensNumberConversion = Collections.unmodifiableMap(tensNumberConversion_internal);

they must be created in this order;)

0
source

Source: https://habr.com/ru/post/1676883/


All Articles