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.

6
  • 5
    japan is repeated again :) Commented Jun 7, 2013 at 9:17
  • HashSet would be better option Commented Jun 7, 2013 at 9:17
  • @PankajKumar Yes you're right. Thanks for pointing it out. I removed my answer and instead voted to close as duplicate. Commented Jun 7, 2013 at 9:38
  • 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. Commented Jun 7, 2013 at 10:25
  • There must be some mismatch in the index. Check out. Commented Jun 7, 2013 at 10:29

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Or rather a Set such as HashSet. The data does not require key-value pairing.
@laalto y ur right ;) i just wake up .. still sleeping i guess
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.

Comments