package ch5;import java.util.*;/*** 2. Set集合及其子类方法示例,需要注意Set无重复元素* 需判断equals和hashcode方法,两个方法均相同时认为两个元素相同* 此外,Iterator接口进行演示:iterator接口有三个方法,next、hasnext和remove;* Iterator是线程安全的,需要注意使用Iterator的对象进行遍历时,不能使用其集合类的方法。*/public class TestCollection2 {public static void main(String[] args) {Collection c = new HashSet(); //Collection换成Set试试c.add(new Name("f4", "l4"));c.add(new Name("f2", "l2"));c.add(new Name("f3", "l3"));c.add(new Name("f4", "l4"));//相同元素不会被加入System.out.println(c.size());System.out.println(c);/*Iterator i = c.iterator();while(i.hasNext()) {Name n = (Name)i.next();System.out.println(n.getFirstName() + " ");if(n.getFirstName() == "f2") {i.remove(); // 只能用Iterator的remove方法,执行了锁定//不能用c.remove(n);会抛出异常}}System.out.println(c);int [] arr = {1,2,3,4,5};for(int v : arr) {System.out.println(v);}for(Object o: c) {System.out.println(o); //好处:语法简便;缺点:1、不能访问下标指向的元素,2、不能删除特定元素}*/}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。