大量Java中的常量 [英] Large amount of constants in Java

查看:547
本文介绍了大量Java中的常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要包括约1兆字节数据的Java应用程序,在源$ C ​​$ C休息非常快速和方便地访问。我的主要背景是不是Java,所以我最初的想法是将数据直接转换为Java源代码code,定义常量数组的1M字节,类等等,这样的事情(而不是C ++结构):

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"}
};

不过,似乎Java不支持这样的构造。它是否正确?如果是的话,什么是这个问题的最佳解决方案?

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

注:数据由2的表与每对50000记录数据,这是将要搜索以各种方式。这可能需要一些指标后,有显著更多的记录,也许100万条记录,保存的这种方式。我预期该应用程序启动非常快,而无需通过这些记录进行迭代。

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.

推荐答案

我个人的的把它的源代码。

I personally wouldn't put it in source form.

相反,包括在你的jar文件中的一些相应的原始格式的数据(我假设你会被打包应用程序或库上),并使用<一个href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream%28java.lang.String%29"相对=nofollow> Class.getResourceAsStream 或<一href="http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getResourceAsStream%28java.lang.String%29"相对=nofollow> ClassLoader.getResourceAsStream 加载它。

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.

您很可能希望有一个类来封装加载,缓存和提供该数据 - 但我没有看到太多受益于将其转化为源$ C ​​$ C

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.

这篇关于大量Java中的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆