Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large amount of constants in Java

Tags:

java

android

I need to include about 1 MByte of data in a Java application, for very fast and easy access in the rest of the source code. My main background is not Java, so my initial idea was to convert the data directly to Java source code, defining 1MByte of constant arrays, classes (instead of C++ struct) etc., something like this:

public final/immutable/const MyClass MyList[] = { 
  { 23012, 22, "Hamburger"} , 
  { 28375, 123, "Kieler"}
};

However, it seems that Java does not support such constructs. Is this correct? If yes, what is the best solution to this problem?

NOTE: The data consists of 2 tables with each about 50000 records of data, which is to be searched in various ways. This may require some indexes later, with significant more records, maybe 1 million records, saved this way. I expect the application to start up very fast, without iterating through these records.

like image 710
Lars D Avatar asked May 04 '10 07:05

Lars D


1 Answers

I personally wouldn't put it in source form.

Instead, include the data in some appropriate raw format in your jar file (I'm assuming you'll be packaging the application or library up) and use Class.getResourceAsStream or ClassLoader.getResourceAsStream to load it.

You may very well want a class to encapsulate loading, caching and providing this data - but I don't see much benefit from converting it into source code.

like image 50
Jon Skeet Avatar answered Oct 12 '22 12:10

Jon Skeet