การรวมอาร์เรย์ 2 ชุดและลบข้อมูลซ้ำ (Merge Arrays Without Duplicates)
ตัวอย่างสาธิตการใช้ System.arraycopy() รวมอาร์เรย์ แล้วใช้ HashSet กรองข้อมูลซ้ำ ก่อนแสดงใน ListView
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project
ขั้นตอนที่ 2: Layout XML (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
ขั้นตอนที่ 3: โค้ด Java (src/MainActivity.java)
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] names = new String[] { "A", "B", "C" };
String[] extended = new String[5]; // ขนาด 5
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = findViewById(R.id.list);
// เพิ่มข้อมูลใน extended index 3, 4
extended[3] = "D";
extended[4] = "E";
// คัดลอก names เข้า extended ตำแหน่ง 0-2
// extended กลายเป็น [A, B, C, D, E]
System.arraycopy(names, 0, extended, 0, names.length);
// ใช้ HashSet กรองซ้ำ (Set ไม่อนุญาต duplicate)
HashSet<String> unique = new HashSet<>(Arrays.asList(extended));
unique.addAll(Arrays.asList(names)); // เพิ่ม names อีกครั้ง (ไม่ซ้ำ)
// แปลงกลับเป็น ArrayList สำหรับ Adapter
ArrayList<String> arrayList = new ArrayList<String>(unique);
ArrayAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, arrayList);
list.setAdapter(adapter);
}
}
การทดสอบ
รันแอป จะแสดง A, B, C, D, E (ไม่ซ้ำ) แม้ names จะถูกเพิ่มเข้าไป 2 ครั้ง

วิธีอื่นๆ ที่ทันสมัยกว่า (Java 8+ / API 24+)
1. ใช้ Stream API (กระชับที่สุด)
List<String> merged = Stream.of(names, extended)
.flatMap(Arrays::stream)
.filter(Objects::nonNull) // กรอง null
.distinct() // ลบซ้ำ
.collect(Collectors.toList());
2. ใช้ Collections.addAll + LinkedHashSet (รักษาลำดับ)
Set<String> unique = new LinkedHashSet<>(); Collections.addAll(unique, names); Collections.addAll(unique, extended); // unique = [A, B, C, D, E] - รักษาลำดับการเพิ่ม
3. ใช้ Apache Commons Collections
Collection<String> union = CollectionUtils.union(
Arrays.asList(names), Arrays.asList(extended));
เปรียบเทียบประสิทธิภาพ
| วิธี | Time Complexity | รักษาลำดับ | Min API |
|---|---|---|---|
| HashSet + addAll | O(n) | ไม่ | All |
| LinkedHashSet | O(n) | ใช่ | All |
| Stream.distinct() | O(n) | ใช่ (encounter order) | 24 |
| Manual loop + contains | O(n²) | ใช่ | All |
ข้อควรระวัง
System.arraycopyต้องการ Array ปลายทางมีขนาดพอHashSetไม่รักษาลำดับ ใช้LinkedHashSetถ้าต้องการ- กรอง
nullก่อน add เข้า Set (ถ้าอาจมี null)