如何删除ArrayList中的重复值?

时间:2018-09-19 08:31:27

标签: java android contacts

现在:

enter image description here

我想要:

enter image description here

代码:

String[] contact = new String[contact_phoneList.size()];
for (int i = 0; i < contact_phoneList.size(); i++) {
    contact [i] = contact_phoneList.get(i);
}

1 个答案:

答案 0 :(得分:2)

使用HashSet删除重复的值:

HashSet hs = new HashSet();
hs.addAll(contact_phoneList); // name of contact_phoneList from which you want to remove duplicates 
contact_phoneList.clear();
contact_phoneList.addAll(hs);

不需要for循环