本文共 6264 字,大约阅读时间需要 20 分钟。
查看Java中Collection接口的源码,我们可以指定该接口的定义:
public interface Collectionextends Iterable { ... }
该接口中主要有以下方法,以下将通过该接口的实现类ArrayList的对象进行介绍说明Collection方法的使用
@Testpublic void testColl(){ Collection coll = new ArrayList(); // boolean add(E e); coll.add(123); coll.add('a'); coll.add("ylaihui"); // size() 返回元素的个数 System.out.println(coll.size()); // 3 Collection coll2 = new ArrayList(); coll2.add(new java.lang.String("ylaihui")); coll2.add(new java.lang.StringBuffer("ylaihui")); //addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中 coll.addAll(coll2); System.out.println(coll.size()); // 5 // 调用 ArrayList的 toString方法 [123, a, ylaihui, ylaihui, ylaihui] System.out.println(coll); // 判断集合是否为空 (集合中是否有元素) return size() == 0 Collection coll3 = new ArrayList(); System.out.println(coll3.isEmpty()); // true System.out.println(coll.isEmpty()); // false // 清空集合元素 coll.clear(); System.out.println(coll.isEmpty()); // true}
代码输出:
3
5
[123, a, ylaihui, ylaihui, ylaihui]
true
false
true
class Order { private int id; private double price; public Order() { } public Order(int id, double price) { this.id = id; this.price = price; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Order order = (Order) o; return id == order.id && Double.compare(order.price, price) == 0; } @Override public String toString() { return "Order{" + "id=" + id + ", price=" + price + '}'; }}
@Testpublic void testMethod() { Collection objects = new ArrayList(); objects.add(123); objects.add(false); objects.add(new String("ylaihui")); // contains(Object obj):判断当前集合中是否包含obj // 会调用obj对象所在类的equals(),并非比较的对象的地址,而是比较的对象的内容 System.out.println(objects.contains(123)); // 自动装箱 true System.out.println(objects.contains("ylaihui")); // true System.out.println(objects.contains(new String("ylaihui"))); // true Order order = new Order(111, 100.00); objects.add(order); // 不管有没有重写equals,结果都为true,因为是同一个对象 调用== 或 equals都是true System.out.println(objects.contains(order)); // true // true, 如果Order类没有重写equals方法,结果为 false System.out.println(objects.contains(new Order(111, 100.00))); // true Collection coll1 = Arrays.asList(123, false); // 判断 objects 集合中是否包含集合 coll1, 子集的关系 System.out.println(objects.containsAll(coll1)); // true // remove(Object obj):从当前集合中移除obj元素。 objects.remove(new Order(111, 100.00)); System.out.println(objects); // [123, false, ylaihui] //removeAll():移除两个部分的交集(coll2和objects的交集被移除) Collection coll2 = Arrays.asList(123, false, 1111); objects.removeAll(coll2); System.out.println(objects); // [ylaihui]}
@Testpublic void test3() { Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Order(1111, 100.00)); coll.add(new String("ylaihui")); coll.add(false); //retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合 Collection coll1 = Arrays.asList(123, 456, 789); coll.retainAll(coll1); System.out.println(coll); // [123, 456]}
@Testpublic void test4() { Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Order(1111, 100.00)); coll.add(new String("ylaihui")); coll.add(false); Collection coll2 = new ArrayList(); coll2.add(123); coll2.add(456); coll2.add(new Order(1111, 100.00)); coll2.add(new String("ylaihui")); coll2.add(false); //equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同。 System.out.println(coll.equals(coll2)); // true}
@Testpublic void test5() { Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Order(1111, 100.00)); coll.add(new String("ylaihui")); coll.add(false); //hashCode():返回当前对象的哈希值 System.out.println(coll.hashCode()); //集合 -> 数组:toArray() Object[] objects = coll.toArray(); for (int i = 0; i < objects.length; i++) { System.out.println(objects[i]); } //数组 -> 集合:调用Arrays类的静态方法asList(),基本数据类型的数组无法转换为多元素的集合 Collection coll2 = Arrays.asList(objects); System.out.println(coll2); // [[123, 456, Order{id=1111, price=100.0}, ylaihui, false]] System.out.println("coll2.size() = " + coll2.size()); // 5 Collection coll3 = Arrays.asList(new String[]{"y", "lai", "hui"}); System.out.println(coll3); // [y, lai, hui] System.out.println(coll3.size()); // 3 Collection coll4 = Arrays.asList(new int[]{1, 2, 3}); System.out.println(coll4); // [[I@22927a81] System.out.println(coll4.size()); // 1 Collection coll5 = Arrays.asList(new Integer[]{1, 2, 3}); System.out.println(coll5); // [1, 2, 3] System.out.println(coll5.size()); // 3}
调用 next() 时指针下移
每次调用 Iterator() 方法都会返回一个 Iterator 对象
System.out.println(iterator.getClass());
对于ArrayList集合的迭代器, 是其内部类的类型, class java.util.ArrayList$Itr,该内部类实现了 Iterator接口
@Testpublic void testIterator(){ Collection coll = new ArrayList(); coll.add(123); coll.add(234); coll.add("ylaihui"); Iterator iterator = coll.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } // 迭代器遍历结束后,需要重新初始化 否则报异常 NoSuchElementException // 可以这样实现,但是不推荐这么使用 iterator = coll.iterator(); for (int i = 0; i < coll.size(); i++) { System.out.println(iterator.next()); }}
如果还未调用next()或在上一次调用 next 方法之后已经调用了 remove 方法,
再调用remove都会报IllegalStateException。
public class IteratorRemove { @Test public void testremove(){ Collection coll = new ArrayList(); coll.add(111); coll.add(222); coll.add("ylaihui"); Iterator it = coll.iterator(); while(it.hasNext()){ Object next = it.next(); if(next.equals(111)){ it.remove(); } } it = coll.iterator(); while (it.hasNext()) System.out.println(it.next()); }}
转载地址:http://yxuhf.baihongyu.com/