JAVA中Colllection的基本功能

本文深入探讨了Java集合框架中Collection接口的基本操作方法,包括add、remove、clear、contains及isEmpty和size方法的使用,通过具体代码示例展示了List和Set在处理数据时的区别。

Collection中的add方法:

代码:

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Collection c=new ArrayList();//父类引用指向子类对象。这里是一个List的集合。
        boolean b1=c.add(new Student("zz",15));
        boolean b2=c.add(new Student("zz",15));
        System.out.println(b1);
        System.out.println(b2);
        
        System.out.println(c);
        

    }

运行结果:

true
true
[Student [name=zz, age=15], Student [name=zz, age=15]]

 

需要注意的是,在调用Collection的add方法时,List允许加入多条相同数据,无论你加入什么,返回值一定为true.而Set不同,它不允许集合中存在相同数据,如果存在相同数据就会返回flase。ArryList的父类的父类,重写了toString 方法,输出对象不是Object类中的toStrng方法。

Collection 中的remove方法:

代码:    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Collection c=new ArrayList();
        c.add("a");
        c.add("b");
        c.add("c");
        c.add("d");
        c.remove("b");
        System.out.println(c);

    }

运行结果:

[a, c, d]

 

很显然,调用Collection中的remove方法能够删除指定元素。

 

Collection中的clear方法。

代码:    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Collection c=new ArrayList();
        c.add("a");
        c.add("b");
        c.add("c");
        c.add("d");
//        c.remove("b");
        c.clear();
        System.out.println(c);

    }
运行结果:
[]
需要注意的是,清空了集合,打印的方法中默认返回的就是"[]"。

Collection中的contains方法。

代码:

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Collection c=new ArrayList();
        c.add("a");
        c.add("b");
        c.add("c");
        c.add("d");
//        c.remove("b");
//        c.clear();
        System.out.println(c.contains("a"));
        System.out.println(c.contains("z"));
        System.out.println(c);

    }

运行结果:

true
false
[a, b, c, d]

调用contains方法判断集合中是否包含,包含为true,不包含为false。

Collection中判断是否为空,isEmpty()。

Collection中查看元素个数,size()。这里就不一一举例。

 

转载于:https://www.cnblogs.com/lovelyYakir/p/5561522.html

### Collection接口常用方法详解 Java 中的 `Collection` 接口是集合框架的顶级接口之一,它定义了所有集合类共有的基本操作。这些方法不仅适用于 `List` 和 `Set` 接口的实现类(如 `ArrayList`、`LinkedList`、`HashSet` 和 `TreeSet`),而且为集合类提供了一致的操作方式。以下是 `Collection` 接口中一些常用的方法及其详细说明。 #### 添加元素 - `boolean add(E e)`:将指定的元素添加到集合中。如果集合因为此操作而发生变化,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(); boolean result = collection.add("Hello"); System.out.println(result); // 输出 true ``` - `boolean addAll(Collection<? extends E> c)`:将指定集合中的所有元素添加到当前集合中。如果当前集合因为此操作而发生变化,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(); Collection<String> anotherCollection = Arrays.asList("World", "!"); boolean result = collection.addAll(anotherCollection); System.out.println(result); // 输出 true ``` #### 删除元素 - `boolean remove(Object o)`:从集合中移除一个与指定对象相等的元素(如果存在)。如果集合包含该元素并成功移除,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); boolean result = collection.remove("banana"); System.out.println(result); // 输出 true ``` - `boolean removeAll(Collection<?> c)`:从当前集合中移除所有与指定集合中的元素相等的元素。如果当前集合因为此操作而发生变化,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Collection<String> toRemove = Arrays.asList("banana", "grape"); boolean result = collection.removeAll(toRemove); System.out.println(result); // 输出 true ``` - `boolean retainAll(Collection<?> c)`:仅保留当前集合中那些也包含在指定集合中的元素。换句话说,移除当前集合中不在指定集合中的所有元素。如果当前集合因为此操作而发生变化,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Collection<String> toRetain = Arrays.asList("banana", "cherry", "date"); boolean result = collection.retainAll(toRetain); System.out.println(result); // 输出 true ``` #### 查询操作 - `int size()`:返回集合中的元素数量。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); int size = collection.size(); System.out.println(size); // 输出 3 ``` - `boolean isEmpty()`:判断集合是否为空。如果集合中没有元素,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(); boolean empty = collection.isEmpty(); System.out.println(empty); // 输出 true ``` - `boolean contains(Object o)`:检查集合是否包含指定的元素。如果包含,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); boolean contains = collection.contains("banana"); System.out.println(contains); // 输出 true ``` - `boolean containsAll(Collection<?> c)`:检查当前集合是否包含指定集合中的所有元素。如果当前集合包含指定集合中的所有元素,则返回 `true`;否则返回 `false`。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Collection<String> toCheck = Arrays.asList("banana", "cherry"); boolean containsAll = collection.containsAll(toCheck); System.out.println(containsAll); // 输出 true ``` #### 其他操作 - `Object[] toArray()`:返回一个包含集合中所有元素的数组。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Object[] array = collection.toArray(); System.out.println(Arrays.toString(array)); // 输出 [apple, banana, cherry] ``` - `<T> T[] toArray(T[] a)`:返回一个包含集合中所有元素的数组,数组的类型与指定数组相同。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); String[] array = collection.toArray(new String[0]); System.out.println(Arrays.toString(array)); // 输出 [apple, banana, cherry] ``` - `Iterator<E> iterator()`:返回一个迭代器,用于遍历集合中的元素。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Iterator<String> iterator = collection.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } // 输出: // apple // banana // cherry ``` #### 集合比较 - `boolean equals(Object o)`:比较当前集合与另一个对象是否相等。两个集合相等当且仅当它们包含相同的元素。 ```java Collection<String> collection1 = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); Collection<String> collection2 = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); boolean equals = collection1.equals(collection2); System.out.println(equals); // 输出 true ``` - `int hashCode()`:返回集合的哈希码值。集合的哈希码是其所有元素的哈希码之和。 ```java Collection<String> collection = new ArrayList<>(Arrays.asList("apple", "banana", "cherry")); int hashCode = collection.hashCode(); System.out.println(hashCode); // 输出某个整数值 ``` 这些方法构成了 `Collection` 接口的核心功能,为集合类提供了统一的操作接口。通过这些方法,可以方便地进行元素的添加、删除、查询和其他操作。[^3]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值