I have a two List List of ColumnIndexMapping and other is List of String
One list contains an Object of a class. Structure is look like below-
public class ColumnIndexMapping {
private int index;
private String columnname;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getColumnname() {
return columnname;
}
public void setColumnname(String columnname) {
this.columnname = columnname;
}
}
Now I have to make a Map using both List. I have to read only those indexes from second list (List of String) for which we have index property in first list. like below -
List<ColumnIndexMapping> colMapList = [
{
"index": 0,
"columnname": "accountname"
},
{
"index": 2,
"columnname": "source"
},{
"index": 4,
"columnname": "customField13"
} ]
List<String> secondList = {"Nitin","India","1234","fnf","qwerty"}
Output would be ->
HashMap<String,String> outputHashmap =
{
"accountname":"Nitin",
"source":"1234",
"customField13":"qwerty"
}
tried approach with Java7
Map<String, String> output = new HashMap<>();
for (ColumnIndexMapping colMap: colMapList) {
output.put(colMap.getColumnname(), secondList .get(colMap.getIndex()));
}
Now what is the best way to doing this in Java8.
ListandMapin Java. Please, post the code that compiles. And, yes, you haven't tried anything. Wha would be your approach in Java 7?