0

I need to call GetNewItem function million times;

Items XY = GetNewItem(X, Y); 
Items XYZ = GetNewItem(XY, Z); 
Items XZ = GetNewItem(X, Z); 
Items YZ = GetNewItem(Y, Z); 

This function aims to 1- find intersection between ArrayList of structure namely

ArrayList<Records> RecordLists

2- and it also calculates the probability for the new ArrayList , this is my code:

 class Records {

        public int RecordId;
        public double Prob;
    }

    class Items {
        public ArrayList<Integer> itemId;
        public ArrayList<Records> RecordLists;
        public double ItemProb = 0.0;

    };

 private ArrayList<Records> Intersection(ArrayList<Records> list1, ArrayList<Records> list2) {
        ArrayList<Records> Result = new ArrayList<>();

            int i = 0, j = 0;
            while (i < list1.size() && j < list2.size()) {
                if (list1.get(i).RecordId== (list2.get(j).RecordId)) {
                    Records RecordDetails= new Records();
                    RecordDetails.RecordId= list1.get(i).RecordId;
                    RecordDetails.Prob+= 1;
                    Result.add(RecordDetails);

                    i++;
                    j++;
                } else if (list1.get(i).RecordId < list2.get(j).RecordId) {
                    i++;
                } else if (list1.get(i).RecordId > list2.get(j).RecordId) {
                    j++;
                }

            }

        return Result;
    }

    public Items GetNewItem(Items item1, Items item2) {
        Items NewItem = new Items ();
        ArrayList<Integer> newItemId = new ArrayList<>();
        newItemId.addAll(item1.itemId);
        newItemId.addAll(item2.itemId);

        NewItem.itemId = newItemId;

        NewItem.RecordLists= Intersection(item1.RecordLists,item2.RecordLists);

        NewItem.ItemProb = getProb(NewItem.RecordLists);          
        return NewItem ;
    }

    private double getProb(ArrayList<Records> RProb) {
        double IProb = 0.0;
        for (int i = 0; i < RProb.size(); i++) {
            IProb += RProb.get(i).Prob;
        }
        return IProb ;
    }

For this code I got 'out of memory error'

I don't know how to save the memory and time, I tried this solution: java.lang.OutOfMemoryError: Java heap space with NetBeans

but my computer did freeze. I don't know what else I have to do.

Community
  • 1
  • 1
user3823821
  • 91
  • 1
  • 7

1 Answers1

0
  1. Please use java conventions, e.g. variables in camel case, non public variables in classes (use getters/constructor/setters)

  2. I'm not sure why are you getting the intersection that way, with that i and j variables. Please try:

    public <T> List<T> intersection(List<T> list1, List<T> list2) {
        List<T> list = new ArrayList<T>();
    
        for (T t : list1) {
            if(list2.contains(t)) {
                list.add(t);
            }
        }    
        return list;
    }
    

If you want to calculate something else maybe do it in a separate method?

  1. Use floats instead of doubles.

Could you please paste whole code? I would like to reproduce this.

Tomek
  • 543
  • 4
  • 12