</pre><pre name="code" class="java">
public class TreeSetTest {
public static void main(String[] args) {
TreeSet<String> tr = new TreeSet<String>();
String[] s = { "11", "22", "22", "33", "33", "33" };
System.out.print("====处理前=======");
for (int i = 0; i < s.length; i++) {
System.out.print(s[i] + " ");
tr.add(s[i]);
}
String[] s2 = new String[tr.size()];
System.out.println("=====处理后======");
for (int i = 0; i < s2.length; i++) {
s2[i] = tr.pollFirst();// 从TreeSet中取出元素重新赋给数组 //tr.pollFirst()这一步执行后 tr.size 为空 应该是清空了 tr
System.out.print(s2[i] + " ");
}
if(tr.contains("11")){
System.out.print(11);//这里不会打印输出 contans不能这么用
//tr.pollFirst()这一步执行后 tr.size 为空 应该是清空了 tr
}
}
TreeSet去重字符串
最新推荐文章于 2022-04-24 19:25:46 发布