如何从ArrayList中Android中删除重复值 [英] How to remove Duplicate value from arraylist in Android

查看:396
本文介绍了如何从ArrayList中Android中删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ArrayList<String> values=new ArrayList<String>();
values.add("s");
values.add("n");
values.add("a");
values.add("s");

在此阵,我想删除重复的值。

In this Array, I want to remove repeated values.

推荐答案

如果你不想在集合重复,你应该考虑为什么你使用的集合,允许重复。删除重复的元素的最简单方法是将内容添加到一套(这将不允许重复),然后添加设回ArrayList的:

If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:

ArrayList al = new ArrayList();
// add elements to al, including duplicates
 HashSet hs = new HashSet();
hs.addAll(al);
al.clear();
al.addAll(hs);

最重要的是,这会破坏ArrayList中元素的顺序。

most importantly, this destroys the ordering of the elements in the ArrayList.

这篇关于如何从ArrayList中Android中删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆