V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
gaopinsong
V2EX  ›  Java

当使用一个复杂的 final class 来作为 final static string 常量的保存地。性能是否有影响

  •  
  •   gaopinsong · 2015-05-02 15:54:36 +08:00 · 2483 次点击
    这是一个创建于 3253 天前的主题,其中的信息可能已经有所发展或是发生改变。

    主要问题是,需要保存非常大量的字符串常量。

    我希望可以把这些常量管理起来。所以现在是使用大量的内部类管理。

    不仅包含大量的内部类。内部类中还包含内部类。不知道这样会不会影响性能问题。

    示例:
    ```

    public final class WmsUrlConstants {

    public static final String URL_WMS_DEFAULT_INDEX = "/wms/default/index";
    
    public static final class Transfer {
    
        public static final String search = "/wms/transfer/list/search";
    
        public static final String delete = "/wms/transfer/list/delete";
    
        public static final String get = "/wms/transfer/edit/getTransfer";
    
        public static final String submit = "/wms/transfer/edit/submitTransfer";
    
        public static final String save = "/wms/transfer/edit/saveTransfer";
    
        public static final String create = "/wms/transfer/edit/create";
    
        public static final class Package {
    
            public static final String save = "/wms/transfer/edit/savePackage";
    
            public static final String delete = "/wms/transfer/edit/deletePackage";
    
            public static final class Item {
    
                public static final String select = "/wms/transfer/edit/getPackageItems";
            }
        }
    
        public static final class Product {
    
            public static final class Sku {
    
                public static final String get = "/wms/transfer/edit/getSkuByCode";
            }
        }
    
        public static final class Store {
    
            public static final String all = "/wms/transfer/edit/getAllWarehouses";
        }
    }
    

    }

    7 条回复    2015-05-12 08:16:04 +08:00
    Septembers
        1
    Septembers  
       2015-05-02 16:04:06 +08:00
    HashMap更合适如果觉得性能差可以考虑用Trie(Prefix Tree)实现Key的匹配
    Septembers
        2
    Septembers  
       2015-05-02 16:07:43 +08:00
    内部类 JDK会拍平
    字节码大概会变成 Transfer$Product$Sku 这样
    9hills
        3
    9hills  
       2015-05-02 17:03:09 +08:00 via iPhone
    Python 的话一个字典就好了
    SoloCompany
        4
    SoloCompany  
       2015-05-02 17:39:23 +08:00 via Android
    字符串常量会被直接内联,多余的担心
    SoloCompany
        5
    SoloCompany  
       2015-05-02 17:40:48 +08:00 via Android
    java 没有 define,这种字符串常量的定义方法就是官方指定的和define类似的替代
    smithtel
        6
    smithtel  
       2015-05-06 09:08:31 +08:00
    这些是直接放入常量池中,不会有性能问题,相反放入其他比如list中,还需要对其维护。
    hellomsg
        7
    hellomsg  
       2015-05-12 08:16:04 +08:00
    @9hills python婊哪都是?真烦人
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   950 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 21:21 · PVG 05:21 · LAX 14:21 · JFK 17:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.