java8 convert string collection to long collection

java8 convert string collection to long collection

Today, I encountered a thing in development. The interface parameter type is List, and we get the stream as List, which needs to be converted. Without further ado, the source code is as follows:
public static void main (String [] args) {

	List<TestVo> listVo = new ArrayList<>();
	listVo.add(new TestVo("xiaoming","28"));
	listVo.add(new TestVo("xiaowang","26"));
	
	listVo.forEach(vo -> System.out.println(vo));
	
	List<String> sl = listVo.stream().map(TestVo :: getAge).collect(Collectors.toList());
	sl.forEach(vo -> System.out.println(vo));
	// 此处还有个mapToLong 但不知道杂用
	List<Long> ll = listVo.stream().map(TestVo :: getAge).map(x -> Long.valueOf(x)).collect(Collectors.toList());
	ll.forEach(vo -> System.out.println(vo));
}
Published 6 original articles · liked 0 · visits 354

Guess you like

Origin blog.csdn.net/weixin_44863376/article/details/103544394