Six Stars

Iterate through HashMap

Hi Friends,

 

The following is my scenario,

 

Map<String, HashSet<String>> iB = new HashMap<String, HashSet<String>>();

if(!iB.containsKey(row5.i)){
iB.put(row5.i,new HashSet<String>());
}
iB.get(row5.i).add(row5.c);

globalMap.put("iBMap", iB);

 

for(String i:globalMap.get("iBMap".keySet())){

row10.issue = i;
StringBuilder sb = new StringBuilder();
for(String c : globalMap.get("iBMap").get(i)) {
sb.append("'").append(cusip).append("'").append(",");
row10.color=c;
}

}

 

Can anyone give some opinions that the reason why this can't go through?

 

Thanks!!

  • Big Data
  • Data Integration
Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
Nine Stars

Re: Iterate through HashMap

You should also give us a hint as to your job structure and any error messages.

 

Just from looking at your code, you're not casting your globalMap.get() results to the appropriate type.  The get() method returns type Object, so you should cast to the right type if you want to use applicable methods.

 

You also need to get the object before you can operate on it.  This means globalMap.get("iBMap".keySet()) won't work, but (Map)globalMap.get("iBMap").keySet() should.

 

Also, since you appear to be using a custom code component, you should get() iBMap once, operate on the local object, and then put() it when finished to save changes. (i.e. assign (Map)globalMap.get("iBMap") to a local variable and loop over it, retrieving keys and values from it rather than constantly getting the object from globalMap).

 

All that said, this type of setup doesn't really leverage Talend's strengths.  With a more general explanation of your use case, we may be able to suggest an approach that doesn't involve custom Java code.

5 REPLIES
Nine Stars TRF
Nine Stars

Re: Iterate through HashMap

What do you expect?

TRF
Six Stars

Re: Iterate through HashMap

Thanks for your reply, TRF.

 

My expectation is passing the value of HashMap(In my case it's a HashSet) to row10..

Nine Stars TRF
Nine Stars

Re: Iterate through HashMap

You should try to expose your use case with sample data and expected result.

TRF
Nine Stars

Re: Iterate through HashMap

You should also give us a hint as to your job structure and any error messages.

 

Just from looking at your code, you're not casting your globalMap.get() results to the appropriate type.  The get() method returns type Object, so you should cast to the right type if you want to use applicable methods.

 

You also need to get the object before you can operate on it.  This means globalMap.get("iBMap".keySet()) won't work, but (Map)globalMap.get("iBMap").keySet() should.

 

Also, since you appear to be using a custom code component, you should get() iBMap once, operate on the local object, and then put() it when finished to save changes. (i.e. assign (Map)globalMap.get("iBMap") to a local variable and loop over it, retrieving keys and values from it rather than constantly getting the object from globalMap).

 

All that said, this type of setup doesn't really leverage Talend's strengths.  With a more general explanation of your use case, we may be able to suggest an approach that doesn't involve custom Java code.

Six Stars

Re: Iterate through HashMap

Thanks so much, it works!