Is it possible to have my program name objects as they're created? Such as using a loop that names objects object1, object2, etc.. This is an issue because it won't always be clear how many objects are necessary, it depends on the users input. Or is there another way to get around this without having to name the objects?
No you cannot create references dynamically as you have mentioned but you can always use a collection of objects. If you know the number of objects upfront, then go for Array otherwise go for a List for dynamic number of objects.
You can create a hashtable of names and objects, which you append to as you create each object.
If the objects are of the same class, you can add an attribute to the class to contain the name.
Related
In a Java Application you many times face a situation, when you want to store all Object References for newly created objects, for example to retrieve it by a specific key later. Key can be any of this object's fields. There are many Collections, that are suitable for it:
List
Map
Set
etc...
All of them have one similar thing, if you want to use it, you have to put it inside a variable. Static variables are bad and should not be used, so variable also needs a class that will hold it. How should you name a class, that is responsible for storing references to other class's objects? Also, is it a good practice to put a method for retrieving a single reference from this "manager" class? Is there some way to automatically generate this kind of class, by its key?
I'm trying to make an interactive console program which creates hierarchy of boxes and balls contained in them. I didn't figure out how to create nested boxes and how to navigate between boxes. The problem is by what method's type I should create new instances of Box. If type is void, I can't get reference to that object anymore. If type is not void I have to store references somewhere. What are keywords in google to solve this case? Is there any pattern for that?
The problem is that every changes to hierarchy I can do only at runtime, so no hardcoded instantiations are allowed.
The key to this is that you have to represent the containment in your data structures:
An class of objects that can contain other objects needs a field which is collection (e.g. a list or set) of references to the objects that it contains.
A class of objects that can be contained by other objects needs a field that refers to an object's current container.
Some classes of objects can be both containers and contained.
Best way to describe this is explain the situation.
Imagine I have a factory that produces chairs. Now the factory is split into 5 sections. A chair can be made fully in one area or over a number of areas. The makers of the chairs add attributes of the chair to a chair object. At the end of the day these objects are collected by my imaginary program and added into X datatype(ArrayList etc).
When a chair is added it must check if the chair already exists and if so not replace the existing chair but append this chairs attributes to it(Dont worry about this part, Ive got this covered)
So basically I want a structure than I can easily check if an object exists if not just straight up insert it, else perform the append. So I need to find the chair matching a certain unique ID. Kind of like a set. Except its not matching the same object, if a chair is made in three areas it will be three distinct objects - in real life they all reperesent the same object though - yet I only want one object that will hold the entire attribute contents of all the chairs.
Once its collected and performed the update on all areas of the factory it needs iterate over each object and add its contents to a DB. Again dont worrk about adding to the DB etc thats covered.
I just want to know what the best data structure in Java would be to match this spec.
Thank you in advance.
I'd say a HashMap: it lets you quickly check whether an object exists with a given unique ID, and retrieve that object if it does exist in the collection. Then it's simply a matter of performing your merge function to add attributes to the object that is already in the collection.
Unlike most other collections (ArrayList, e.g.), HashMaps are actually optimized for looking something up by a unique ID, and it will be just as fast at doing this regardless of how many objects you have in your collection.
This answer originally made reference to the Hashtable class, but after further research (and some good comments), I discovered that you're always better off using a HashMap. If you need synchronization, you can call Collections.synchronizedMap() on it. See here for more information.
I'd say use ArrayList. Override the hashcode/equals() method on your Chair object to use the unique ID. That way you can just use list.contains(chair) to check if it exists.
I'd say use an EnumMap. Define an enum of all possible part categories, so you can query the EnumMap for which part is missing
public enum Category {
SEAT,REST,LEGS,CUSHION
}
Suppose I have a CatHerder and he has Cats, when I getCatHerderByID(String id) should I load in the CatHerder and set an arrayList of cats or an arrayList of catIDs?
getCatHerderByID() should be a method which returns an object of type CatHerder, which can have a property catsOwned which will contain a list of IDs, populated by the constructor. In addition, you can have a method on the CatHerder object called getCats() which returns an array of Cat consisting of the IDs in the CatHerder object. You shouldn't load unnecessary information just because it has a relationship with the information you actually want -- in many cases, everything will have a relationship with the data you actually want.
When you don't have much data or many concurrent users, you can go with either way.
One easy options would be to add an additional parameter specifying whether to return a "deep copy" including the relations, or just the attributes of the object itself.
Stay away from the urge to add more parameters, or you will end up re-implementing SQL :)
During serializing objects, can we assign name to different objects? So, that on the time of reading objects, i can call any object by its name and later on can access its members.
I can do it by assigning a unique field to each object and later compare it against that field but that will cost - O(n).
Is there any other way to fast access any particular object, serialized in a file of suppose 100 objects.
Thanks you
Put them in a map and serialise the map instead?
Why do all of the objects have to be in one stream? What if you just save each object in its own file and access the object by file name? If you really need a single file, you could archive them all in a ZIP file, and let it handle the indexing function for you.
Maybe it's overkill, but for that use case i would use a database. Something like Berkeley DB sounds appropiate:
http://download.oracle.com/docs/cd/E17277_01/html/GettingStartedGuide/index.html