【发布时间】:2019-09-01 15:51:21
【问题描述】:
我接受一个手机号码数组,我需要删除数组中的重复号码。我选择的方式是将数组转换为哈希集,然后将其转换回数组。有没有更好的办法?
我的代码如下:
public static void main (String[] args) {
String[] mobiles = new String[]{"1", "2", "3", "1", "1"};
Set<String> data = new HashSet<>(Arrays.asList(mobiles));
String[] result = data.toArray(new String[0]);
for (String s : result) {
System.out.println(s);
}
}
【问题讨论】:
-
Arrays.stream(mobiles).distinct().toArray(String[]::new) -
@shmosel 牛逼
-
欢迎来到 Stack Overflow。您是否注意到您应该在发布问题之前进行搜索和研究?这个话题已经讨论过很多次了。 I downvoted because research must be done to ask a good question。也见How do I ask a good question?。
标签: java