What is the need of collection framework in java?

2020-06-18 09:09发布

What is the need of Collection framework in Java since all the data operations(sorting/adding/deleting) are possible with Arrays and moreover array is suitable for memory consumption and performance is also better compared with Collections.

Can anyone point me a real time data oriented example which shows the difference in both(array/Collections) of these implementations.

10条回答
smile是对你的礼貌
2楼-- · 2020-06-18 10:01

Collection framework are much higher level compared to Arrays and provides important interfaces and classes that by using them we can manage groups of objects with a much sophisticated way with many methods already given by the specific collection.

For example:

  • ArrayList - It's like a dynamic array i.e. we don't need to declare its size, it grows as we add elements to it and it shrinks as we remove elements from it, during the runtime of the program.
  • LinkedList - It can be used to depict a Queue(FIFO) or even a Stack(LIFO).
  • HashSet - It stores its element by a process called hashing. The order of elements in HashSet is not guaranteed.
  • TreeSet - TreeSet is the best candidate when one needs to store a large number of sorted elements and their fast access.
  • ArrayDeque - It can also be used to implement a first-in, first-out(FIFO) queue or a last-in, first-out(LIFO) queue.
  • HashMap - HashMap stores the data in the form of key-value pairs, where key and value are objects.
  • Treemap - TreeMap stores key-value pairs in a sorted ascending order and retrieval speed of an element out of a TreeMap is quite fast.

To learn more about Java collections, check out this article.

查看更多
够拽才男人
3楼-- · 2020-06-18 10:03

For each class in the Collections API there's a different answer to your question. Here are a few examples.

LinkedList: If you remove an element from the middle of an array, you pay the cost of moving all of the elements to the right of the removed element. Not so with a linked list.

Set: If you try to implement a set with an array, adding an element or testing for an element's presence is O(N). With a HashSet, it's O(1).

Map: To implement a map using an array would give the same performance characteristics as your putative array implementation of a set.

查看更多
贪生不怕死
4楼-- · 2020-06-18 10:04

Several reasons:

  • Java's collection classes provides a higher level interface than arrays.
  • Arrays have a fixed size. Collections (see ArrayList) have a flexible size.
  • Efficiently implementing a complicated data structures (e.g., hash tables) on top of raw arrays is a demanding task. The standard HashMap gives you that for free.
  • There are different implementation you can choose from for the same set of services: ArrayList vs. LinkedList, HashMap vs. TreeMap, synchronized, etc.
  • Finally, arrays allow covariance: setting an element of an array is not guaranteed to succeed due to typing errors that are detectable only at run time. Generics prevent this problem in arrays.

Take a look at this fragment that illustrates the covariance problem:

  String[] strings = new String[10];
  Object[] objects = strings;

  objects[0] = new Date();  // <- ArrayStoreException: java.util.Date
查看更多
闹够了就滚
5楼-- · 2020-06-18 10:05

Arrays are not efficient always. What if you need something like LinkedList? Looks like you need to learn some data structure : http://en.wikipedia.org/wiki/List_of_data_structures

查看更多
登录 后发表回答