在java中像在python中一样获得一系列字典

gc0ot86w  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(121)

在以前的帖子中:Populate manually a HasMap just like a dictionary in pyhton我想知道如何手动填充python中称为dictionary的内容。有人建议我读这个链接:How to directly initialize a HashMap (in a literal way)?
阅读完后,我意识到我没有正确地解释自己,所以我又问了一遍这个问题。在python中,我有一个dictionary.py文件,其中包含这些字典:

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"}

还有十几个类似的
在推荐给我的链接中(见上文),唯一能为我做的回答是:

// 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")
);

这让我想到,对于我在python中拥有的大约20个字典中的每一个,我都必须对该类型进行运行时初始化。
或者有什么东西可以让我拥有一dictionary.java内容类似于www.example.comdictionary.py

t2a7ltrp

t2a7ltrp1#

  • "...我想知道如何手动填充python中称为dictionary的内容。..."*

Java有 * Map * 集合。
最值得注意的是 * HashMapLinkedHashMap * 和 * TreeMap *。
第一个是无序的,第二个将保持顺序,第三个将按升序值排序,除非另有说明。

  • "...是否存在允许我拥有内容类似于www.example.com的www.example.com文件的东西"* dictionary.java file with content similar to dictionary.py "*

在Java中没有类似的语法。
您将找到的最接近的方法是您指定的方法。

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")));

由于内容已经在 * Python * 中,您可以编写一个脚本来为您生成 * Java * 代码。
这将默认为一个 * String * 值,所以你必须调整它。

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")));

相关问题