2

I am trying to clean my code and want to used optimised approches in the code.

Currently I have hashmap of this type private HashMap<String, ArrayList<allProperty>> fData;

So The data is coming from server in every 5 second. and I am updating this hashmap every 5 second something like this

       fData.put("Student", allStudent);
       fData.put("Emp", allEmp);
       fData.put("Other", allOther);

So from allproperty class object, there is marks fields and I am trying to extract that marks field for all key and creating new arraylist of integer so that I can append the marks in created arraylist by every 5 second.

I was using different arraylist for each student, emp and other before and it was working fine but it seems like alot of repeation of code. That is why I am trying to achieve it with hash map

I am trying soemthing like this but it is not appending data ...

for (String type: allTypes) {
            if(fData.get(type).size() > 0){
                arraySort(fData.get(type));
                temp = new ArrayList<Integer>();
                temp.add(fData.get(type).get(0).marks);
            }
            cData.put(type, temp);

cType is hashMap of type given below and

 private HashMap<String, ArrayList<Integer>> cData;

where allTypes is

 signalTypes.add("Student");
    signalTypes.add("Emp");
    signalTypes.add("Other");

at the end I want three new arraylist with key and able to append data in every 5 seconds. Thanks

5
  • the class allProperty should have marks as public field... Commented Nov 22, 2014 at 11:05
  • yes it has...I can access it ..that i know Commented Nov 22, 2014 at 11:07
  • Are you sure it would not be better using 'addAll' for temp? It is not clear what the type of fData and marks. Commented Nov 22, 2014 at 11:09
  • try print this in log fData.get(type).get(0).marks does it get data correctly? it should give int.. as temp is ArrayList<Integer> Commented Nov 22, 2014 at 11:09
  • yes it gets but the problem is when I get new data after 5 second, it does not append in new created arraylist ..I hope I am clear Commented Nov 22, 2014 at 11:10

1 Answer 1

2
  1. Get your ArrayList

2a. If it doesn't exist - put your new ArrayList

2b. Else USE this ArrayList. It's mutable. So you can call .add on this object and it will be updated in HashMap

Try this:

for (String type: allTypes) {
        if (fData.get(type).size() > 0) {
            arraySort(fData.get(type));
            if (cData.get(type) == null) {
                temp = new ArrayList<Integer>();
                temp.add(fData.get(type).get(0).marks);
                cData.put(type, temp);
            } else {
                cData.get(type).add(fData.get(type).get(0).marks);
            }
        }
}
Sign up to request clarification or add additional context in comments.

5 Comments

but I guess I am doing the same ...the idea is to update each arrraylist specific to key
You creating new ArrayList instead of using old one in your code. When you use old one you shouldn't call map.put()
but if I would not create new one, then the loop will run for three times and add three values instead one for particular key
this is the error...as there is not object exist Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
put == null instead of != null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.