有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在安卓中将kotlin集转换为java

kotlin设置代码如下:

val CITIES_WITH_LOGIN_ENABLED =
 setOf<CustomCity>(CustomCity.MUNICH, CustomCity.BERLIN, CustomCity.SAIGON, 
 CustomCity.TESTMUNICH, CustomCity.TBILISI)

我想把那个代码转换成java,它的等价java代码是什么


共 (4) 个答案

  1. # 1 楼答案

    如果查看setOf()的定义,它相当于Java Set,它的不可变集只包含指定的对象,并且该集是可序列化的

    您可以选中Menu > Tools > Kotlin > Show Kotlin Bytecode > Decompile

    您可以使用HashSet将其序列化(注意类型)

    Set<CustomCity> set = new HashSet<>(Arrays.asList(CustomCity.MUNICH,CustomCity.TBILISI));
    
  2. # 2 楼答案

    您可以直接使用构造函数

    Set<CustomCity> CITIES_WITH_LOGIN_ENABLES = new HashSet<>(Arrays.asList(CustomCity.MUNICH, CustomCity.BERLIN, CustomCity.SAIGON, CustomCity.TESTMUNICH, CustomCity.TBILISI));
    
  3. # 3 楼答案

    Set<CustomCity> CITIES_WITH_LOGIN_ENABLED = new HashSet<CustomCity>
    
    CITIES_WITH_LOGIN_ENABLED.add(CustomCity.MUNICH);
    CITIES_WITH_LOGIN_ENABLED.add(CustomCity.BERLIN);
    CITIES_WITH_LOGIN_ENABLED.add(CustomCity.TESTMUNICH);
    CITIES_WITH_LOGIN_ENABLED.add( CustomCity.TBILISI);
     //TO print your set:
    SYstem.out.println(CITIES_WITH_LOGIN_ENABLED);
    
  4. # 4 楼答案

    Set<CustomCity> CITIES_WITH_LOGIN_ENABLED = new HashSet<>(
        Arrays.asList(
            CustomCity.MUNICH, 
            CustomCity.BERLIN,
            CustomCity.SAIGON,
            CustomCity.TESTMUNICH,
            CustomCity.TBILISI
        )
    );