• 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

Recording an object's existence, then seeing if it has been recorded before

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi; I'd like to know if there is a class in Java that allows me to 'record' an object, then see whether I've recorded it before very quickly. It seems like I can use a HashMap for this, with the keys being my records and the values being whatever object, so that when I put in a new key, it makes a new instance of the value object, and I simply check whether the value object is null or not. But I was wondering if there's a more specialized class to implement this behavior. I only intend to record each object only once, so it's not necessary to keep count of the number of times I've recorded the object.
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch. What do you mean by

that allows me to 'record' an object

?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A HashSet would be simpler, it seems to me.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That might not work if you have overridden the equals method. It depends whether two objects returning true from equals count as the same object or not.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That might not work if you have overridden the equals method. It depends whether two objects returning true from equals count as the same object or not.



What does the OP mean by "record the object" Campbell? Could you please explain?
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bartholomew,

a simple approach could be to include, in the variable of interest, a static field 'Number', and in the constructor you add 1 to this number.
By inspecting this field, you can see the number of instances.

If you have a lot of classes you're interested in, then this method is less suited (since you have to subclass existing classes).

I agree with Mansukhdeep though. Could you elaborate a little more about your intentions?

Greetz,
Piet
 
Bartholomew Benson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, essentially, what I would like to do is something like this: say there's a robot walking around a maze. When it comes across something, it checks to see whether it has seen that something before (something of type, say, FieldObject); if it hasn't, then it's recorded in the robot's memory, otherwise, the robot ignores it. There's only one of any given type of thing the robot can encounter (they're all of type FieldObject, though), so keeping track of the number of things it's seen isn't important. So what I'd like to do is to implement that functionality of simply returning true or false based upon if we have already put this object into the robot's memory. I was wondering if there's a data structure (as a class in java.util or something) that supports doing stuff like this (put the FieldObject in it, then query the data structure to see if I've put it inside) in a more straightforward manner than using, say, a HashMap.

Sorry for the late reply, I thought I replied to this topic before when I actually left the thread reply page hanging in the background.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is wrong with the suggestion of a set earlier?
 
Bartholomew Benson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What is wrong with the suggestion of a set earlier?



Well, I wrote that post after reading the first post, and not any of the following posts. Now that I have, I now see that a HashSet is probably a good choice. Oops. Thanks!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bartholomew Benson wrote:Well, I wrote that post after reading the first post, and not any of the following posts. Now that I have, I now see that a HashSet is probably a good choice. Oops. Thanks!


OK, but my question to you is: What type should that HashSet be? I doubt if HashSet<FieldObject> is going to help you much, since what you want to know is if the Robot has seen another FieldObject of the same type.

One thing that might be worth considering is a public Type enum inside your FieldObject class (Google for 'nested') that every FieldObject instance is initialized with.

Your "memory" could then simply be an EnumSet or EnumMap - which is probably even quicker (and much more compact) than a HashSet. You could even set up different 'targets' for each type, allowing the Robot to pick up say 1 of this type, and 3 of another.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bartholomew Benson wrote: . . . Well, I wrote that post after reading the first post, and not any of the following posts. . . .

Happens all the time. Just one of those things.
 
Bartholomew Benson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Bartholomew Benson wrote:Well, I wrote that post after reading the first post, and not any of the following posts. Now that I have, I now see that a HashSet is probably a good choice. Oops. Thanks!


OK, but my question to you is: What type should that HashSet be? I doubt if HashSet<FieldObject> is going to help you much, since what you want to know is if the Robot has seen another FieldObject of the same type.

One thing that might be worth considering is a public Type enum inside your FieldObject class (Google for 'nested') that every FieldObject instance is initialized with.

Your "memory" could then simply be an EnumSet or EnumMap - which is probably even quicker (and much more compact) than a HashSet. You could even set up different 'targets' for each type, allowing the Robot to pick up say 1 of this type, and 3 of another.

HIH

Winston



Well, to be honest, the overall problem is a bit more complicated than I explained at first. The robot has to walk to certain FieldObjects and pick them up, at which point they're removed forever. Thus, every FieldObject has a unique reference. The robot determines what type of FieldObject it is, puts it into an appropriate ArrayList, and then performs actions based on what kind of FieldObject it is (ie. some FieldObjects are dangerous, so it has to ignore them or make a note not to get close). All in all, there are maybe only 5 or 6 types of FieldObjects the robot can encounter (or rather, they're all of base class type FieldObject, but they have internal String data that allows me to differentiate one 'type' of FieldObject from another), and contrary to what I said earlier, it can encounter the same type multiple times (typically only 3 times at maximum). But the references (which is what I meant by types, sorry about that) are all guaranteed to be unique. The FieldObjects that get picked up are why I want to use the HashSet: I want the robot to record down every FieldObject it picks up and then report a list of all the FieldObjects it picks up along the way. I hope to get many samples of data with this for various starting times and durations, with which I can determine the ideal times to make the robot run (certain durations and starting times offer more FieldObjects than others).
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic