Java: memory efficient storing of arrays of integers -
premise: problem might known, , might using wrong wording, please refer me elsewhere if case.
quick problem overview: have store high number of arrays of integers in order avoid duplication. doing following:
linkedlist<int[]> arraysalreadyused;
upon using array, add list. before using array see if in list. since need use many high dimensional arrays run memory issues.
question: good/the best way of doing in order minimize amount of memory occupied? there way represent such arrays hash string? , better?
it may make sense create wrapper implements equals
, hashcode
can place arrays in set
o(1) contains
/add
. like:
public class intarray { private final int[] array; private final int hash; public intarray(int[] array) { this.array = array; this.hash = arrays.hashcode(this.array); //cache hashcode better performance } @override public int hashcode() { return hash; } @override public boolean equals(object obj) { if (obj == null) return false; if (getclass() != obj.getclass()) return false; final intarray other = (intarray) obj; return arrays.equals(this.array, other.array); } }
you can use set:
set<intarray> arrays = new hashset<> ();
that create small overhead (guestimate less 20 bytes per wrapper) perform better linkedlist.
if memory concern go int[][]
more painful...
Comments
Post a Comment