2

I have one array have same name multiple times. so how to remove this, only i want display in one name : ArrayList name = new ArrayList();

name= india, japan, china, usa, china, japan, Australia, india, china

I want to display like this

name= india,japan china, usa, Australia

I am little bit confused , how to remove duplicate name. It means, i want to display unque name one at a time, not a multiple times same name in list.

Note:

I found the solution, but when i display on list, if first element japan display then i click then it is shows india in Toast, how to occur this? then others same. not accurate display on toast.

3 Answers3

8

couldnt you check before adding if !arraylist.contains(string) then arraylist.add(string); I think that would be best approach

For example:

if (!array.contains(value)) {
   array.add(value);
}
Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
0

Use a HashMap instead of an ArrayList. Any Element in a HashMap is unique.

LibettenRadler
  • 115
  • 2
  • 8
0

Using Hashset will be best options for this. Hashset doesnot require key value pair mechanism. or other option you try with contains() method of Arraylist but it required looping for checking the elements.

chaitanya
  • 1,726
  • 2
  • 17
  • 13