I have this method:
public String nodePropertyGenerator(Map<String,Object> properties){
String prop="";
for(Map.Entry<String, Object> entry : properties.entrySet()) {
prop += entry.getKey() + ":'" +entry.getValue()+"' ,";
Iterator iterator = properties.entrySet().iterator();
}
return prop;
}
I want to this method return a string like this:
name:'hossein' , age:'24' , address:'khorram'
so I want to check if my map's element be end element , it remove "," from end of string. how to do that. please help me. thanks
You can't tell whether the current iteration is the last one. I suggest you use a StringBuilder
instead of repeated string concatenation, then just trim the length at the end:
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, Object> entry : properties.entrySet()) {
builder.append(entry.getKey())
.append(":'")
.append(entry.getValue())
.append("' ,");
}
if (builder.length() > 0) {
builder.setLength(builder.length() - 2);
}
return builder.toString();
See more on this question at Stackoverflow