It will be impossible saving so many object that can not fit the available memory. There are two solutions. First is to use a cache which will persist to a local file objects that can not fit in the memory. Something like ehcache. Second solution is instead of using objects to switch to a two dimensional array
long[][] cache = new long[1000*1000][];
long[] row = new long[2];
row would hold mToId and mType. Rows would be inserted into cache using mFromId as index.
Here's an example:
Random r = new Random();
class ActVO {
private long mFromId;
private long mToId;
private int mType;
}
int capacity = 1000*1000;
List<ActVO> resultSet = new ArrayList<ActVO>();
for (int i = 0; i < capacity; i++) {
ActVO element = new ActVO();
element.mFromId = i;
element.mToId = r.nextLong();
// let's say there are not more than 10 types
element.mType = r.nextInt(10);
resultSet.add(element);
if (i == 57) {
System.out.printf(" db result 57: mToId=%d, mType=%d\n", element.mToId, element.mType);
}
}
long[][] cache = new long[capacity][];
// iterating trough a database set
for (ActVO element : resultSet) {
long[] row = new long[2];
row[0] = element.mToId;
row[1] = element.mType;
cache[(int) element.mFromId] = row;
}
System.out.printf("57th row from cache: mToId=%d, mType=%d\n", cache[57][0], cache[57][1]);