Using a String as an identifier when creating a new Object - java

I have a String eg:
1,TSM,501,SM0156,John Thorne,BCO200,24,30,2,CSM,500,AC1157,Peter Jones,BCO104,24,60,...
The string represents a list of people within a department.
The "SM0156" & "AC1157" are their unique identifiers within the department
I would like to loop through the string and create a new 'person object' every time I meet an identifier. The object is then stored in an ArrayList. I believe I could do this with the following code:
deptList.add(new = PersonDetails());
This creates my object but I want to be able to reference it later possibly by the unique identifier! Whilst looping through the original string i have extracted out the identifier in this case "SM0156". I was hoping there was a way to then use this as the reference to the object EG
PersonDetails "SM0156" = new PersonDetails();
deptList.add("SM0156");
Obviously here "SM0156" represents a string but surely I could convert it somehow to use as an reference to my new PersonObject??
Thanks for any help in advance..

You do not want to attempt this. Store your details in a collection and depending on the collection you should use it accordingly.
If you choose an ArrayList, make sure your PersonDetails class has a field Id which you can look up (or create a new class that holds an Id and a PersonDetails object).
Another solution is creating a Map<String, PersonDetails> to map the Id to the person.

Try to use HashMaps. HashMaps stores the objects with key/value principal.
http://www.mkyong.com/java/how-to-use-hashmap-tutorial-java/

You can use a Map<String, PersonDetails>.

Related

decoupling object construction tied to input data

What I'm doing
I'm using Dependency Injection to decouple my classes.
How I'm trying to do it
The class I am making constructs ObjectImplementation (the interface) Objects to hold data and it acts as a sort of container. I'm doing this to parse data and cross reference two data-sets. My problem is that I currently have object construction tied to the data being formatted a certain way. I am using the Factory pattern and a properties file "config.properties".
What I want to be able to do
I want to be able to have the factory take in an array of fields or some other similar type and be able to construct instances of the reflected object type without dependencies on the data. In this case they are Salesrep instances but other times I want to construct Salesrep instances or other classtype instances with different fields filled and different ones null without formatting the data to contain the names of fields.
The end goal
The point is so that I can construct different objects with the same container code. If I want to contain the objects differently I'll simply make a new implementation of the parent interface of this container class.
What I'm thinking is the problem
I've figured out that a Fieldmap was a good idea through previous versions of this question and my own research. Yet there is no way to actually set those fields without having something in the data to match to the Fieldmap
Extra Clarification
I really want to know if I can find a way to achieve my goal without adding field names to the data
//creates new properties object and loads in the file configuration
Properties prop = new Properties();
prop.load(SalesRepbyId.class.getResourceAsStream("config.properties"));
//reflects in the class we wish to use
Class<? extends ObjectImplementation> Classtouse = Class.forName(prop.getProperty("ObjectImplementation")).asSubclass(ObjectImplementation.class);
//initializes the data and some hashmaps to store the data or the methods of the reflected class
ArrayList<String[]> Salesrep_contactlist = FileParser.ReadFile();
Map<String, ObjectImplementation> SalesrepByIdMap = new HashMap<>();
Map<String, Method> MethodMap = new HashMap<>();
//adds in the data (fields) by constructing objects of the reflected type using the ObjectImplementation interface
for (String[] fieldarray : Salesrep_contactlist) {
ObjectImplementation object_to_add = null;
try {
//utilizes the factory pattern to return an instance of the reflected class
object_to_add = Factory.getObjectImpl(prop.getProperty("ObjectImplementation"),fieldarray);
/**
uses a method hashmap to map the name of the method to the Method object.
I did it this way because dynamic variable declarations are not possible and
I wanted to decouple Method declarations from the specific class that has
them. If i just hardcoded in which methods I get from the implementing class
that introduces extra dependencies I don't want.
**/
for (Method method:Classtouse.getMethods()) {
MethodMap.put(method.getName(),method);
}
//same as above but for fields this time
for (Field field:Classtouse.getFields()) {
FieldMap.put(field.getName(),field);
}
//object_to_add is a String[] with the format [Fieldname1:fieldinput1,Fieldname2:Fieldinput2]
//so I want to get this array and get each element, seperate the fieldname and then use that string to access the actual Field object of the same name in FieldMap
String fieldname = object_to_add.get(0).split(":").get(0)
String fieldinput = object_to_add.get(0).split(":").get(1)
Field name_example = Fieldmap.get(fieldname)
name_example.set(String.class,fieldinput)
//This requires the data to have the fieldname in it rather than just the fieldinput (or data). Also it confines the input to be strings because I don't think I can use a generic type to set this field even though potentially I would want to.
There is no way for me to dynamically set Field types without something to go off of in the data or elsewhere. In order to avoid something hard coded like: Salesrep rep = new rep (arg1,arg2,arg3 ...) I needed to use the Fieldmap and be able to match the data coming in to what fields I wanted to set. Since I didn't want to do it by order ex:
List list = new list("bob","800-352-4324","foo#example.com");
int i = 0;
for(i = 0, i > list.size(), i++){
Field field = new Field(//need a name here automatically rather than hardcoded)
field.set(object_to_add,list[i])
i++
}
The above didn't have any reference to the actual name of the Field that I actually use in my class. I didn't want that and then it dawned on me that the first line of my data (which is in CSV format) has the Field names effectively listed. ex:
(in the CSV File) foo.txt:
1: name,phonenumber,email
2: "bob","800-352-4324","foo#example.com"
3: "steve","800-444-4444","annoyingcommercials#example.com"
4: ...
Using this knowledge My solution is to use the first line of my data to specify the field names and their order so that when I take in lines as an array of these strings I can use the first line array as a reference to how to set the fields. I will know that the first element in the array should be the name the second should be the number ect ect. This way I only have to change the first line if I want to change how many fields the data holding class actually has.
puesdocode:
ObjectImpl. Classtouse = refelct in the class to use here from properties file
List(String[]) fieldarray = the raw data taken in and converted to a list of string arrays
String[] firstline = fieldarray.getfirstline()
List(String[]) restoflines = fieldarray.getallotherlines()
for i = 0, i > firstline.size(), i++{
Fieldmap.put(Name of the field from firstline[i], create a new Field object here with the Name);
Field fieldtoset = Fieldmap.get(Name of the field again)
fieldtoset.set(make an instance of the Classtouse here, restoflines[i] which represents the data in the 'Name' column)
}
For some silly reason I had it in my head that there was a way to do this without any change to the data, as if the Factory which created the object could take in arbitrary/generic arguments and somehow just know where each field went. I realized that that was silly because I needed to tell the code how to actually set the fields but In a way that it wasn't hard-coded into the class. This solution puts the dependency on the data so now its not hard-coded into the class. I should have seen this sooner.

Finding size of a ArrayList where list name is passed as a String

I have an ArrayList of a java object with name say, "mylist".
I am getting the name of the list(mylist) from database which comes as a String. I need to find the size of this list in JAVA and need to iterate over it.
Is there a way to do this?
Edit:
To be more clear here, I have a class "Candidate"
class Candidate {
String firstName;
String lastName;
List<Education>educationRecords;
.....
}
class Education {
String school;
String degree;
.....
}
I need to populate Candidate object from a JSONObject whose structure is very different from Candidate object(so can't use Gson or jackson).
The simple fields like firstName, lastName, school and degree are stored in the database.
I fetch a Listfields and iterate over them to set the Candidate object. For setting firstName, lastName, I am using SpEL.
But, for fields like school and degree, it becomes complex as I would need to know the size of education records in JSONObject.
So, what I thought was while iterating over all fields if school field is present in Education class(using reflection), I would need to check if educationRecords is a of List type in Candidate class and then by some logic X(taking educationRecords as variable name) iterate over it.
It is the logic "X" I am missing.
I don't quite understand the use case here. However, my understanding is that you have an ArrayList<Object> mylist and you need to find the size of it based on a string retrieved from DB.
You can store the list in a cache, where the key is the name of the list and value is the actual object. You can use redis, eh-cache or memcache (Or anything similar) to achieve the same.
So when you retrieve the name from DB, get the corresponding object from the cache and do size() on the object after type casting it. If your are storing the list type instead of object type in the cache, then the last part is not needed (typecasting).
You can use a map:
Map<String, List> listMap = new Hashmap<>();
Each time you create a list - add it to your map with a key that is a name of your list. Once you get a name you can easily check if such list exists in your map, get it, check its size and iterate through it... Be careful that once you are done with your list you should remove it from your map as well as otherwise you will keep them around and it will eat your memory up.

A proper way to make an HashMap with multiple value type

I have an object the represent an entity. By example i have the "user" java object that have the followings field, String name, String first name, String address, boolean deadOrAlive. But now instead of having field i want to put them into a hashmap, so my first reflex was to make it this way :
private HashMap<String, Object> fieldsHM;
This would means that i have to cast my HM value when i want to use it and i don't want to make that because i need to know the type before i use my value. I want to make something like :
Set<String> keys = fieldsHM.keySet();
for(String key : keys) {
if(fieldsHM.get(key).isBoolean()) {
// Do the appropriate things
} else {
// Do the thing for this case...
}
}
I think it's possible in java, would this be good to do it this way ?
EDIT 1: I don't want to make a hashMap because this is not what i need. What i need is a way to browse the fields of the Entity user fields by fields, and depending the type of the field do the appropriate things.
I don't want to make a hashMap because this is not what i need. What i
need is a way to browse the fields of the Entity user fields by
fields, and depending the type of the field do the appropriate things.
I guess that would be a job for Reflection, like User.class.getFields().
It will still be uncomfortable to distinguish between primitive field, but you could use their wrapper classes instead.
But whatever path you choose, I think there would be a better solution if you would state what the actual goal is.
Depending on your actual use case, it might make sense to use JSON (maybe with databind) or even a database.
You could use the heterogeneous container pattern, but I would abandon the map idea and use a proper object.

How to create a Vector where each entry is a structure with 2 fields

I am trying to create a data structure in Java that is a Vector into which some information about an unknown number of entities will go from a database. When it comes to this information, I only care about 2 fields. Also, it is required that when I iterate through this Vector, I can extract these two fields (say, String) in pairs. Schematically speaking,
String s1 = Vector[1].Field1, String s2 = Vector[1].Field2
Is this even possible? Does anyone know a more efficient way to achieve this?
Note: I would like to keep it in a single Vector because I pass it to another class for processing.
Use
public class Entry {
public String field1;
public String field2;
}
List<Entry> vector = ...;
Why not using Map<String, String>
But map will be unique keys
Also you can create your own Class and pass it to the Vector<yourclass>
Here's how I would do it :
Create an object Pair with 2 attributes : field 1, field 2
Add this object to your vector.
Use a Vector of maps, with keys value1 and value2. Vector<Map<String, String>>.
Or define a new object with two attributes ( I suggest the second option )

add property for a object dynamicly

Hi:
In our application,we have retrive some data from the database,for example,the table have columes:id,name,age,address,email.
Then we will get some of these propertis according to the client.
If client need the id, name, we get the id name, if client need id, name, age, we get the id, name, age.
Now we want to create a class to wrap these properties. However we do not know exactly which field are requested.
String[] requestPro={"name","id"}; //this field is specified by client
Map<String, Object> map=new HashMap<String, Object>();
Entity en=Entity.newInstance();
for(String p:requestPro){
map.put(p, BeanUtils.getProperty(en, p));
}
Here can I replace the map with a Class?
If I understand you right, you want to dynamically add properties to a class, or rather: to a specific instance of a class.
The former is possible e.g. in Groovy, where there is a metaclass object for every class, to which you can assign behavior at runtime, the latter is possible in JavaScript, where you can assign behavior both to an object's prototype and to an object itself. But neither of those versions is possible in Java, so using a Map or a similar structure is the thing to do in Java.

Categories

Resources