एंड्रॉइड में TreeSet का उपयोग करने का तरीका
यह उदाहरण दर्शाता है कि एंड्रॉइड ऐप में TreeSet का उपयोग कैसे किया जाए। TreeSet एक सॉर्टेड सेट है जो डुप्लिकेट मानों को अनुमति नहीं देता और एलिमेंट्स को नैचुरल ऑर्डर में रखता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल संशोधित करें
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, सेव और रिफ्रेश बटन, और डेटा दिखाने के लिए ListView लिया है।
चरण 3: MainActivity.java में कोड जोड़ें
src/MainActivity.java में निम्न कोड जोड़ें:
package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.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.Set;
import java.util.TreeSet;
public class MainActivity extends AppCompatActivity {
EditText name;
ArrayAdapter arrayAdapter;
ArrayList array_list;
Set set = new TreeSet<>();
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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()) {
set.add(name.getText().toString());
array_list.clear();
array_list.addAll(set);
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 Studio में प्रोजेक्ट की एक्टिविटी फ़ाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें। नाम दर्ज करें और Save पर क्लिक करें - नाम सॉर्टेड ऑर्डर में ListView में जुड़ जाएगा। डुप्लिकेट नाम नहीं जुड़ेंगे। Refresh बटन से लिस्ट अपडेट करें।