Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert List<Object[]> to Map<String,BigInteger> with Streams & Lambda Java8

I have the data as:

List<Object[]> result=fromDB();

How can I write the code below with Streams in Java 8?

Map<String,BigInteger> map= new HashMap<>();
for (Object[] obj : result) {
    map.put((String)obj[0], (BigInteger)obj[1])
}
like image 698
Rob Avatar asked Aug 24 '26 19:08

Rob


1 Answers

Map<String, BigInteger> map =
    fromDB().stream()
            .collect(Collectors.toMap(
                o -> (String) o[0],
                o -> (BigInteger) o[1],
                (b1, b2) -> b2
            ));
like image 71
Tunaki Avatar answered Aug 27 '26 09:08

Tunaki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!