การหาค่าต่ำสุดและสูงสุดจาก List ใน Java
มีหลายวิธีในการหาค่า Min/Max จาก List ขึ้นอยู่กับว่า List เรียงลำดับแล้วหรือยัง และ Java version
วิธีที่ 1: เรียงลำดับก่อน (Sorting) - ตัวอย่างต้นฉบับ
import java.util.*;
public class Demo {
public static void main(String[] args) {
List inputList = new ArrayList<>(Arrays.asList(500, 650, 300, 250, 110));
System.out.println("ลิสต์: " + inputList);
// สร้างสำเนาแล้วเรียง (ไม่เปลี่ยนลิสต์ต้นฉบับ)
List sortedList = new ArrayList<>(inputList);
Collections.sort(sortedList);
if (sortedList.isEmpty()) {
System.out.println("ลิสต์ว่าง");
return;
}
int min = sortedList.get(0);
int max = sortedList.get(sortedList.size() - 1);
System.out.println("ค่าต่ำสุด: " + min);
System.out.println("ค่าสูงสุด: " + max);
}
}
ผลลัพธ์
ลิสต์: [500, 650, 300, 250, 110]
ค่าต่ำสุด: 110
ค่าสูงสุด: 650
วิธีที่ 2: Collections.min()/max() - ไม่ต้องเรียง (O(n))
List list = Arrays.asList(500, 650, 300, 250, 110);
int min = Collections.min(list); // 110
int max = Collections.max(list); // 650
System.out.println("Min: " + min + ", Max: " + max);
วิธีที่ 3: Stream API (Java 8+) - แนะนำ
import java.util.*;
import java.util.stream.*;
public class StreamMinMax {
public static void main(String[] args) {
List list = Arrays.asList(500, 650, 300, 250, 110);
// หา min/max แยกกัน
OptionalInt minOpt = list.stream().mapToInt(Integer::intValue).min();
OptionalInt maxOpt = list.stream().mapToInt(Integer::intValue).max();
minOpt.ifPresent(min -> System.out.println("Min: " + min));
maxOpt.ifPresent(max -> System.out.println("Max: " + max));
// หรือหาทั้งคู่พร้อมกัน (single pass)
IntSummaryStatistics stats = list.stream()
.mapToInt(Integer::intValue)
.summaryStatistics();
System.out.println("Min: " + stats.getMin());
System.out.println("Max: " + stats.getMax());
System.out.println("Avg: " + stats.getAverage());
System.out.println("Sum: " + stats.getSum());
System.out.println("Count: " + stats.getCount());
}
}
วิธีที่ 4: Manual Loop (O(n), ไม่ต้อง import)
public class ManualMinMax {
public static void findMinMax(List list) {
if (list == null || list.isEmpty()) {
System.out.println("ลิสต์ว่าง");
return;
}
int min = list.get(0);
int max = list.get(0);
for (int i = 1; i < list.size(); i++) {
int val = list.get(i);
if (val < min) min = val;
if (val > max) max = val;
}
System.out.println("Min: " + min + ", Max: " + max);
}
}
ตัวอย่างแบบ OOP (Encapsulation)
import java.util.*;
public class Demo {
static Integer getMin(List list) {
if (list == null || list.isEmpty()) return null;
return Collections.min(list);
}
static Integer getMax(List list) {
if (list == null || list.isEmpty()) return null;
return Collections.max(list);
}
public static void main(String[] args) {
List inputList = new ArrayList<>(Arrays.asList(500, 650, 300, 250, 110));
System.out.println("ลิสต์: " + inputList);
System.out.println("ค่าต่ำสุด: " + getMin(inputList));
System.out.println("ค่าสูงสุด: " + getMax(inputList));
}
}
การเปรียบเทียบประสิทธิภาพ
| วิธี | Time Complexity | Space | หมายเหตุ |
|---|---|---|---|
| Sort + get(0)/get(last) | O(n log n) | O(n) | เปลี่ยนลำดับลิสต์ (ต้อง copy ก่อน) |
| Collections.min/max | O(n) | O(1) | สองรอบการวนลูป |
| Stream summaryStatistics | O(n) | O(1) | หา min, max, avg, sum, count รอบเดียว |
| Manual Loop | O(n) | O(1) | เร็วที่สุด ควบคุมได้เต็มที่ |
ข้อควรระวัง
- List ว่าง: Collections.min/max โยน
NoSuchElementException - Stream: คืนค่า
Optionalต้องตรวจสอบisPresent() - Custom objects: ต้อง implement
Comparableหรือส่งComparator