This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on collections framework in java

 
Naga venkateswara Rao
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Naga venkateswara Rao
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the best that I can think!




Any more creative ideas for a best approach?

Thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic