Java basic learning: collection

Collection

1.collection concept

A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate and transmit aggregated data. Typically, they represent data items that form natural groups, such as playing cards (a collection of cards), mail folders (a collection of letters), or phone directories (a mapping of names to phone numbers). If you've used the Java programming language - or any other programming language - you're already familiar with these collections.

 

2.Collections Framework concept

       The Collections Framework is a unified architecture for representing and manipulating collections. All collection frames contain the following:

Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces usually form a hierarchy.

 

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

    }

}

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325874049&siteId=291194637