wood burning stoves 2.0*
The moose likes Java in General and the fly likes Question on collections framework in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question on collections framework in java" Watch "Question on collections framework in java" New topic
Author

Question on collections framework in java

Naga venkateswara Rao
Greenhorn

Joined: Dec 16, 2009
Posts: 7
Here is the requirement:

Using any java collection to capture below data
1. Furniture of a house that contains items made out of wood, plastic and glass.
2. Should be able to get total size of items in the collection
3. Also, We need to specifically count items depending on the material.

A abstract representation -

Furniture: 1. dressing table (wooden) 2. chairs (wooden) 3. telephone (plastic) 4. acquarium (glass) 5. dressing table (glass) 6. teaset (glass)

total items: 7
wood items : 2
plastic items : 1
glass items : 3

Any ideas on how it can be done. Code snippet would be great!

Thanks
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Yep, I have a pretty good idea, several actually, of how this could be done.
Here is a code snippet.



I showed you mine, now show me yours.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Welcome to JavaRanch. We don't like doing other people's homework here, but we do like helping others to learn, so that they can do their homework themselves. So, it doesn't really work well if you just post your homework assignment and say "I have no idea, will somebody please do this for me?".

Have you tried writing some code yourself? Where exactly did you get stuck?

If you don't know how collections work in Java, have a look at this excellent tutorial: Collections (written by Joshua Bloch, the guy who implemented the collections in the Java API himself).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Naga venkateswara Rao
Greenhorn

Joined: Dec 16, 2009
Posts: 7
Here is my approach! Might be a crude one





I can see that we get correct output:


Total items :5
glass items 2
wood items 2
platic items 1



Naga venkateswara Rao
Greenhorn

Joined: Dec 16, 2009
Posts: 7
This is the best that I can think!




Any more creative ideas for a best approach?

Thanks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

That looks like a good start!

Some tips:

You can create a constructor in the Furniture class that takes an item name, color and attributes, and sets those values. Then you don't need the doItem method in your ItemsCollection class - you could just do new Furniture("dressing table", "cream", "medium size");.

Line 19: It is not necessary to initialize obj with new Furniture(); - you're not going to use that Furniture object anyway.

You're using Furniture objects as a key in a Map. For this to work properly, class Furniture must implement proper equals() and hashCode() methods.

Why did you choose for a Map<Furniture, String>? Why not make the material a property of the Furniture object (just like the name, color and attributes), and just store them in a List<Furniture>?

Lines 37-41: You can iterate through collections in a much simpler way, without the need to explicitly deal with an Iterator:


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Question on collections framework in java
 
Similar Threads
array of objects confusion
using array list
Using html:options tags - novice to struts
Building table/roof: what gradient?
A problem regarding a java question