如何在地图中打开对象列表

Joh*_*ith -1 java

我试图找出如何将对象列表转换为java中的映射或处理列表

示例发票有两个字段,注释和金额:

List<Invoice> invoices = Arrays.asList(
new Invoice( "note1", "amount1" ), 
new Invoice( "note2", "amount2" ) );
Run Code Online (Sandbox Code Playgroud)

现在,我可以将此列表放入如下的地图中:

Map<Long, String>
Run Code Online (Sandbox Code Playgroud)

Ale*_*exR 5

类似于以下内容:

Map<Long, String> map = new HashMap<Long, String>();
for(Invoice invoice : invoces) {
    map.put(invoice.getId(), invoce.getName());
}
Run Code Online (Sandbox Code Playgroud)

既然你没有提到你想要存储的具体Long密钥和String值,我认为你的类Invoice有很长的ID和String名称.哟可以map.put(...)根据你的实际需要改变线路 .