import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Device> devices = new ArrayList<>();
int n = sc.nextInt();
for(int i=0; i<n; i++){
Device d = new Device(sc.nextInt(), sc.nextInt());
devices.add(d);
}
sc.close();
Collections.sort(devices);
for(Device d : devices){
System.out.println(d.id + " " + d.gas);
}
}
}
class Device implements Comparable<Device> {
private int id;
private int gas;
Device(int id, int gas){
this.id = id;
this.gas = gas;
}
public int compareTo(Device d){
return id - d.id;
}
}
Comparable, compareTo로 정렬
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Device> devices = new ArrayList<>();
int n = sc.nextInt();
for(int i=0; i<n; i++){
Device d = new Device(sc.nextInt(), sc.nextInt());
devices.add(d);
}
sc.close();
devices = devices.stream().sorted((e2, e1) -> e2.id - e1.id).collect(Collectors.toList());
for(Device d : devices){
System.out.println(d.id + " " + d.gas);
}
}
}
class Device {
int id;
int gas;
Device(int id, int gas){
this.id = id;
this.gas = gas;
}
}
stream으로 정렬
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Device> devices = new ArrayList<>();
int n = sc.nextInt();
for(int i=0; i<n; i++){
Device d = new Device(sc.nextInt(), sc.nextInt());
devices.add(d);
}
sc.close();
String output = devices
.stream()
.sorted(Comparator.comparingInt(Device::getId))
.map(e -> "%d %d".formatted(e.getId(), e.getGas()))
.collect(Collectors.joining("\n"));
System.out.println(output);
}
}
class Device {
private int id;
private int gas;
Device(int id, int gas){
this.id = id;
this.gas = gas;
}
public int getId() {
return id;
}
public int getGas() {
return gas;
}
}
stream 바로 출력
'Coding Test > 코드업' 카테고리의 다른 글
[코드업/자바] 3004 - 데이터 재정렬 (0) | 2023.02.28 |
---|