Loading a map using Properties class

前端 未结 2 1353
甜味超标
甜味超标 2020-11-30 15:55

I have a map with 75000 entries and each entry value will be of size 10kb on average.

I load this map into memory using Properties class . But due to the size of the

相关标签:
2条回答
  • 2020-11-30 16:11

    I assume you don't need to load all the properties at the same time, but you rather need to iterate through all properties. Personally, I would go with parsing manually the file line by line and working in a stream-like fashion. If there is a lib for processing very large property I don't know it.

    0 讨论(0)
  • 2020-11-30 16:19

    Don't use Properties, which is legacy

    1. Divide entries into multiple files

    2. Read each file in sequence, load and process using Preferences

    Example code:

    package com.mypack.test;
    
    import java.io.*;
    import java.util.*;
    import java.util.prefs.Preferences;
    
    public class PreferencesExample {
    
        public static void main(String args[]) throws FileNotFoundException {
            Preferences ps = Preferences.userNodeForPackage(PreferencesExample.class);
            // Load file object
            File fileObj = new File("d:\\data.xml");
            try {
                FileInputStream fis = new FileInputStream(fileObj);
                ps.importPreferences(fis);
                System.out.println("Prefereces:"+ps);
                System.out.println("Get property1:"+ps.getInt("property1",10));
    
            } catch (Exception err) {
                err.printStackTrace();
            }
        }
    }
    

    Sample xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE preferences SYSTEM 'http://java.sun.com/dtd/preferences.dtd'>
    <preferences EXTERNAL_XML_VERSION="1.0">
    <root type="user">
    <map />
    <node name="com">
      <map />
      <node name="mypack">
        <map />
        <node name="test">
          <map>
            <entry key="property1" value="80" />
            <entry key="property2" value="Red" />
          </map>
        </node>
      </node>
    </node>
    </root>
    </preferences>
    
    0 讨论(0)
提交回复
热议问题