• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner Class Iterator for elements of Bag Map  RSS feed

 
Phil Maqui
Greenhorn
Posts: 10
Chrome Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im having problems with my project and im asked to make a iterator over the elements of a bag and that i needed to use a inner class to define my iterator for densebag
This is what ive got so far, im not sure if the other methods work correctly but here goes

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a specific question? I didn't look at your code very closely, but your equals() method is clearly not correct, nor is add().
 
Phil Maqui
Greenhorn
Posts: 10
Chrome Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Do you have a specific question? I didn't look at your code very closely, but your equals() method is clearly not correct, nor is add().


i was trying to ask how to implement the inner class iterator, this is what ive done so far:


 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Phil,

I'd suggest you go through the Java Docs on what exactly what an iterator is, what all methods are there in an iterator and how to implement an iterator.

In the first code which you have posted you are overriding the equals and Hashcode, Looking at the code I don't think you know exactly what you are doing...So as to why overriding equals and hashcode is necessary take a look at this example from Stephan van Hulst http://www.coderanch.com/t/514972/java/java/Explation-Object-Identity#2330779

In case you don't understand the Java doc's on how to implement an iterator, here is a small example

 
Phil Maqui
Greenhorn
Posts: 10
Chrome Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I kinda know the iterator works, and that those 3 contracts are required to be implemented if i am to create my own iterator class as its stated in the interface.
My main problem is making a specific iterator for maps, i can make my own iterators for arrays and arraylists but having trouble on this one, the purpose as to why i need to define my own is because it needs to count even the duplicates in the map for example i can have:

For example, if T happens to represent type "String", then we can imagine a DenseBag containing the following:
"apple", "apple", "apple", "duck", "duck", "zebra", "zebra", "zebra", "zebra"

In this case the underlying map would look like this:

KEY VALUE
================
"apple" 3
"duck" 2
"zebra" 4

In other words, if a bag contains 5 instances of the String "a", my iterator will generate the String "a" 5 times.
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I guess you know you are talking about having duplicate entries in a Map, as far as I know Maps of any sort cannot have duplicate values.
 
Phil Maqui
Greenhorn
Posts: 10
Chrome Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its more like for example there is 5x "apple" so in the key "apple" the value is 5 (as Integer), its kinda a trick question but thats how it works

here is the project description : http://www.cs.umd.edu/class/spring2013/cmsc132-34/Projects/P6/Proj4.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!