Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में HashSet का उपयोग कैसे करें?

एंड्रॉइड में 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

विशेषताHashSetLinkedHashSetTreeSet
क्रमकोई नहींइंसर्शन ऑर्डरसॉर्टेड (नेचुरल)
परफॉरमेंससबसे तेज़थोड़ा धीमासबसे धीमा (log n)
नल अनुमतहाँहाँनहीं
इटरेशन क्रमअनिश्चितइंसर्शन क्रमआरोही क्रम

ऐप चलाएं

नाम जोड़ें - ListView में रैंडम क्रम में दिखेंगे। डुप्लीकेट नाम जोड़ने पर इग्नोर होंगे।

एंड्रॉइड में HashSet का उपयोग कैसे करें?

  1. Android Toolbar में SearchView का उपयोग कैसे करें? Android Toolbar में SearchView का उपयोग कैसे करें?

    Android Toolbar/ActionBar में SearchView जोड़कर ListView फ़िल्टर करना टूलबार में सर्च आइकन जोड़कर ListView डेटा को रियल-टाइम फ़िल्टर करने का पूरा उदाहरण। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project। चरण 2: लेआउट में Toolbar और ListView res/layout/activity_main.xml: <?xml ve

  1. एंड्रॉइड में XMLPullParser से XML पार्स कैसे करें? एंड्रॉइड में XMLPullParser से XML पार्स कैसे करें?

    एंड्रॉइड में XMLPullParser का उपयोग करके XML डेटा पार्स करना XMLPullParser एंड्रॉइड में XML प्रोसेसिंग के लिए एक हल्का, कुशल और लो-मेमोरी पार्सर है। यह पुल मॉडल पर काम करता है — यानी आप पार्सर से अगला इवेंट मांगते हैं, बजाय इसके कि पार्सर आपको कॉलबैक दे (जैसे SAX)। यह बड़े XML फाइल्स के लिए आदर्श है

  1. एंड्रॉइड टीवी पर ADB सेट अप और उपयोग कैसे करें एंड्रॉइड टीवी पर ADB सेट अप और उपयोग कैसे करें

    चाहे आपके पास एंड्रॉइड स्मार्टफोन हो या एंड्रॉइड टीवी, ऑपरेटिंग सिस्टम की प्रकृति समान रहती है। दूसरे शब्दों में, एक एंड्रॉइड टीवी किसी भी अन्य एंड्रॉइड डिवाइस की तरह ही ट्वीक करने योग्य है। यदि आप कुछ गंभीर ट्वीक करने की योजना बना रहे हैं जैसे एंड्रॉइड ऐप साइडलोड करना, तो आपको एंड्रॉइड डिबग ब्रिज,