开发者

What is a java collection? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answe开发者_开发知识库red in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

I want to know: What is a collection in Java?


Usually an instance of java.util.Collection (although java.util.Map is officially also a part of the collections framework)

Although the Collection interface can be implemented directly, usually client code will use an implementation of one of the sub interfaces: Set, List, Queue / Deque

Here is some sample code (on the left side you will usually see an interface and on the right side an implementation class).

Sets don't store duplicates, all of their elements are unique:

final Set<String> basicSet = new HashSet<String>();
basicSet.add("One");
basicSet.add("Two");
basicSet.add("One");
basicSet.add("Three");
System.out.println(basicSet.toString());
// Output: [Three, One, Two]
// (seemingly random order, no duplicates)

SortedSets are a special case of sets that store elements in a specified order:

final SortedSet<String> sortedSet = new TreeSet<String>();
sortedSet.add("One");
sortedSet.add("Two");
sortedSet.add("One");
sortedSet.add("Three");
System.out.println(sortedSet.toString());
// Output: [One, Three, Two]
// (natural order, no duplicates)

Lists let you store a value multiple times and access or modify insertion order:

final List<String> strings = new ArrayList<String>();
strings.add("Two");
strings.add("Three");
strings.add(0, "One"); // add item to beginning
strings.add(3, "One"); // add item at position 3 (zero-based)
strings.add("Three");
strings.add(strings.size() - 1, "Two"); // add item at last-but-one position
System.out.println(strings);
// Output: [One, Two, Three, One, Two, Three]

There is also a practical shorthand for defining a list:

List<String> strings = Arrays.asList("One", "Two", "Three");
// this returns a different kind of list but you usually don't need to know that

etc.

To get a better understanding, read The Collections Trail from the Sun Java Tutorial (online), or Java Generics and Collections by Maurice Naftalin and Philip Wadler


I think this question is best answered in a non-programming sense.

Say you have 5 balls, and you want to move them around easily. You get a bag and place the 5 balls inside of it. The bag acts as a container. You can now move this bag around, and so quite easily the 5 balls move with it.

Simply put, your holding zero or more objects, inside another object for easy retrieval.


Collection is an interface in the Java API, and according to the docs it is...

The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.

Common examples of collections are: ArrayList, HashSet, LinkedList, Stack and Vector.


It's a class that implements java.util.Collection interface.

There's another branch for those that implement java.util.Map.

These are the basis for data structures in Java: List, Set, LinkedList, HashMap, TreeMap, etc.


Quoting Java API "A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜