import java.util.Iterator;编译并执行此代码会导致以下结果:
导入 java.util.TreeSet;
public class search_element {
public static void main(String[] args) {
// 创建 TreeSet
TreeSettset = new TreeSet ();
// 填充 TreeSet
tset.add(abc”);
tset.add(bcd”);
tset.add(cde”);
tset.add(def”);
tset.add(efg”);
字符串 e = def”;
// 检查 TreeSet
boolean exists = tset.contains(e);
System.out.println(e+ 是否存在于树集中?+存在);
字符串 e2 = eee”;
exists = tset.contains(e2);
System.out.println(e2+ 是否存在于树集中?+存在);
迭代器<字符串> 迭代器 = tset.iterator();
// 搜索元素并用 equals
while (iterator.hasNext()){
String obj = iterator.next();
if(obj.equals(e))
System.out.println(对象 '+e+' 存在于列表中”);
}
}
}
def 是否存在于 treeset 中?trueReferences:
eee 是否存在于树集中?false
def 对象存在于 list
Please disable your ad blocker and refresh the window to use this website.