首页 > 解决方案 > 代码对于 android studio 中的方法来说太大了

问题描述

作为https://stackoverflow.com/a/2407930/8970520的参考,有人告诉它使用属性文件来扩展分配的大小。但我不明白该怎么做。请您详细说明并解释它是如何做到的有效吗?在此先感谢。

标签: javaandroidandroid-studio

解决方案


你的代码是这样的:

public String[] checks(String qww){
    Map<String, String[]> bitmaps  = new HashMap<>();
    String[] cs;
    cs=new String[]{"1", "1", "1", "1", "1", ... 775 more bits ... };
    bitmaps.put("a",cs);
    cs=new String[]{"1", "1", "1", "1", "1", ... 775 more bits ... };
    bitmaps.put("A",cs);
    ... 49 more entries ...
    return bitmaps.get(qww);
}

这在很多层面上都是错误的:

  1. 将位存储为 1 个字符的字符串会浪费内存,因为它使用每个字符串超过 30 个字节,只是为了存储一个

    至少,您可以将位字符存储在char从单个字符串创建的数组中,例如

    Map<String, char[]> bitmaps  = new HashMap<>();
    bitmaps.put("a", "11111 ... 775 more bits ...".toCharArray());
    bitmaps.put("A", "11111 ... 775 more bits ...".toCharArray());
    ... 49 more entries ...
    return bitmaps.get(qww);
    
  2. 该代码在每次调用时构建整个bitmaps地图,然后获取其中一个地图值并丢弃该地图。浪费处理时间。

    要么只构建bitmaps一次地图,要么改用switch语句:

    public char[] checks(String qww) {
        switch (qww) {
            case "a": return "11111 ... 775 more bits ...".toCharArray();
            case "A": return "11111 ... 775 more bits ...".toCharArray();
            ... 49 more entries ...
            default: return null;
        }
    }
    
  3. 该数据量应该存储在代码之外,例如在属性文件中。

    当加载到内存中时,这些位应该存储在一个BitSet

因此,首先创建一个属性文件,例如checks.bitmaps. 名称可以是任何东西,它只是一个文本文件。内容:

a = 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111000000000111111110000000000000001111000000000000000001110000000000000000001100000000000000000001000111111110000000010111111111110000000011111111111110000000111100000000000000000000000000000000000000000000000000000000000000000000000000000000000111110000000000000111111110000000000001111111100000000000011111110000000000000111111000000000000000111100000000000000000000000000000000000000000000000000000000000000100000001000000000111000000011000000111110000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
A = 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110000000011111111100000000000001111110000000000000000111100000000000000001111000001111100000001110011111111100000011101111111111100000111111111111111000001111111111111110000001111100000000000000011100000000000000000100000000000000000001000000000000000000000000000111111000000000000111111110000000000001111111100000000000011111110000000000000111111100000000000000111100000000010000000000000000000100000000000000000001100000000000100000011110000000111000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
... 49 more entries ...

这样做的另一个优点是数据更容易查看和编辑。

更改代码如下:

class Main {
    private static final Map<String, BitSet> bitmaps = new HashMap<>();
    static {
        try (InputStream inStream = Main.class.getResourceAsStream("checks.bitmaps")) {
            Properties props = new Properties();
            props.load(inStream);
            for (String key : props.stringPropertyNames()) {
                char[] bits = props.getProperty(key).toCharArray();
                BitSet bitmap = new BitSet(bits.length);
                for (int i = 0; i < bits.length; i++)
                    bitmap.set(i, bits[i] == '1');
                bitmaps.put(key, bitmap);
            }
        } catch (Exception e) {
            throw new IllegalStateException("Bitmap file failed to load: " + e, e);
        }
    }
    public BitSet checks(String qww) {
        return bitmaps.get(qww);
    }
}

调用者现在可以检查 1 位,如下所示:

BitSet bitmap = main.checks(qww);
if (bitmap.get(index)) {
    // Bit is '1'
} else {
    // Bit is '0'
}

推荐阅读