java.util.Collection体系源码解读<二>Collection接口源码

Collection接口是绝大多数集合类的父接口.

1.Collection是java.util所有集合相关类的根接口

2.Collection表示一组对象,这些对象可以看做是Collection的元素

3.一些 Collection允许有重复的元素(如List),而另一些则不允许(如Set),一些 Collection是有序的(如List),而另一些则是无序的(如Set)

4.由public interface Collection<E> extends Iterable<E>{}我们可以看到Collection接口是继承了Iterable接口的一些特性的,所以我们先来看看Iterable接口相关的源码:

java.util.Iterable源码:

public interface Iterable<T> {
//该接口包含一个泛型迭代器,用来迭代Collection的元素
Iterator<T> iterator();
}

java.util.Iterator源码:

package java.util;
public interface Iterator<E> {
    //是否有下一个元素可以迭代
    boolean hasNext();
    //返回下一个迭代的元素
    E next();
    //这里可以变迭代边删除,但是在一般的for循环遍历中,不可以边遍历边删除
    void remove();
}

6.下面我们来看Collection源码:


package java.util;

public interface Collection<E> extends Iterable<E> {
   
    /**返回此 collection 中的元素数。注意:如果此 collection 包含的元素大于 Integer.MAX_VALUE,则返回 Integer.MAX_VALUE*/
    int size();

    /**判断collection是否有元素*/
    boolean isEmpty();

    /**collection是否包含某个元素.注意:当且仅当此 collection 至少包含一个满足 (o==null ? e==null : o.equals(e)) 的元素 e 时,返回 true
        ,可见,包含null的前提是此collection中也必须包含一个null
    */
    boolean contains(Object o);

    /**返回在此 collection 的元素上进行迭代的迭代器。
        注意:如果collection的实例中元素是无序的,则素返回的顺序没有任何保证
     */
    Iterator<E> iterator();

    /**返回包含此 collection 中所有元素的数组.
       注意:1.此数组是new出来的,并不维持对原有数据的引用
            2.如果元素的迭代器保证了元素是有序的,则返回的数组中的对象也是有序的
    */
    Object[] toArray();

     /**将collection元素装入指定数组并返回
        注意:1.若a能容纳所有元素,则返回包含此 collection 元素的数组
             2.若a能不能容纳所有元素, 则会残生一个新数组,用来a和此collection的所有元素
     */
    <T> T[] toArray(T[] a);

    /**添加元素,如果此 collection 由于添加而发生更改,则返回 true。
        注意:1.如果此 collection 不允许有重复元素,并且已经包含了指定的元素,则返回 false。
             2.一些 collection 拒绝添加 null 元素(如),个人觉得最好所有的collectio集合拒绝添加null元素
             3.如果 collection 由于某些原因(已经包含该元素的原因除外)拒绝添加特定的元素,那么它必须 抛出一个异常(而不是返回 false)
    */
    boolean add(E e);

    /**从此 collection 中移除指定元素,如果此 collection 包含一个或多个满足 (o==null ? e==null : o.equals(e)) 的元素 e,则移除这样的元素。
       如果移除成功,或者此 collection 由于调用而发生更改,则返回 true
       注意:抛出的异常.ClassCastException.NullPointerException.UnsupportedOperationException
    */
    boolean remove(Object o);

  
    /**是否包含某个集合里面的所有元素,如果此 collection 包含指定 collection 中的所有元素,则返回 true*/
    boolean containsAll(Collection<?> c);

    /**将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。
        如果在添加的过程中时同时有某个进程修改指定的 非空collection,那么这个方法不一定能确保执行成功(一般不建议这么做)
     */
    boolean addAll(Collection<? extends E> c);

    /**移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
    此调用返回后,collection 中将不包含任何与指定 collection 相同的元素*/
    boolean removeAll(Collection<?> c);

    /**保留此 collection 中那些也包含在指定 collection 的元素
        换句话说,移除此 collection 中未包含在指定 collection 中的所有元素。*/
    boolean retainAll(Collection<?> c);

    /**移除此 collection 中的所有元素*/
    void clear();

    /**比较此 collection 与指定对象是否相等
        jdk源注释文档中关于此方法说了许多,个人总结如下:
        1.除非自己需要实现值比较,而不是引用比较,否则不建议改写此方法
        3.比较时,该方法的相等必须是相等的,也就是说若q.equals(b)==true则b.equals(a)
        4.自己重写一个实现该接口的集合,但不是List或者Set的派生集合时,重写此方法一定要注意第一点,同时,对于list或者Set,只能是list和
        list相等,set和set相等,自己重写一个实现Collection的集合类时,如果调用常规的equals方法,将该类与任何列表或 set 进行比较时,
        常规的 equals 方法必须返回 false.
        5.无法写出一个同时实现正确实现 Set 和 List 接口的类
    */
    boolean equals(Object o);

    /**返回此 collection 的哈希码值。
        注意:1.当 Collection 接口没有为 Object.hashCode 方法的常规协定添加任何约束时,为了满足 Object.hashCode 方法的常规协定,
        程序员应该注意任何重写 Object.equals 方法的类必须重写 Object.hashCode 方法。
        需要特别指出的是,c1.equals(c2) 暗示着 c1.hashCode()==c2.hashCode()。 */
    int hashCode();
}



好的,根据您的要求,我来给您提供一份相应的 Java 代码。请注意,为了让代码能够正常运行,您需要在 Point3d 类中实现 equals() 和 hashCode() 方法。同时,由于我无法获取您的完整代码,以下代码仅供参考,您可能需要对其进行适当修改才能让其在您的项目中运行。 ```java import java.util.HashSet; import java.util.Iterator; import java.util.Random; import java.util.Set; import java.util.TreeSet; public class Main { public static void main(String[] args) { // 创建 HashSet 集合对象 Set<Point3d> hashSet = new HashSet<>(); // 循环添加随机点 Random random = new Random(); for (int i = 0; i < 1000; i++) { int x = random.nextInt(5); int y = random.nextInt(5); int z = random.nextInt(5); Point3d point = new Point3d(x, y, z); hashSet.add(point); } // 显示集合的维度和是否含指定点 System.out.println("Hash Set Dimension: " + hashSet.size()); Point3d targetPoint = new Point3d(0, 0, 0); System.out.println("Hash Set Contains Target Point: " + hashSet.contains(targetPoint)); // 使用 Iterator 遍历集合 Iterator<Point3d> iterator = hashSet.iterator(); System.out.println("Hash Set Elements:"); while (iterator.hasNext()) { System.out.println(iterator.next()); } // 创建 TreeSet 集合对象 Set<Point3d> treeSet = new TreeSet<>(hashSet); // 显示集合的维度 System.out.println("Tree Set Dimension: " + treeSet.size()); // 使用 Iterator 遍历集合 iterator = treeSet.iterator(); System.out.println("Tree Set Elements:"); while (iterator.hasNext()) { System.out.println(iterator.next()); } // 删除三个坐标都匹配的所有点 removePoints(treeSet); } public static void removePoints(Set<Point3d> set) { Set<Point3d> toRemove = new HashSet<>(); for (Point3d point : set) { if (point.x == point.y && point.y == point.z) { toRemove.add(point); } } set.removeAll(toRemove); } } class Point3d implements Comparable<Point3d> { int x; int y; int z; public Point3d(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } @Override public boolean equals(Object obj) { if (obj instanceof Point3d) { Point3d other = (Point3d) obj; return this.x == other.x && this.y == other.y && this.z == other.z; } return false; } @Override public int hashCode() { int result = 17; result = 31 * result + x; result = 31 * result + y; result = 31 * result + z; return result; } @Override public int compareTo(Point3d other) { double distance1 = Math.sqrt(x * x + y * y + z * z); double distance2 = Math.sqrt(other.x * other.x + other.y * other.y + other.z * other.z); return Double.compare(distance1, distance2); } @Override public String toString() { return "(" + x + ", " + y + ", " + z + ")"; } } ``` 在以上代码中,我们首先创建了一个 HashSet 集合对象,并循环添加了 1000 个随机点。然后,我们使用 size() 方法和 contains() 方法分别显示了集合的维度和是否含指定点。接着,我们使用 Iterator 遍历了集合,并将其中的元素输出到控制台。 接下来,我们使用 TreeSet 构造函数创建了一个 TreeSet 集合对象,并将 HashSet 集合对象作为参数传递给它。这样做可以将 HashSet 中的元素自动按照指定的比较规则排序,并将排序后的元素添加到 TreeSet 中。我们同样使用 size() 方法和 Iterator 遍历了 TreeSet 集合,并将其中的元素输出到控制台。 最后,我们编写了一个名为 removePoints() 的方法,该方法接受一个点集合作为参数,并删除其中三个坐标都匹配的所有点。我们使用 HashSet 集合来存储需要删除的点,然后使用 removeAll() 方法从原集合中删除这些点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值