如何从String数组中查找重复的字符串 [英] How to find duplicate string from an Array of String

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

问题描述

我有一个字符串数组,它包含字符串列表。我想弄清楚这个列表中是否有任何重复的条目。基本上我有一个用户列表,应该没有重复的条目。

I have an array of string, which holds the list of strings. I want to figure out is there any duplicate entries in this list. Basically i have a list of users, and there should be no duplicate entries.

推荐答案

你可以将String数组添加到HashSet

you can add the String array to the HashSet

Set<String> h = new HashSet<String>(Arrays.asList(new String[] { "a", "b" }));

这将获得唯一的字符串值。如果需要,将HashSet转换回数组

this will get you unique String values. If necessary convert the HashSet back to array

String[] uniqueValues = h.toArray(new String[0]);

这篇关于如何从String数组中查找重复的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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