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

Android-এ আনসর্টেড LinkedList থেকে ডুপ্লিকেট সরানোর পদ্ধতি

Android-এ আনসর্টেড (Unsorted) LinkedList-এ ডুপ্লিকেট সরিয়ে ফেলা

এই টিউটোরিয়ালে আমরা দেখব কিভাবে HashSet ব্যবহার করে একটি আনসর্টেড LinkedList-এর ডুপ্লিকেট এলিমেন্টগুলো সরিয়ে অনIQUE মানের সেট তৈরি করা যায়। নোট: HashSet অর্ডার রক্ষা করে না।

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

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

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.HashSet;
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);

        // HashSet ব্যবহার করে ডুপ্লিকেট সরান (অর্ডার রক্ষা হয় না)
        HashSet<Integer> hashSet = new HashSet<>(linkedList);

        TextView textView = findViewById(R.id.text);
        textView.setText("ডুপ্লিকেট বাদ (অর্ডার নেই): " + hashSet.toString());
    }
}

ফলাফল

ইনপুট: [2, 8, 2, 5, 7, 5, 9]

আউটপুট (HashSet - র‍্যান্ডম অর্ডার): [2, 5, 7, 8, 9] (অর্ডার ভিন্ন হতে পারে)

Android-এ আনসর্টেড LinkedList থেকে ডুপ্লিকেট সরানোর পদ্ধতি

HashSet vs LinkedHashSet - পার্থক্য

যদি অর্ডার রক্ষা করতে চান (Unsorted List-এ)

// LinkedHashSet ব্যবহার করুন
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>(linkedList);
// ফলাফল: [2, 8, 5, 7, 9] (প্রথম আবির্ভাবের অর্ডার রক্ষা)

Java 8+ Stream API দিয়ে (অর্ডার রক্ষা সহ)

linkedList.stream().distinct().collect(Collectors.toCollection(LinkedHashSet::new));

সারসংক্ষেপ

আনসর্টেড লিংকড লিস্ট থেকে ডুপ্লিকেট সরিয়ে ফেলার সবচে্য সহজ উপায় new HashSet<>(list)। কিন্তু অর্ডার ম হয়ে গেলে LinkedHashSet ব্যবহার করুন। HashSet O(1) গড় টাইমে ইনসার্ট/লুকআপ দেয়।

  1. অ্যান্ড্রয়েডে ListView-এ ক্লিক করলে আইটেম মুছে ফেলা

  2. Android-এ প্রোগ্রাম্যাটিক্যালি নোটিফিকেশন বার থেকে নোটিফিকেশন সরানোর পদ্ধতি

  3. অ্যান্ড্রয়েড থেকে ম্যালওয়্যার সরানোর উপায়

  4. আপনার অ্যান্ড্রয়েড ফোন থেকে ভাইরাস নিরাপদে রিমুভ করার ধাপে ধাপে গাইড

কপিরাইট © https://bd.wsxdn.com সমস্ত অধিকার সংরক্ষিত
বৈশিষ্ট্যHashSetLinkedHashSet
ডুপ্লিকেট অ্যালাউ করে?নানা
ইনসারশন অর্ডার রক্ষা করে?না (Unpredictable)হ্যাঁ
পারফরম্যান্সকিছুটা দ্রুতHashSet-এর চেয়ে একটু ধীর