Collection Interface VS Arrays

collection vs list vs arrays as return type for EJB method

  • Prefer collections over arrays; Use generics
  • Use interfaces instead of concrete classes

Then you usually have 4 options: List, Set, Collection and Iterable. There it depends what is the semantics you want to include.

  • If it is an internal API - decide it based on the characteristics of the collection:
    • does it only hold unique items? Set
    • will clients need random access to it? List
    • will clients need to modify it (add, remove) (without the above two characteristics)? Collection
    • will clients just need to iterate it? Iterable
  • If it's a web service it doesn't matter - it is serialized the same way.

(Note: there are some collection interfaces with more specific semantics: Queue, Deque, Map, Bag, Multiset, etc. - but it will be fairly obvious when you need to return them)

What are the major differences between a Collection, an ArrayList, and a List in Java?

A Collection is an interface that defines the highest-level of shared collection behavior, and extends Iterable (which just defines the iterator() method).

A List is an interface that defines the highest-level of shared List behavior.

ArrayList is an implementation of List and in general wouldn't be used in a declaration unless you need an implementation guarantee (e.g., fast indexed access), but is fine to use as a list value.

Read the docs to see the differences–they're described in the API. The implementation (ArrayList) will have a type-specific implementation of each method in each interface it implements.

In general, use the least-specific interface that provides what you need. For example, passing Collection instead of List allows a wider selection of implementations. If you need something like indexed access (e.g., List.get(int)), a Collection wouldn't work.

As with most things, which to use depends entirely upon your needs.

Difference between Collection and Arraylist in Java?

The Collections API is a set of classes and interfaces that support operations on collections of objects.

Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.

Whereas,
ArrayList: It is re-sizable array implementation. Belongs to 'List' group in collection. It permits all elements, including null. It is not thread -safe.

Collections: It implements Polymorphic algorithms which operate on collections.

Collection: It is the root interface in the collection hierarchy.

The following interfaces (collection types) extends the Collection interface:

  • List
  • Set
  • SortedSet
  • NavigableSet
  • Queue
  • Deque

Java does not come with a usable implementation of the Collection interface, so you will have to use one of the listed subtypes. The Collection interface just defines a set of methods (behaviour) that each of these Collection subtypes share. This makes it possible ignore what specific type of Collection you are using, and just treat it as a Collection. This is standard inheritance, so there is nothing magical about, but it can still be a nice feature from time to time.

Collections vs Arrays regarding sort()

Well, besides operating on different stuff (Collections.sort operates on a List, and Arrays.sort operates on an array), java.util.Collections.sort() simply calls java.util.Arrays.sort() to do the heavy lifting.

Also, for what it's worth, notice that Arrays.sort runs a merge sort.

Java: Difference Between a collection and 'Data Structure'

A data structure is how the data is represented inside the storage in memory. A collection is how it can be accessed. I stress on the word "can".

If you store data in a LinkedList and sort it, the performance will drop. The same algorithm if you use a ArrayList the performance will enhance. Just by changing the way its represented in memory will help various factors.

You "can" access it using a collection representation, you "can" also use the "index" to access the data. You "can" also go getFirst, getNext, getPrev.

Your confusion is between internal storage and accessing the storage. Separate the 2.



Related Topics



Leave a reply



Submit