java基础学习:collection

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);

    }

}

 


猜你喜欢

转载自blog.csdn.net/leftmumu/article/details/79486465