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

Cách đảo ngược LinkedList trong Android

Giới thiệu

Ví dụ này minh họa cách đảo ngược (reverse) một LinkedList trong Android, kết hợp sắp xếp và loại bỏ trùng lặp.

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.Collections;
import java.util.LinkedHashSet;
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);

        // 1. Sắp xếp tăng dần
        Collections.sort(linkedList);
        // linkedList: [2, 2, 5, 5, 7, 8, 9]

        // 2. Đảo ngược (giảm dần)
        Collections.reverse(linkedList);
        // linkedList: [9, 8, 7, 5, 5, 2, 2]

        // 3. Loại bỏ trùng lặp, giữ thứ tự hiện tại (giảm dần)
        LinkedHashSet uniqueReversed = new LinkedHashSet<>(linkedList);
        // uniqueReversed: [9, 8, 7, 5, 2]

        TextView textView = findViewById(R.id.text);
        textView.setText("Gốc: [2, 8, 2, 5, 7, 5, 9]\n\n" +
            "Sau sort: [2, 2, 5, 5, 7, 8, 9]\n\n" +
            "Sau reverse: [9, 8, 7, 5, 5, 2, 2]\n\n" +
            "Kết quả (unique + reverse): " + uniqueReversed);
    }
}

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ả

Kết quả cuối cùng: [9, 8, 7, 5, 2] (giảm dần, không trùng lặp)

Các cách đảo ngược LinkedList

// Cách 1: Collections.reverse() - thay đổi list gốc
Collections.reverse(linkedList);

// Cách 2: descendingIterator() - duyệt ngược, không thay đổi list
for (Integer i : linkedList.descendingIterator()) { ... }

// Cách 3: ListIterator từ cuối
ListIterator it = linkedList.listIterator(linkedList.size());
while (it.hasPrevious()) { ... }

// Cách 4: Java 8 Stream (API 24+)
List reversed = linkedList.stream()
    .sorted(Comparator.reverseOrder())
    .collect(Collectors.toList());

Lưu ý: Code gốc có @RequiresApi không cần thiết. Collections.reverse() và LinkedHashSet hoạt động mọi API level. Logic: sort → reverse → unique cho kết quả giảm dần không trùng.