2
\$\begingroup\$

Consider this code:

           public class DataSt {

            DataType time;
            DataType memory;


            public DataSt(DataType time, DataType memory)
            {
                this.time=time;
                this.memory=memory;
            }

            private static void saveIt(DataSt p, DataSt p2, String keyval, List<DataSt> comp) {      
                  ...
                }


            private static void saveIt( String keyval, List<DataSt> comp) {
                     ...
         }

            public static List<DataSt> compO(List<DataSt> curR, List<DataSt> prevR) {            
                Map<String,DataSt> cur = new HashMap<>();Map<String,DataSt> prev= new HashMap<>();      
                for(DataSt abc:curR)
                {
                    DataSt v = new DataSt (abc.time,abc.memory);
                    cur.put(abc.key,v);
                }

                for(DataSt abc:prevR)
                {
                    DataSt values = new DataSt (abc.time,abc.memory);
                    prev.put(abc.key,v);
                }

                List<DataSt> comp = new ArrayList<DataSt>();

                for (Entry < String, DataSt > abc: cur.entrySet()) {
                    if (prev.containsKey(abc.getKey())) {
                        saveIt(abc.getValue(), prev.get(abc.getKey()), abc.getKey(), comp);
                    } else {
                        saveIt(abc.getKey(), comp);
                    }
                }

                for (Entry<String, DataSt> abc: prev.entrySet()) {
                    if (!(cur.containsKey(abc.getKey()))) {
                        saveIt(abc.getKey(), comp);
                    }
                }   
                return comp;
            }
        }

With reference to the link "https://stackoverflow.com/questions/45230104/list-key-comparisons-and-return-value". It would be great if someone could review the code and suggest optimizations.

\$\endgroup\$
2
  • \$\begingroup\$ You've added the performance tag. Do you experience any performance issues? \$\endgroup\$
    – t3chb0t
    Jul 26, 2017 at 10:58
  • \$\begingroup\$ @t3chb0t no I don't....... just wanted to make sure I have a well optimized code w.ref. to the link "stackoverflow.com/questions/45230104/…" as I need to store the final comparison result into a List where the comparison turns little hard w.r.t ArrayList as suggested in the above link... \$\endgroup\$
    – nt fury
    Jul 27, 2017 at 9:44

1 Answer 1

1
\$\begingroup\$
public static List<DataSt> compO(List<DataSt> curR, List<DataSt> prevR) {

    Map<String,DataSt> cur = new HashMap<>();Map<String,DataSt> prev= new HashMap<>();
   curR.parallelStream().forEach(entry -> cur.put(entry.key,new DataSt (entry.time,entry.memory)));
   prevR.parallelStream().forEach(entry -> prev.put(entry.key,new DataSt (entry.time,entry.memory)));

    List<DataSt> comp = new ArrayList<DataSt>();
    for (Entry < String, DataSt > abc: cur.entrySet()) {
        final String key = abc.getKey();
        if (prev.containsKey(key)) {
            saveIt(abc.getValue(), prev.get(key), key, comp);
        } else {
            saveIt(key, comp);
        }
    }

    for (Entry<String, DataSt> abc: prev.entrySet()) {
        final String key = abc.getKey();
        if (!(cur.containsKey(key))) {
            saveIt(key, comp);
        }
    }
    return comp;
}

Also you can try to use parallel stream on map, but it should not be HashMap, and I'm not sure that it will be faster.

\$\endgroup\$
1
  • \$\begingroup\$ Please, mark this answer as right if it is true. \$\endgroup\$ Jul 27, 2017 at 9:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.