Remove Duplicate Entries Data From ListView in Android

How to remove repeated elements from arraylist in android array adapter using HashSet.

In this tutorial we would going to delete all the same listview data because repeated data makes your listview more complex. So here is the complete step by step tutorial for Remove Duplicate Entries Data From ListView in Android.

How to Remove Duplicate Entries Data From ListView in Android.

Code for MainActivity.java file.

package com.android_examples.removerepeateddata_android_examplescom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ListView listview;
    List<String> subject_list;
    ArrayAdapter<String> arrayadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView)findViewById(R.id.listView1);

        subject_list = new ArrayList<String>();

        subject_list.add("ONE");
        subject_list.add("TWO");
        subject_list.add("THREE");
        subject_list.add("FOUR");
        subject_list.add("FIVE");
        subject_list.add("SIX");
        subject_list.add("SEVEN");
        subject_list.add("EIGHT");
        subject_list.add("NINE");
        subject_list.add("TEN");
        subject_list.add("THREE");
        subject_list.add("FOUR");
        subject_list.add("FIVE");
        subject_list.add("THREE");
        subject_list.add("FOUR");
        subject_list.add("FIVE");
        subject_list.add("THREE");
        subject_list.add("FOUR");
        subject_list.add("FIVE");

        //Deleting Same entries

        HashSet<String> hashSet = new HashSet<String>();
        hashSet.addAll(subject_list);
        subject_list.clear();
        subject_list.addAll(hashSet);

        //Alphabetic sorting.
        Collections.sort(subject_list);

        arrayadapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, subject_list);

        listview.setAdapter(arrayadapter);

    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.removerepeateddata_android_examplescom.MainActivity">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

Screenshot:

Remove Duplicate Entries

One Comment

  1. Great site. Just had a quick read.

Leave a Reply

Your email address will not be published. Required fields are marked *