एंड्रॉइड में HashSet का उपयोग करना
यह उदाहरण दर्शाता है कि HashSet (तेज़, अनऑर्डर्ड, कोई डुप्लीकेट नहीं) का उपयोग करके डेटा कैसे स्टोर करें और ListView में दिखाएं।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: 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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="नाम दर्ज करें" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="सेव करें" />
<Button
android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="रिफ्रेश" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
चरण 3: MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private EditText nameEditText;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> arrayList;
private Set<String> hashSet = new HashSet<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.name);
listView = findViewById(R.id.listView);
arrayList = new ArrayList<>();
// रिफ्रेश बटन
findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (arrayAdapter != null) {
arrayAdapter.notifyDataSetChanged();
}
}
});
// सेव बटन
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString().trim();
if (!name.isEmpty()) {
boolean added = hashSet.add(name);
updateListView();
Toast.makeText(MainActivity.this,
added ? "नया जोड़ा गया" : "पहले से मौजूद (डुप्लीकेट)",
Toast.LENGTH_SHORT).show();
} else {
nameEditText.setError("नाम दर्ज करें");
}
}
});
}
private void updateListView() {
arrayList.clear();
arrayList.addAll(hashSet); // क्रम अनिश्चित (HashSet अनऑर्डर्ड है)
if (arrayAdapter == null) {
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
} else {
arrayAdapter.notifyDataSetChanged();
}
}
}
HashSet की विशेषताएं
- सबसे तेज़ Set इम्प्लीमेंटेशन - HashMap पर आधारित
- कोई क्रम नहीं - इंसर्शन ऑर्डर, सॉर्टेड ऑर्डर कुछ भी नहीं
- कोई डुप्लीकेट नहीं - equals() और hashCode() के आधार पर
- नल अनुमत - एक null एलिमेंट स्टोर कर सकते हैं
- थ्रेड-सेफ नहीं - मल्टीथ्रेड के लिए
Collections.synchronizedSet()उपयोग करें
HashSet vs LinkedHashSet vs TreeSet
| विशेषता | HashSet | LinkedHashSet | TreeSet |
|---|---|---|---|
| क्रम | कोई नहीं | इंसर्शन ऑर्डर | सॉर्टेड (नेचुरल) |
| परफॉरमेंस | सबसे तेज़ | थोड़ा धीमा | सबसे धीमा (log n) |
| नल अनुमत | हाँ | हाँ | नहीं |
| इटरेशन क्रम | अनिश्चित | इंसर्शन क्रम | आरोही क्रम |
ऐप चलाएं
नाम जोड़ें - ListView में रैंडम क्रम में दिखेंगे। डुप्लीकेट नाम जोड़ने पर इग्नोर होंगे।
