কম্পিউটার টিউটোরিয়াল

Android-এ LinkedList রিভার্স (Reverse) করার পদ্ধতি

Android-এ LinkedList রিভার্স করা: Collections.reverse() ব্যবহার করে

এই টিউটোরিয়ালে আমরা দেখব কিভাবে LinkedList-কে সর্ট করার পর Collections.reverse() ব্যবহার করে উল্টো করা যায়, এবং LinkedHashSet দিয়ে ডুপ্লিকেট সরিয়ে ফলাফল TextView-এ দেখানো যায়।

প্রয়োজনীয় ধাপসমূহ

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio → File → New Project → প্রয়োজনীয় ডিটেইলস পূরণ করুন।

ধাপ ২: লেআউট ফাইল (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:orientation="vertical"
    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" />

</LinearLayout>

ধাপ ৩: MainActivity.java

package com.example.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        // ১. সর্ট করুন (অসেন্ডিং)
        Collections.sort(linkedList);  // [2, 2, 5, 5, 7, 8, 9]

        // ২. রিভার্স করুন (ডিসেন্ডিং)
        Collections.reverse(linkedList);  // [9, 8, 7, 5, 5, 2, 2]

        // ৩. ডুপ্লিকেট সরিয়ে অর্ডার রক্ষা (LinkedHashSet)
        LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(linkedList); // [9, 8, 7, 5, 2]

        TextView textView = findViewById(R.id.text);
        textView.setText("সর্টেড + রিভার্সড + অনIQUE: " + hashSet.toString());
    }
}

ফলাফল

প্রক্রিয়া:

  1. অরিজিনাল: [2, 8, 2, 5, 7, 5, 9]
  2. সর্টেড: [2, 2, 5, 5, 7, 8, 9]
  3. রিভার্সড: [9, 8, 7, 5, 5, 2, 2]
  4. অনIQUE (LinkedHashSet): [9, 8, 7, 5, 2]

Android-এ LinkedList রিভার্স (Reverse) করার পদ্ধতি

গভীর ব্যাখ্যা

  • Collections.sort() — ন্যাচারেল অর্ডারিংয়ে সর্ট করে (O(n log n))।
  • Collections.reverse() — লিস্টের অর্ডার উল্টো করে (O(n))।
  • LinkedHashSet — ডুপ্লিকেট সরিয়ে ইনসারশন অর্ডার রক্ষা করে।

শুধু রিভার্স করতে চাইলে (সর্ট ছাড়া)

Collections.reverse(linkedList); // মূল অর্ডার উল্টো হয়ে যাবে

সারসংক্ষেপ

Collections.sort() + Collections.reverse() + LinkedHashSet কম্বিনেশন দিয়ে একটি লিংকড লিস্টকে সর্টেড, রিভার্সড এবং ডুপ্লিকেট-ফ্রি করা যায়। এটি ডেটা প্রসেসিং পাইপলাইনের একটি সাধারণ প্যাটার্ন।

  1. Android-এ ListView-এর জন্য LinkedList-এন সব এলিমেন্ট মুছে ফেলার পদ্ধতি

  2. Android-এ AlertDialog-এ সাধারণ লিস্ট যোগ করার উপায়

  3. এন্ড্রয়েডে SearchView ব্যবহার করার পদ্ধতি

  4. Android-এ ইনস্টল করা অ্যাপ্লিকেশনগুলির তালিকা পাওয়ার পদ্ধতি