|
private int accNo;
private String accType;
private String accStatus;
public Account(int accountNo, String accountType, String accountStatus){
accNo = accountNo;
accType = accountType;
accStatus = accountStatus;
}
}
|
|
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ListtoMap {
public static void main(String[] args) {
List<Account> li = new ArrayList<Account>();
Account ac1= new Account(100,"Savings","Open");
Account ac2= new Account(101,"Checking","Closed");
li.add(ac1);
li.add(ac2);
// Before Java 8
Map<Integer, String> beforeJava8 = new HashMap<>();
for (Account acc: li) {
beforeJava8.put(acc.getAccNo(), acc.getAccStatus());
}
System.out.println("Before Java 8 "+beforeJava8);
// Java 8 Using Lambda expression and Stream API
Map<Integer, String> map = li.stream().collect(Collectors.toMap(x -> x.getAccNo() , x -> x.getAccStatus()));
System.out.println("In Java 8 "+map);
}
}
|
Before Java 8 {100=Open, 101=Closed}
Java 8 {100=Open, 101=Closed}