How to differentiate different Collection in java? -


please explain how different collection used under different scenario.

by mean how can differentiate when use list, set or map interface.

please provide links examples can provide clear explanation.

also

    if insertion order preserved should go list.     if insertion order not preserved should go set. 

what "insertion order preserved" means?

insertion order

insertion order preserving order in have inserted data.

for example have inserted data {1,2,3,4,5}

set returns {2,3,1,4,5} while list returns {1,2,3,4,5} .//it preserves order of insertion

when use list, set , map in java

1) if need access elements using index, list way go. implementation e.g. arraylist provides faster access if know index.

2) if want store elements , want them maintain order on inserted collection go list again, list ordered collection , maintain insertion order.

3) if want create collection of unique elements , don't want duplicate choose set implementation e.g. hashset, linkedhashset or treeset. set implementation follow there general contract e.g. uniqueness add addition feature e.g. treeset sortedset , elements stored on treeset can sorted using comparator or comparable in java. linkedhashset maintains insertion order.

4) if store data in form of key , value map way go. can choose hashtable, hashmap, treemap based upon subsequent need.

you find more useful info @ http://java67.blogspot.com/2013/01/difference-between-set-list-and-map-in-java.html


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -