简体   繁体   English

Java:将HashMap添加到ArrayList for Talend

[英]Java: Add HashMap to ArrayList for Talend

I am using Talend tJavaFlex component where there is start code(runs once in the beginning), main code(runs for every row), end code(runs once at end). 我正在使用Talend tJavaFlex组件,其中有开始代码(在开始处运行一次),主代码(每行运行一次),结束代码(末尾运行一次)。

**In the start code(create an empty list):**
java.util.List sharedList=new java.util.ArrayList<>(); 

**In the main code(create HashMap for each row and add to list):**
Consider each row has fields: startId, endID, time, flag.

sharedList.add(new java.util.HashMap<String, String>("startId",row1.startId));
<I am not sure how to handle this part>

**In end code(expose the list to other components)**
System.out.print(sharedList.size());

Could you suggest how to create HashMap for each row and add to list. 您能否建议如何为每一行创建HashMap并将其添加到列表。

You need to correct your sharedList declaration from, 您需要从以下位置更正您的sharedList声明,

java.util.List sharedList=new java.util.ArrayList<>();

to

java.util.List<Map<String, String>> sharedList=new java.util.ArrayList<Map<String, String>>();

And your main code should be written something like this, 而且您的主要代码应该这样写,

Map<String, String> rowDataMap = new HashMap<String, String>();
rowDataMap.put("startId",row1.startId);
rowDataMap.put("endID",row1.endID);
rowDataMap.put("time",row1.time);
rowDataMap.put("flag",row1.flag);
sharedList.add(rowDataMap);

Let me know if this looks fine and/or if you have any other queries. 让我知道这是否正常和/或您还有其他疑问。

You can create and initialize a HashMap and add it to a List at once like this, 您可以像这样创建并初始化HashMap并将其立即添加到List中,

List list = new ArrayList();

list.add(new HashMap() {{
    put("a", "b");
}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM