java基础学习:collection

本文介绍了集合的概念及其在Java编程语言中的应用。集合是一种用于存储、检索、操作和传输聚合数据的对象。文章还详细阐述了集合框架的基本组成部分,包括用于表示集合的接口以及如何通过迭代器安全地修改集合。

Collection

1.collection概念

一个集合(有时称为容器)只是一个将多个元素分组到一个单元中的对象。集合用于存储,检索,操作和传输聚合数据。典型地,它们表示形成自然组的数据项目,例如扑克牌(卡片集合),邮件文件夹(字母集合)或电话目录(名称到电话号码的映射)。如果您已经使用了Java编程语言 - 或者其他任何编程语言 - 则您已经熟悉了这些集合。

 

2.Collections Framework概念

       集合框架是用于表示和操作集合的统一体系结构。所有的集合框架包含以下内容:

接口:这些是表示集合的抽象数据类型。接口允许集合独立于其表示的细节被操纵。在面向对象的语言中,接口通常形成一个层次结构。

 

Note that Iterator.remove isthe only safe way to modify a collection during iteration; thebehavior is unspecified if the underlying collection is modified in any otherway while the iteration is in progress.

Use Iterator instead ofthe for-each construct when you need to:

  • Remove the current element. The for-each construct hides the iterator, so you cannot call remove. Therefore, the for-each construct is not usable for filtering.
  • Iterate over multiple collections in parallel.


staticvoid filter(Collection<?> c) {

    for (Iterator<?> it = c.iterator();it.hasNext(); )

        if (!cond(it.next()))

            it.remove();

}


importjava.util.*;

 

publicclass FindDups2 {

    public static void main(String[] args) {

        Set<String> uniques = newHashSet<String>();

        Set<String> dups    = new HashSet<String>();

 

        for (String a : args)

            if (!uniques.add(a))

                dups.add(a);

 

        // Destructive set-difference

        uniques.removeAll(dups);

 

        System.out.println("Uniquewords:    " + uniques);

        System.out.println("Duplicatewords: " + dups);

    }

}

 


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值