如果你知道hashSet去重功能的话,也许能解决你的问题;
写个简单的代码解决你的问题吧;
import java.util.HashSet;
import java.util.Set;
public class SetTest {
public static void main(String[] args) {
String [] test = {"a","sd","er","f","a","sd","sd","a","f","a"};
Set<String> set = new HashSet<String>();
for(String t : test){
set.add(t);
}
System.out.println("集合test个数:"+test.length);
System.out.println("去重后集合在还剩下数据个数:"+set.size());
}
}
如果追求顺序性,考虑用linkedHashSet 链表去重,去重原理还是构建二叉树数据结构,比较hashcode和equas ,只是二叉树数据结构在时间复杂度上更为优异·
|