0

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.

7
  • What have you tried so far? Commented Aug 23, 2019 at 7:06
  • It's not how you define List and Map in Java. Please, post the code that compiles. And, yes, you haven't tried anything. Wha would be your approach in Java 7? Commented Aug 23, 2019 at 7:07
  • @Jason I tried using Java7 approach. simply iterate both list using for loop and then do the logic but i want to achieve this using Java8 Commented Aug 23, 2019 at 7:08
  • why you have downgraded this question, I know the solution using java7 but i want to get resolve this using Java8. Commented Aug 23, 2019 at 7:09
  • 2
    @nitintyagi I downvoted. You've shown no effort (put a POJO class, and two JSON snippets into Java code which made it invalid). You didn't show how you would solve the problem by the standard means, either. I am open to upvote as soon as you edit the question. Commented Aug 23, 2019 at 7:20

2 Answers 2

2

If you are looking for Java 8 stream-based approach specifically, the following would do.

Map<String, String> outputMap = colMapList.stream()
            .collect(Collectors.toMap(ColumnIndexMapping::getColumnname, 
                    (colMap) -> secondList.get(colMap.getIndex())));
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you have populated the colMapList and secondList correctly, the conversion code is relatively simple:

List<ColumnIndexMapping> colMapList = ...; // assumed this is populated correctly
List<String> secondList = ...;             // assumed this is populated correctly

Map<String, String> output = new HashMap<>();
for (ColumnIndexMapping mapping : colMapList) {
    output.put(mapping.getColumnname(), secondList.get(mapping.getIndex()));
}

1 Comment

I'm assuming that you've already checked there will be enough entries in the secondList to populate the elements specified by the colMapList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.