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条回答
做自己的国王
2楼-- · 2020-06-18 09:45

Collection classes like Set, List, and Map implementations are closer to the "problem space." They allow developers to complete work more quickly and turn in more readable/maintainable code.

查看更多
疯言疯语
3楼-- · 2020-06-18 09:45

Well the basic premise is "wrong" since Java included the Dictionary class since before interfaces existed in the language...

collections offer Lists which are somewhat similar to arrays, but they offer many more things that are not. I'll assume you were just talking about List (and even Set) and leave Map out of it.

Yes, it is possible to get the same functionality as List and Set with an array, however there is a lot of work involved. The whole point of a library is that users do not have to "roll their own" implementations of common things.

Once you have a single implementation that everyone uses it is easier to justify spending resources optimizing it as well. That means when the standard collections are sped up or have their memory footprint reduced that all applications using them get the improvements for free.

A single interface for each thing also simplifies every developers learning curve - there are not umpteen different ways of doing the same thing.

If you wanted to have an array that grows over time you would probably not put the growth code all over your classes, but would instead write a single utility method to do that. Same for deletion and insertion etc...

Also, arrays are not well suited to insertion/deletion, especially when you expect that the .length member is supposed to reflect the actual number of contents, so you would spend a huge amount of time growing and shrinking the array. Arrays are also not well suited for Sets as you would have to iterate over the entire array each time you wanted to do an insertion to check for duplicates. That would kill any perceived efficiency.

查看更多
虎瘦雄心在
4楼-- · 2020-06-18 09:46

Java Collections came up with different functionality,usability and convenience.

When in an application we want to work on group of Objects, Only ARRAY can not help us,Or rather they might leads to do things with some cumbersome operations.

One important difference, is one of usability and convenience, especially given that Collections automatically expand in size when needed:

Collections came up with methods to simplify our work.

Each one has a unique feature:

  • List- Essentially a variable-size array;
    You can usually add/remove items at any arbitrary position;
    The order of the items is well defined (i.e. you can say what position a given item goes in in the list).

    Used- Most cases where you just need to store or iterate through a "bunch of things" and later iterate through them.

  • Set- Things can be "there or not"— when you add items to a set, there's no notion of how many times the item was added, and usually no notion of ordering.

    Used- Remembering "which items you've already processed", e.g. when doing a web crawl;
    Making other yes-no decisions about an item, e.g. "is the item a word of English", "is the item in the database?" , "is the item in this category?" etc.

Here you find use of each collection as per scenario:

查看更多
相关推荐>>
5楼-- · 2020-06-18 09:50

It depends upon your application's needs. There are so many types of collections, including:

  • HashSet
  • ArrayList
  • HashMap
  • TreeSet
  • TreeMap
  • LinkedList

So for example, if you need to store key/value pairs, you will have to write a lot of custom code if it will be based off an array - whereas the Hash* collections should just work out of the box. As always, pick the right tool for the job.

查看更多
smile是对你的礼貌
6楼-- · 2020-06-18 09:50

Collection is the framework in Java and you know that framework is very easy to use rather than implementing and then use it and your concern is that why we don't use the array there are drawbacks of array like it is static you have to define the size of row at least in beginning, so if your array is large then it would result primarily in wastage of large memory. So you can prefer ArrayList over it which is inside the collection hierarchy.

Complexity is other issue like you want to insert in array then you have to trace it upto define index so over it you can use LinkedList all functions are implemented only you need to use and became your code less complex and you can read there are various advantages of collection hierarchy.

查看更多
【Aperson】
7楼-- · 2020-06-18 09:54
  • Arrays are not resizable.
  • Java Collections Framework provides lots of different useful data types, such as linked lists (allows insertion anywhere in constant time), resizeable array lists (like Vector but cooler), red-black trees, hash-based maps (like Hashtable but cooler).
  • Java Collections Framework provides abstractions, so you can refer to a list as a List, whether backed by an array list or a linked list; and you can refer to a map/dictionary as a Map, whether backed by a red-black tree or a hashtable.

In other words, Java Collections Framework allows you to use the right data structure, because one size does not fit all.

查看更多
登录 后发表回答