-3

As a reference from the https://stackoverflow.com/a/2407930/8970520 It was told to use properties file to extend the allocated size .But i couldn't understand how to do that.Could you please elaborate and explain how it works?Thanks in advance.

sandeysh
  • 61
  • 1
  • 6

2 Answers2

1

In this case, using the properties file to extend the size is the wrong approach. Some people have already pointed this out, but you really, really don't want to include data in the source code. If the data changes, you would have to recompile your application and do a re-deploy of your app.

If the data does not change frequently, you could consider an XML file or SQLite database. If the data changes, though, you'll definitely need a mechanism to update it (even if the data changes rarely). Perhaps consider a web service.

If the data changes frequently, you'll definitely want to consider a web service to download the data.

1

Your code is like this:

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

That is just wrong on so many levels:

  1. Storing bits as 1-character strings is a waste of memory, since it uses more than 30 bytes per string, just to store a bit.

    At the very least, you could have stored the bit-characters in a char array, created from a single string, e.g.

    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. The code builds up the entire bitmaps map on every call, then gets one of the map values and discards the map. Waste of processing time.

    Either build the bitmaps map once only, or use a switch statement instead:

    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. That amount of data should be stored outside the code, e.g. in a property file.

    When loaded into memory, the bits should be stored in a BitSet

So, first create a property file, e.g. checks.bitmaps. The name can be anything, it is just a text file. Content:

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

Another advantage of this is that the data is so much easier to view and edit.

Change the code as follows:

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

The caller can now check for 1-bits as follows:

BitSet bitmap = main.checks(qww);
if (bitmap.get(index)) {
    // Bit is '1'
} else {
    // Bit is '0'
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Which is the best way out of three ways in my case? @Andreas – sandeysh Jul 21 '20 at 13:15
  • @sandeysh **All 3!** --- 1) the bits are stored as a single string of 0's and 1's, 2) the `bitmaps` map is only built once, 3) the data for the `bitmaps` map is stored outside the class. --- So, **the second half of the answer**, that's why I spent time writing all that. – Andreas Jul 21 '20 at 13:20