เรียนรู้การใช้งานคอลเลคชั่นในภาษา Java (ArrayList , HashSet , HashMap)
คอลเล็กชัน (Collection)
- เป็นตัวจัดการกลุ่มข้อมูลที่ใช้เก็บข้อมูลหลายๆค่าเอาไว้ด้วยกัน ทำให้เราจัดการข้อมูลได้ง่ายและสะดวกมากขึ้นอีก ทั้งยังมีความยืดหยุ่น สามารถเพิ่มและลดขนาดได้เองอัตโนมัติตามข้อมูลที่มีอยู่
- การใช้งาน Collection จะเรียกใช้งานผ่าน package java.util
ตัวอย่างคอลเล็กชัน (Collection)
- ArrayList — จัดการข้อมูลแบบ Array ในรูปแบบของ Dynamic Size
- HashSet — จัดการข้อมูลไม่ซ้ำกัน
- HashMap — จัดการความสัมพันธ์ของข้อมูล
การใช้งานคอลเล็กชัน (Collection)
- import java.util.ชื่อคลาส;
- import java.util.*;
ArrayList
คลาส ArrayList เป็นหนึ่งในกลุ่ม Collection ที่ใช้เก็บข้อมูลเหมือน Arrayแต่ ArrayList นั้นสามารถย่อและขยายขนาดการเก็บข้อมูลได้อัตโนมัติ
ข้อจำกัดของ Array
- การสร้าง Array นั้นจะต้องกำหนดขนาดของ Array เสมอเมื่อกำหนดแล้วจะไม่สามารถเปลี่ยนแปลงได้ (Fixed Size) และไม่สามารถเก็บข้อมูลได้เกินกว่าขนาดที่กำหนด
- การสร้าง Array ต้องรู้ว่าจะเก็บข้อมูลอะไรบ้าง จึงจะสามารถกำหนดขนาด Array ได้อย่างถูกต้อง ซึ่งเป็นเรื่องที่ยากมาก
ใช้ ArrayList แทน Array
- ArrayList ทำงานคล้ายกับ Array มีการเก็บข้อมูลและอ้างอิงตำแหน่งของข้อมูลผ่านหมายเลขกำกับ (Index)
- ArrayList มีความยืดหยุ่นในการเก็บข้อมูลสามารถเพิ่มหรือลดขนาดได้ตามความต้องการ (Dynamic Size)
การสร้าง ArrayList
import java.utill.ArrayList;
ArrayList <class_name> ชื่อตัวแปร = new ArrayList<>();
import java.util.ArrayList;
import java.util.List;
public class Program {
public static void main(String[] args) {
//แบบไม่กำหนดข้อมูลเริ่มต้น
ArrayList<String> data = new ArrayList<>();
//แบบกำหนดข้อมูลเริ่มต้น
ArrayList<String> newdata = new ArrayList<>(List.of("C","C++","C#"));
}
}
เมธอดจัดการ ArrayList
- add(element) เพิ่มสมาชิกใหม่ 1 รายการ
- add(index,element) เพิ่มสมาชิกใหม่ 1 รายการไปยัง index ที่กำหนด
- addAll([element]) เพิ่มสมาชิกใหม่หลายรายการ
- addAll(index ,[elementt])เพิ่มสมาชิกใหม่หลายรายการไปยัง index
ที่กำหนด - size() จำนวนสมาชิกทั้งหมดใน ArrayList
- get(index) ดึงข้อมูลสมาชิกใน ArrayList จาก index ที่กำหนด
- set(index,element) เปลี่ยนแปลงข้อมูลสมาชิกใน ArrayList
จาก index ที่กำหนด - contains(element) ตรวจสอบว่ามีสมาชิกใน ArrayList หรือไม่
- clear() ลบสมาชิกทั้งหมดออกจาก ArrayList
- remove(element) ลบสมาชิกที่ระบุออกจาก ArrayList
- remove(index) ลบสมาชิกตาม index ที่ระบุออกจาก ArrayList
- indexOf(element) ตรวจสอบลำดับสมาชิกใน ArrayList
import java.util.ArrayList;
public class Program {
public static void main(String[] args) {
ArrayList<String> data = new ArrayList<>();
data.add("Java");
data.add("PHP");
data.add("Python");
data.add(1,"SQL");
data.set(0, "JavaScript");
data.addAll(0,newdata);
data.remove(1);
data.remove("SQL");
data.clear();
System.out.println(data.contains("C++"));
System.out.println(data.indexOf("C++"));
System.out.println(data);
}
}
HashSet
มีลักษณะการทำงานคล้ายกับ ArrayList โดยข้อมูลหรือสมาชิกที่เก็บใน HashSet นั้นต้องมี “ค่าไม่ซ้ำกัน” แต่สมาชิกใน HashSet นั้นจะไม่มีการเรียงลำดับที่ถูกเพิ่มเข้ามา
LinkedHashSet , TreeSet
- LinkedHashSet มีลักษณะคล้ายกับ HashSet แต่สมาชิกจะเรียงลำดับที่ถูกเพิ่มเข้ามา
- TreeSet มีลักษณะคล้ายกับ HashSet แต่สมาชิกจะเรียงลำดับจากน้อยไปมาก , ตามลำดับตัวอักษร หรือลำดับอื่นๆตามที่ผู้ใช้กำหนด
เมธอดจัดการ HashSet , LinkedHashSet , TreeSet
- add(element) เพิ่มสมาชิกใหม่ 1 รายการ
- size() จำนวนสมาชิกทั้งหมดใน HashSet
- contains(element) ตรวจสอบว่ามีสมาชิกใน HashSet หรือไม่
- clear() ลบสมาชิกทั้งหมดออกจาก HashSet
- remove(element) ลบสมาชิกที่ระบุออกจาก HashSet
import java.util.*;
public class Program {
public static void main(String[] args) {
HashSet<String> data = new HashSet<>();
// LinkedHashSet<String> data = new LinkedHashSet<>();
// TreeSet<String> data = new TreeSet<>();
data.add("Java");
data.add("PHP");
data.add("Python");
data.add("C#");
data.add("Dart");
data.add("Assembly");
data.add("PHP");
data.add("R");
data.add("Go");
data.remove("PHP");
data.clear();
System.out.println(data);
System.out.println(data.size());
}
}
HashMap
เป็นรูปแบบการเก็บความสัมพันธ์ของข้อมูลในลักษณะของ key และ value กล่าวคือ จะใช้ key เป็น index ในการเข้าถึงข้อมูล (value) แต่ละตัว ซึ่งค่า key นั้นต้องไม่ซ้ำกัน
การสร้าง HashMap
HashMap<key_type , value_type> ชื่อตัวแปร = new HashMap<>();
key_type คือ ประเภทข้อมูลของคีย์
value_type คือ ประเภทข้อมูลของ value
เมธอดจัดการ HashMap
- put(key,element) เพิ่มสมาชิกใหม่
- get(key) ดึงสมาชิกจากคีย์ที่กำหนด
- size() จำนวนสมาชิกใน HashMap
- clear() ลบสมาชิกทั้งหมดออกจาก HashMap
- remove(key) ลบสมาชิกตามคีย์ที่ระบุออกจาก HashMap
- containsKey(key) ตรวจสอบว่ามีคีย์ใน HashMap หรือไม่
- containsValue(value) ตรวจสอบว่ามีค่าข้อมูลใน HashMap หรือไม่
import java.util.*;
public class Program {
public static void main(String[] args) {
//ตัวอย่างที่ 1
HashMap<String,String> countries = new HashMap<>();
countries.put("TH","Thailand");
countries.put("JP", "Japan");
countries.put("CN", "China");
countries.remove("CN");
countries.clear();
System.out.println(countries.containsValue("Lao"));
System.out.println(countries);
System.out.println(countries.size());
//ตัวอย่างที่ 2
HashMap<Boolean,String> confirm = new HashMap<>();
confirm.put(true,"OK");
confirm.put(false, "Cancel");
System.out.println(confirm);
}
}