collection framework

Showing posts with label collection framework. Show all posts
Showing posts with label collection framework. Show all posts

List vs Queue vs Set vs Map


 List vs Queue vs Set vs Map


ListQueueSetMap
DuplicatesYesYesNoNo(Allow duplicate values not keys)
OrderYesYesNoNo
Null valuesYesPriority queue doesn't allow, but queue using LinkedList allows nullSingle nullSingle null key and many null values

ArrayList vs LinkedList vs Vector


ArrayList vs LinkedList vs Vector 


ArrayList
LinkedList
Vector
Data structureIndex-based  dynamic arrayDoubly linked listGrowable array
Increment size50%No initial size100%
Traverse Uses iteratorUses iteratorUses enumeration
Memory usageLess memory usageMore memory usage
AccessibilityRandom and fastSequential and slowRandom and fast
OrderInsertion orderInsertion orderInsertion order
DuplicatesAllowAllowAllow
Insert / DeleteSlowFastSlow
SynchronizedNoNoYes
ImplementsRandomAccess interfaceNA
RandomAccess interface &
Serializable interface
Null valuesYesYesYes

Array vs ArrayList


 

Array
ArrayList
Fixed-sizeSize is not fixed
Not type-safeType-safe
Allow both primitives and objectsDoesn't allow primitives. But after Java 5 auto-boxing will convert primitives to objects

Type safety means the compiler will validate the types while compiling and throw an error if you do anything wrong. 

Streams vs Collections in Java


 Streams vs Collections


Collections
Streams
The collections are used to store & group data in data structures like List, Set, Map...etc.Streams are used to perform complex operations like filtering, matching, mapping, etc... on stored data
Data modification can be done. You can add or remove data from a CollectionData cannot be modified
External iterations need to be done Internal iterations are being used
Collections can be traversedStreams can be traversed only once. If you need another traverse, you need to create a new stream
Collections are eagerly constructedStreams are lazily constructed