• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Case studies on Collection API

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me a book or a source where I could find case studies on java Collection API?
I need to get an insight as to which collection to use in a particular scenario.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the "Implementations" page in http://docs.oracle.com/javase/tutorial/collections/.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shivang sarawagi wrote:Can anyone tell me a book or a source where I could find case studies on java Collection API?
I need to get an insight as to which collection to use in a particular scenario.



Bringing this up again...Even am facing the difficulties to understand the Collection API in Java ...Could anyone help me or give me a suggestion what are the important things to learn in Collection and how can I learn it quickly..

Thanks in advance..
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch Sarah!

Here's a table of the most important interfaces you should know, and the classes you should use as their default implementations:

Collection interfaceDefault implementation to useWhen to use the interface
ListArrayListYour elements need to be ordered* or you want to allow duplicate elements.
SetLinkedHashSetYou want to disallow duplicate elements.
SortedSetTreeSetYou want to keep the elements in your set sorted*.
MapLinkedHashMapLike a Set, but you want to associate each element (key) with another object.
SortedMapTreeMapYou want to keep the keys in your map sorted*.
QueueArrayDequeYou want to add elements to the back and retrieve them from the front.
DequeArrayDequeYou want to add and retreive elements to and from either end.


There are some specialized interfaces like NavigableSet and BlockingQueue, but you can read more about them when you become familiar with the basics.

*Ordered means something different than sorted. Ordered means that each element is associated with an index. The list List.of('D', 'C', 'A', 'B') is ordered, even if it is not sorted, because 'D' is associated with the index 0, 'C' is associated with the index 1, etc.

An important rule is that you never declare variables of a more specific type than you need. Let's say that you just want to keep a collection of objects in memory, and you don't care about their order. You just want to add elements to the collection (possibly duplicates) and iterate through the existing elements. You would then use the Collection interface, because it does everything you need:

As you can see, while I initialized my collection field with an instance of ArrayList, I declared it as a Collection, because that's all I need. Similarly, if you don't need to add any items to a collection and you don't need to check the size of a collection and you only want to iterate through the elements, you can use the Iterable interface instead of the Collection interface.

Another important rule: Never use raw types! Collections are generic types, meaning they accept generic type arguments. In the Collection<Thing> myThings declaration I wrote above, Thing is the generic type argument. Java allows you to write just Collection, but that will disable type checking by your compiler, which can be a source of bugs.

The best way to learn is by doing. With these rules in mind, USE collections. Read API documentation of the interfaces while you're using them. Try to use methods you're unfamiliar with to solve problems. Let people review your code, and they might suggest and explain classes and interfaces you're not familiar with yet.
 
Sarah Jay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephan....

I will follow your suggestions ..
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will also find examples in the “interfaces” page of the same tutorial.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan, great post, but I disagree with one thing. You use LinkedHashSet and LinkedHashMap as defaults. I would prefer HashSet and HashMap unless you need a fixed order. If you don't need it (and I hardly ever really need it apart from getting predictable test results), for instance because you use a Map just for lookups, a HashMap is a (slightly) better choice.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think they help in debugging, because elements don't jump around while stepping through the code. You can easily replace all of them before release if you *really* need to optimize memory usage, which I often find is unnecessary.

I will admit that I usually prefer HashMap over LinkedHashMap myself, but I think LinkedHashMap is a more sensible default for a beginning programmer until they can reason a bit more about their own choices.
reply
    Bookmark Topic Watch Topic
  • New Topic