使用HashSet过滤重复字符

来源:互联网 发布:阿里域名 编辑:程序博客网 时间:2024/05/29 16:41
转自:

http://www.cnblogs.com/bluedream2009/archive/2009/10/26/1590285.html

Set集合会将重复的键值过滤掉.利用这一特性.可以将数组里相同的字符串过滤掉.

public static void main(String[] args) {        String[] str = {"a", "b", "c", "b", "c", "d","a"};        Set<String> set = new HashSet<String>();        // 将Collection追加到Set中        set.addAll(Arrays.asList(str));        // 将Set转储到一个新分配的数组        String[] result = set.toArray(new String[0]);        for(String str2: result){            System.out.print("==>" + str2);        }    }

结果:

==>d==>a==>c==>b

0 0
原创粉丝点击