java 创建匿名对象及声明map list时初始化

java 创建匿名对象 类似于c# 中的 new { a:"aaa",b:"bbb"};

1 创建匿名对象
Object myobj = new Object() {
public final boolean success = true;
}

2 字面量方式初始化map Map<String, String> myMap = new HashMap<>() {{
        put("a", "b");
put("c", "d");
}};
}
3 初始化数组
var refEntityIDs = new ArrayList<String>(Arrays.asList(
     BalanceConst.BEIDVoucherOptFail,
BalanceConst.BEIDAccountingDocument,
BalanceConst.BEIDAccDocOptPreTbl
));
 

参考

https://*.com/questions/8488833/what-is-the-java-equivalent-of-creating-an-anonymous-object-in-c

https://*.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-way

上一篇:Java入门教程十二(集合与泛型)


下一篇:JSon_零基础_003_将Map集合对象转换为JSon格式的对象字符串,返回给界面