0

In a previous post: Populate manually a HasMap just like a dictionary in pyhton I was wondering how to manually populate what in python is called dictionary. I was suggested to read this link: How to directly initialize a HashMap (in a literal way)?

After reading it, I realized that I didn't explain myself correctly, so I ask the question again. In python I have a dictionary.py file with these dictionaries:

 convert_en_it = {"Mon": "Mon", "Tue": "Tue", "Wed": "Wed", "Thu": "Thurs", "Fri": "Fri", "Sat": "Sat" , "Sun": "Sun"}

 convert_number_month = {"1": "January", "2": "February", "3": "March", "4": "April", "5": "May", "6": "June" ,
                         "7": "July", "8": "August", "9": "September", "10": "October", "11": "November",
                         "12": "December"}

 conv_simb = {"P": "P", "M": "M", "S": "S", "CM": "E", "CP": "D", "CS": "T" , "VP": "F", "VM": "G", "VS": "R",
              "NM": "U", "NP": "W", "PM": "H", "PP": "L", "EM": "A", "EP": "B", "ES ": "IF", "AM": "I",
              "AP": "J", "AS": "K", "BM": "N", "BP": "O", "BS": "Q", "X": "X", "- -": "V", "Y": "Y", "Z": "Z",
              "ME": 'a', "BT": "c"}

 conv_to_location = {"M": "M", "P": "P", "S": "S", "D": "CP", "E": "CM", "T": "CS" , "F": "VP", "G": "VM", "R": "VS",
                     "Z": "Z", "A": "EM", "B": "EP", "C": "ES", "I": "AM", "J": "AP", "K ": "AS",
                     "N": "BM", "O": "BP", "Q": "BS", "U": "NM", "W": "NP","H": "PM", "L ": "PP", "Y": "Y", "X": "x",
                     "V": "--", "": ' ', "a": "ME", "c": "BT"}

 sel_hour = {"M": 3.5, "P": 5, "S": 3, "D": 6, "E": 6, "T": 3, "F": 5, "G": 3.5 , "R": 3.5,
             "Z": 9, "A": 3.5, "B": 4.5, "C": 3.5, "I": 5, "J": 5, "K": 4, "N": 5, "O ": 5, "Q": 4,
             "H": 5, "L": 5, "U": 4, "W": 4, "Y": 9, "c": 9, "u": 10, "a": 8.5}

 which_table = {"Z": "biblio", "Y": "show", "M": "permanent", "P": "permanent",
                "S": "permanent", "D": "show", "E": "show", "T": "show", "F": "extra", "G": "extra",
                "R": "extra", "H": "pieces", "L": "pieces", "A": "educational", "B": "educational",
                "C": "educational", "I": "artcity", "J": "artcity", "K": "artcity", "N": "artcity",
                "O": "artcity", "Q": "artcity", "U": "esprit", "W": "esprit"}

plus another dozen similar.

In the link recommended to me (see above) the only answer that could do for me was the following:

// this works for any number of elements:
import static java.util.Map.entry;
Map<String, String> test2 = Map.ofEntries(
     entry("a", "b"),
     entry("c", "d")
);

which leads me to think that for each of the about 20 dictionaries I have in python I have to do a run-time initialization of that type.

Or is there something that allows me to have a dictionary.java file with content similar to dictionary.py

Ryan Day
  • 113
  • 1
  • 8
  • 1
    No, there's no special syntax for constructing Maps in Java. – Unmitigated Jun 24 '23 at 20:49
  • You can get something similar using Java properties files: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Properties.html#load(java.io.Reader) – markspace Jun 24 '23 at 21:03

1 Answers1

2

"... I was wondering how to manually populate what in python is called dictionary. ..."

Java has the Map collection.
Most notably, the HashMap, LinkedHashMap, and TreeMap.

The first is unordered, the second will preserve order, and the third will sort by ascending value, unless otherwise specified.

"... is there something that allows me to have a dictionary.java file with content similar to dictionary.py"

There is no similar syntax, within Java.

The closest approach you'll find is the one you specified.

Map<String, String> convert_en_it
    = new LinkedHashMap<>(
        Map.ofEntries(
            entry("Mon", "Mon"),
            entry("Tue", "Tue"),
            entry("Wed", "Wed"),
            entry("Thu", "Thurs"),
            entry("Fri", "Fri"),
            entry("Sat", "Sat"),
            entry("Sun", "Sun")));

Since the content is already within Python, you could write a script to generate the Java code for you.
This will default to a String value, so you'll have to adjust it.

def to_java(dictionary, name):
    print('Map<String, String> ' + name)
    print('    = new LinkedHashMap<>(')
    print('        Map.ofEntries(')
    length = len(dictionary)
    index = 0
    for key, value in dictionary.items():
        print('            ', end='')
        print('entry("{0}", "{1}")'.format(key, value), end='')
        if index != length - 1:
            print(',')
        else:
            print('));')
        index += 1
    print()


to_java(convert_number_month, 'convert_number_month')
Map<String, String> convert_number_month
    = new LinkedHashMap<>(
        Map.ofEntries(
            entry("1", "January"),
            entry("2", "February"),
            entry("3", "March"),
            entry("4", "April"),
            entry("5", "May"),
            entry("6", "June"),
            entry("7", "July"),
            entry("8", "August"),
            entry("9", "September"),
            entry("10", "October"),
            entry("11", "November"),
            entry("12", "December")));
Reilas
  • 3,297
  • 2
  • 4
  • 17
  • Understand. Will write a lot of entry ("x","y"). Its not what I dreamed but is easier to amalyze in case I need to make some checks or changes – Ryan Day Jun 25 '23 at 07:25