D
Donkey Hottie
I'm parsing data from disk, and need to keep a collection of Strings in
memory.
Java does not consider
String s1 = "Abba" ;
String s2 = "Abba" ;
the same, their reference in memory will be propably different. So two
times "Abba" will be allocated?
I may have tens of thousands of "Abba" read from disk to an "index"
object having a String containing the content.
I don't want tens of thousands of different "Abba" strings in the JVM
memory, but I want that
s1 == s2
My algorithm so far is that
SortedMap map = new TreeMap()
map.put("Abba", "Abba");
..
String parsedString ...
String s = (String)map.get(parsedString);
if (s == null)
{
map.put(parsedString, parsedString);
s = parsedString ;
}
myObject.set(s) ;
That way I get only one copy the string in memory. I may have tens of
thousends of "records" read, but the string parsed is mostly same.
MyObject is an index object, containing metadata about the info just read
(file name, position in the file, etc), and that string among others.
So my question is..
How to create a private String table, as Java does not do it? Better
solutions than TreeMap?
TreeSet would be cool, but it has no getter suitable.
memory.
Java does not consider
String s1 = "Abba" ;
String s2 = "Abba" ;
the same, their reference in memory will be propably different. So two
times "Abba" will be allocated?
I may have tens of thousands of "Abba" read from disk to an "index"
object having a String containing the content.
I don't want tens of thousands of different "Abba" strings in the JVM
memory, but I want that
s1 == s2
My algorithm so far is that
SortedMap map = new TreeMap()
map.put("Abba", "Abba");
..
String parsedString ...
String s = (String)map.get(parsedString);
if (s == null)
{
map.put(parsedString, parsedString);
s = parsedString ;
}
myObject.set(s) ;
That way I get only one copy the string in memory. I may have tens of
thousends of "records" read, but the string parsed is mostly same.
MyObject is an index object, containing metadata about the info just read
(file name, position in the file, etc), and that string among others.
So my question is..
How to create a private String table, as Java does not do it? Better
solutions than TreeMap?
TreeSet would be cool, but it has no getter suitable.