java stream List 안의 값 중 가장 큰 데이터 출력
1. java stream List 안의 값 중 가장 큰 데이터 출력
- java stream 을 이용해서 List Element 의 값 중 가장 큰 데이터 출력
2. source
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;
import static org.hamcrest.Matchers.is;
public class StreamListMaxTest {
private List<TestIO> list = new ArrayList<>();
@Before
public void setUp() {
TestIO io = new TestIO("test1", 1000L);
TestIO io2 = new TestIO("test2", 2000L);
list.add(io);
list.add(io2);
}
@Test
public void LIST_값_중_가장큰객체출력_테스트 () throws Exception {
TestIO testIO = list.stream()
.filter(io -> StringUtils.hasText(io.getName()))
.max(Comparator.comparing(TestIO::getAmount))
.orElseThrow(NoSuchElementException::new);
;
Assert.assertThat(testIO.getAmount(), is(2000));
}
public static class TestIO {
private String name;
private Long amount;
public String getName() {
return name;
}
public Long getAmount() {
return amount;
}
public TestIO(String name, Long amount) {
this.name = name;
this.amount = amount;
}
}
}
Author And Source
이 문제에 관하여(java stream List 안의 값 중 가장 큰 데이터 출력), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@rainmaker007/java-stream-List-안의-값-중-가장-큰-데이터-출력저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)