Table of Contents
1. Giới thiệu
Lớp LinkedHashSet trong Java kế thừa HashSet và triển khai Set Interface. Nó tạo một collection mà sử dụng một linked list để lưu giữ các phần tử theo thứ tự chúng đã được chèn.
Các điểm quan trọng về lớp LinkedHashSet trong java là:
- LinkedHashSet chỉ chứa các phần tử duy nhất, không chấp nhận 2 phần tử trùng nhau.
- LinkedHashSet đảm bảo thứ tự được thêm vào.
- LinkedHashSet sử dụng đối tượng LinkedHashMap nội bộ để lưu trữ và xử lý các phần tử của nó.
- LinkedHashSet cho phép chứa phần tử NULL.
- LinkedHashSet không được đồng bộ. Để có LinkedHashSet đồng bộ, hãy sử dụng phương thức Collections.synchronizedSet ().
2. Hierarchy của lớp TreeSet
Lớp java.util.LinkedHashSet được định nghĩa như sau:
public class LinkedHashSet<E>
extends HashSet<E>
implements Set<E>, Cloneable, java.io.Serializable {
private static final long serialVersionUID = -2851667679971038690L;
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true);
}
}
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable
{
static final long serialVersionUID = -5024744406713321676L;
private transient HashMap<E,Object> map;
private static final Object PRESENT = new Object();
public HashSet() {
map = new HashMap<>();
}
}
3. Các phương thức khởi tạo (constructor) của lớp LinkedHashSet
- LinkedHashSet(): khởi tạo một danh sách mảng trống.
- LinkedHashSet(Collection c): khởi tạo một danh sách với các phần tử của collection c.
4. Các phương thức (method) của lớp LinkedHashSet
Xem thêm các phương thức của Set ở bài viết Set Interface trong java.
5. Ví dụ minh họa
5.1. Ví dụ sử dụng LinkedHashSet với kiểu dữ liệu cơ bản (Wrapper)
package com.maixuanviet.collection.linkedhashset;
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSetExample {
public static final int NUM_OF_ELEMENT = 5;
public static void main(String[] args) {
// Create set
Set<String> set = new LinkedHashSet<>();
set.add("Item01");
set.add("Item02");
set.add("Item03");
set.add("Item04");
set.add("Item05");
set.add("Item02");
set.add("Item03");
// Show set through for-each
for (String item : set) {
System.out.print(item + " ");
}
}
}
Kết quả thực thi chương trình trên:
Item01 Item02 Item03 Item04 Item05
5.2. Ví dụ sử dụng LinkedHashSet với kiểu do người dùng tự định nghĩa (Object)
package com.maixuanviet.collection.linkedhashset;
import java.util.LinkedHashSet;
import java.util.Set;
class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
}
public class LinkedHashSetExample {
public static final int NUM_OF_ELEMENT = 5;
public static void main(String[] args) {
// Create list
Set<Student> students = new LinkedHashSet<>();
Student student1 = new Student(1, "myname1");
Student student2 = new Student(2, "myname2");
Student student3 = new Student(3, "myname3");
Student student4 = new Student(4, "myname4");
Student student5 = new Student(5, "myname5");
students.add(student1);
students.add(student3);
students.add(student2);
students.add(student5);
students.add(student4);
students.add(student2);
students.add(student3);
// Show set student
for (Student student : students) {
System.out.println(student);
}
}
}
Kết quả thực thi chương trình trên:
Student [id=1, name=myname1] Student [id=3, name=myname3] Student [id=2, name=myname2] Student [id=5, name=myname5] Student [id=4, name=myname4]
Related posts:
Java Program to Implement LinkedHashMap API
Rate Limiting in Spring Cloud Netflix Zuul
Java Program to Implement Solovay Strassen Primality Test Algorithm
Add Multiple Items to an Java ArrayList
Assertions in JUnit 4 and JUnit 5
Xây dựng ứng dụng Client-Server với Socket trong Java
Spring Boot - Google Cloud Platform
Converting Between an Array and a Set in Java
Hướng dẫn Java Design Pattern – Composite
Java Program to Implement Sieve Of Eratosthenes
Introduction to Using FreeMarker in Spring MVC
Java Program to Represent Linear Equations in Matrix Form
Java Program to Solve a Matching Problem for a Given Specific Case
Java Program to Implement Gaussian Elimination Algorithm
Spring Boot Integration Testing with Embedded MongoDB
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Format ZonedDateTime to String
Java Program to Implement Ford–Fulkerson Algorithm
An Intro to Spring Cloud Security
Quick Guide to @RestClientTest in Spring Boot
Spring Boot - Actuator
Immutable Objects in Java
Java Program to Implement Stack using Linked List
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Removing all Nulls from a List in Java
Jackson – Decide What Fields Get Serialized/Deserialized
Allow user:password in URL
Java Program to Implement Ternary Search Algorithm

