ก่อนเข้าตัวอย่าง ควรทราบว่า CopyOnWriteArrayList คืออะไร นี่คือเวเรียนท์ที่ปลอดภัยต่อเธรด (thread-safe) ของ ArrayList ซึ่งดำเนินการเพิ่ม ตั้งค่า และอื่นๆ โดยการสร้างสำเนาใหม่ของอาร์เรย์พื้นฐาน
ตัวอย่างการใช้งาน subList() ใน Android CopyOnWriteArrayList
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่ใน Android Studio
ไปที่ File → New Project และกรอกรายละเอียดที่จำเป็นทั้งหมด
ขั้นตอนที่ 2: เพิ่มโค้ดใน res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/actionEvent"
android:textSize="40sp"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
ใช้ TextView แสดงองค์ประกอบของ CopyOnWriteArrayList
ขั้นตอนที่ 3: เพิ่มโค้ดใน src/MainActivity.java
package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.concurrent.CopyOnWriteArrayList;
public class MainActivity extends AppCompatActivity {
CopyOnWriteArrayList<String> copyOnWriteArrayList;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
copyOnWriteArrayList = new CopyOnWriteArrayList<>();
final TextView actionEvent = findViewById(R.id.actionEvent);
copyOnWriteArrayList.add("sai");
copyOnWriteArrayList.add("ram");
copyOnWriteArrayList.add("krishna");
copyOnWriteArrayList.add("prasad");
copyOnWriteArrayList.add("ram");
actionEvent.setText("" + copyOnWriteArrayList);
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// subList(fromIndex inclusive, toIndex exclusive)
actionEvent.setText("" + copyOnWriteArrayList.subList(0, 2));
}
});
}
}
การทดสอบแอปพลิเคชัน
รันแอปพลิเคชันบนอุปกรณ์ Android จริง ใน Android Studio คลิกไอคอน Run เลือกอุปกรณ์ แล้วตรวจสอบหน้าจอเริ่มต้น

คลิก TextView จะแสดง SubList ตำแหน่ง 0 ถึง 1 (ไม่รวม 2) คือ "sai", "ram"

คำอธิบาย subList()
subList(int fromIndex, int toIndex)คืนค่า View ของรายการย่อย (SubList) จากfromIndex(รวม) ถึงtoIndex(ไม่รวม)- SubList ที่ได้ เชื่อมโยงกับ List ต้นทาง - การเปลี่ยนแปลง SubList จะส่งผลต่อ List เดิม และในทางกลับกัน
- รองรับอินดั๊กซ์ติดลบไม่ได้ (จะโยน IndexOutOfBoundsException)
คำเตือนสำคัญ (Important Caveats)
⚠️ CopyOnWriteArrayList.subList() ไม่ปลอดภัยต่อเธรด (Not Thread-Safe) สำหรับการ Mutate!
- แม้ CopyOnWriteArrayList จะ Thread-Safe แต่ SubList ที่ได้ ไม่ได้ สร้างสำเนาเมื่อแก้ไข
- การ
add,remove,setบน SubList อาจทำให้เกิด ConcurrentModificationException หรือข้อมูลไม่สอดคล้อง - แนะนำ: ใช้ SubList สำหรับ Read-Only เท่านั้น (iteration, streaming) หรือสร้างสำเนาใหม่
new CopyOnWriteArrayList<>(list.subList(...))
กรณีใช้งานที่เหมาะสม
- อ่านข้อมูลบางส่วนเพื่อนำไปแสดงผล (Read-only)
- สตรีมข้อมูลด้วย Stream API:
list.subList(0, 10).stream()... - แบ่งหน้า (Pagination) แบบอ่านอย่างเดียว