I have 2 list, where as list1 has 5 values and list2 has 2 values as below,
List1 | List2 |
---|---|
A | - |
B | - |
C | 000123 |
D | - |
E | 000876 |
I need to fetch both list and check which list1 value and list2 value And I'm looking for output as below
A has -
B has -
C has 000123
D has -
E has 000876
Can someone help on this? Tried below screenshot of code enter image description here
CodePudding user response:
I am assuming your lists are same length and in list2 empty values are null.
List<String> result = new ArrayList<>();
for (int i = 0; i < list1.size(); i ) {
if(list2.get(i) == null){
result.add(list1.get(i) " has");
}else{
result.add(list1.get(i) " has " list2.get(i));
}
}
System.out.println(String.join(" - ",result));
CodePudding user response:
You can try this. I have considered that list1 and list2 will be of same size.
List<String> list1 = Arrays.asList("A", "B", "C", "D", "E");
List<String> list2 = Arrays.asList("-", "-", "000123", "-", "000876");
Map<String, String> newList = new HashMap<>();
for (int i = 0; i < list1.size(); i ) {
newList.put(list1.get(i), list2.get(i));
}
newList.forEach((key, value) -> System.out.println(key " has " value));
OutPut:
A has -
B has -
C has 000123
D has -
E has 000876