Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách xóa trùng lặp từ LinkedList chưa sắp xếp trong Android

Giới thiệu

Ví dụ này minh họa cách loại bỏ các phần tử trùng lặp từ một LinkedList chưa được sắp xếp (unsorted) trong Android, sử dụng HashSet.

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền đầy đủ thông tin cần thiết.

Bước 2: Thiết kế giao diện (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:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:gravity="center"
        android:padding="16dp" />

</LinearLayout>

Bước 3: MainActivity.java

package com.example.myapplication;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.HashSet;
import java.util.LinkedList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinkedList linkedList = new LinkedList<>();
        linkedList.add(2);
        linkedList.add(8);
        linkedList.add(2);
        linkedList.add(5);
        linkedList.add(7);
        linkedList.add(5);
        linkedList.add(9);

        // HashSet: Loại bỏ trùng lặp, KHÔNG giữ thứ tự
        HashSet hashSet = new HashSet<>(linkedList);

        TextView textView = findViewById(R.id.text);
        textView.setText("Danh sách gốc (unsorted): " + linkedList + 
            "\n\nSau khi xóa trùng (HashSet - thứ tự ngẫu nhiên): " + hashSet);
    }
}

Chạy ứng dụng

Kết nối thiết bị Android thật với máy tính. Trong Android Studio, nhấn Run và chọn thiết bị để chạy.

Kết quả

Danh sách gốc: [2, 8, 2, 5, 7, 5, 9]

Kết quả HashSet: [2, 5, 7, 8, 9] (thứ tự không đảm bảo, tùy triển khai hash)

So sánh các Set để xóa trùng lặp

Set ImplementationGiữ thứ tựSắp xếpHiệu năng
HashSetKhôngKhôngO(1) tốt nhất
LinkedHashSetCó (thứ tự thêm)KhôngO(1)
TreeSetKhôngCó (tăng dần)O(log n)

Giữ thứ tự xuất hiện đầu tiên (LinkedHashSet)

// Giữ thứ tự xuất hiện ĐẦU TIÊN trong list gốc
LinkedHashSet orderedUnique = new LinkedHashSet<>(linkedList);
// Kết quả: [2, 8, 5, 7, 9] - 2 và 5 chỉ giữ lần đầu xuất hiện

Sắp xếp sau khi xóa trùng (TreeSet)


// Tự động sắp xếp tăng dần
TreeSet sortedUnique = new TreeSet<>(linkedList);
// Kết quả: [2, 5, 7, 8, 9]

Lưu ý: Code gốc dùng @RequiresApi không cần thiết. HashSet constructor từ Collection tự động loại trùng. Chọn Set phù hợp: HashSet (nhanh nhất, không quan tâm thứ tự), LinkedHashSet (giữ thứ tự), TreeSet (cần sắp xếp).