collections - basic concepts

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/86038686

The Java collection libray has two distinct concepts:

  • 1. Collection: a sequence of individual elements with one or more rules applied to them.  These are List, Set and Queue.
  • 2. Map: a group of key-value object pairs that looks up a value using a key. We look up a value object using a key object just like we look up a definition using a word.

for example:

// collections/SimpleCollection.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.*;

public class SimpleCollection {
  public static void main(String[] args) {
    Collection<Integer> c = new ArrayList<>();// <>() since Java 8
    for (int i = 0; i < 10; i++) {
      c.add(i); // Autoboxing
    }
    for (Integer i : c) {
      System.out.print(i + ", ");
    }
  }
}

My output:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
BUILD SUCCESSFUL
boolean add(E e) // Interface Collection method
Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

adding groups of elements:

// collections/AddingGroups.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Adding groups of elements to Collection objects

import java.util.*;

public class AddingGroups {
  public static void main(String[] args) {
    Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
    Integer[] moreInts = {6, 7, 8, 9, 10};
    collection.addAll(Arrays.asList(moreInts));
    // Runs significantly faster, but you can't
    // construct a Collection this way:
    Collections.addAll(collection, 11, 12, 13, 14, 15);
    Collections.addAll(collection, moreInts);
    // Produces a list "backed by" an array:
    List<Integer> list = Arrays.asList(16, 17, 18, 19, 20);
    list.set(1, 99); // OK -- modify an element // please note this
    // list.add(21); // Runtime error; the underlying [1]
    // array cannot be resized.
  }
}

[1]'s error is:

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:148)
        at java.util.AbstractList.add(AbstractList.java:108)
        at AddingGroups.main(AddingGroups.java:21)
:collections:AddingGroups FAILED

for more examples infomation see here.

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/SimpleCollection.java

3. https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html

4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/AddingGroups.java

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/86038686