简体   繁体   中英

Java: List how to get specific element on a line

I'm trying to get a specific element from a list, but not the entire list. For example if I do

System.out.println(words.get(0))

I would get

[16, Matthew, 13,954, Alexis, 8,181]

But how would I get a specific element on this line, like the name Matthew and not the whole line. [Matthew]

 List<List<String>> words = topNames.stream()
                .map(sentence -> sentence.split("\\s+"))
                .map(Arrays::asList)
                .collect(Collectors.toList());


            System.out.println(words.get(0));

Add a map() operation to map to the elements at the specified index :

 List<String> wordsAtTheOneIndex = topNames.stream()
                .map(sentence -> sentence.split("\\s+"))
                .map(Arrays::asList)
                .map(l -> l.get(1))
                .collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM