import java.util.Comparator;
import java.util.stream.*;
.
.
.
public class Problem24 {
public static void main(String[] args) {
MyCoordinate myCoordinate = new MyCoordinate();
//myCoordinate의 nearGasStationsList리스트에 GasStationCoordinate 인스턴스를 추가한 것
myCoordinate.AddGasStation("gasStation1");
myCoordinate.AddGasStation("gasStation2");
myCoordinate.AddGasStation("gasStation3");
myCoordinate.AddGasStation("gasStation4");
//스트림에 리스트 요소를 대입..
for(int i = 0 ; i < myCoordinate.nearGasStationsList.size(); i++){
Stream<GasStationCoordinate> gasStationsStream = Stream.of (
myCoordinate.nearGasStationsList.get(i)
);
//GasStationCoordinate 인스턴스의 멤버변수 중 distance을 기준으로 정렬하여 출력함
gasStationsStream.sorted(Comparator.comparing(GasStationCoordinate::getDistance)).forEach(System.out::println);
}
}
}
ArrayList의 요소가 되는 객체가 있고 그 객체가 여러 멤버변수를 가지고 있을 때,
그 객체의 멤버변수 중 하나의 값을 기준으로 정렬하는 코드가 필요했다.
검색해서 코드에는 적용했지만 전혀 이해하지 못했다ㅜㅜ
아직 진도를 나가지 않은
stream 파트와 컬렉션 프레임웍 파트의 Comparator이 사용된 것으로 보인다.
=> 진도 나간 이후 다시 볼 것
'JABA' 카테고리의 다른 글
| 15. 반복문으로 객체 만들기 (0) | 2023.04.05 |
|---|---|
| 13. 객체지향 (1) (0) | 2023.04.04 |
| 12. static (0) | 2023.03.24 |
| 11. 생성자와 Method 메서드 (0) | 2023.03.24 |
| 10. format 출력 printf (0) | 2023.03.24 |