Android में TreeSet का उपयोग करके ArrayList से डुप्लिकेट हटाना
यह उदाहरण दर्शाता है कि Android में TreeSet का उपयोग करके ArrayList से डुप्लिकेट कैसे हटाएं और ListView में दिखाएं।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट XML जोड़ें (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:hint="Enter Name"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/save"
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/refresh"
android:text="Refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
उपरोक्त कोड में, हमने नाम के लिए EditText लिया है। जब उपयोगकर्ता सेव बटन पर क्लिक करता है, तो यह डेटा को ArrayList में संग्रहीत करेगा। ListView के परिवर्तन प्राप्त करने के लिए रिफ्रेश बटन पर क्लिक करें।
चरण 3: MainActivity.java कोड जोड़ें (src/MainActivity.java)
package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.TreeSet;
public class MainActivity extends AppCompatActivity {
Button save, refresh;
EditText name;
ArrayAdapter arrayAdapter;
ArrayList<String> array_list;
private ListView listView;
@Override
protected void onCreate(Bundle readdInstanceState) {
super.onCreate(readdInstanceState);
setContentView(R.layout.activity_main);
array_list = new ArrayList<>();
name = findViewById(R.id.name);
listView = findViewById(R.id.listView);
findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayAdapter.notifyDataSetChanged();
listView.invalidateViews();
listView.refreshDrawableState();
}
});
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!name.getText().toString().isEmpty()) {
array_list.add(name.getText().toString());
TreeSet<String> hs = new TreeSet<String>();
hs.addAll(array_list);
array_list.clear();
array_list.addAll(hs);
arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, array_list);
listView.setAdapter(arrayAdapter);
Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_LONG).show();
} else {
name.setError("Enter NAME");
}
}
});
}
}
एप्लिकेशन चलाएं
अपने वास्तविक Android मोबाइल डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio से ऐप चलाने के लिए, अपनी प्रोजेक्ट की एक्टिविटी फ़ाइलों में से एक खोलें और टूलबार से Run आइकन पर क्लिक करें। अपने मोबाइल डिवाइस को एक विकल्प के रूप में चुनें और फिर अपने मोबाइल डिवाइस की जांच करें जो आपकी डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा -

उपरोक्त परिणाम में, हम ArrayList में समान नाम डाल रहे हैं। टोस्ट प्रदर्शित होता है लेकिन ArrayList में डाला नहीं जाता।