Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8972215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:09:50+00:00 2026-06-15T18:09:50+00:00

I have a 1.7G file with the following format: String Long String Long String

  • 0

I have a 1.7G file with the following format:

String Long String Long String Long String Long ... etc

Essentially, String is a key and Long is a value in a hashmap i’m interested in initialising before running anything else in my application.

My current code is:

  RandomAccessFile raf=new RandomAccessFile("/home/map.dat","r");
                raf.seek(0);
                while(raf.getFilePointer()!=raf.length()){
                        String name=raf.readUTF();
                        long offset=raf.readLong();
                        map.put(name,offset);
                }

This takes about 12 mins to complete and I’m sure there are better ways of doing this so I would appreciate any help or pointer.

thanks


Update as in EJP suggestion?

EJP thank you for your suggestion and I hope this is what you meant. Correct me if this is wrong

DataInputStream dis=null;
    try{
     dis=new DataInputStream(new BufferedInputStream(new FileInputStream("/home/map.dat")));
     while(true){
       String name=dis.readUTF();
       long offset=dis.readLong();
       map.put(name, offset);
     }
    }catch (EOFException eofe){
      try{
        dis.close();
      }catch (IOException ioe){
        ioe.printStackTrace();
      }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:09:52+00:00Added an answer on June 15, 2026 at 6:09 pm

    I would construct the file so it can be used in place. i.e. without loading this way. As you have variable length records you can construct an array of the location of each record, then place the key in order so you can perform a binary search for data. (Or you can use a custom hash table) You can then wrap this with method which hide the fact the data is actually store in a file instead of turned into data objects.

    If you do all this the “load” phase becomes redundant and you won’t need to create so many objects.


    This is a long example but hopefully shows what is possible.

    import vanilla.java.chronicle.Chronicle;
    import vanilla.java.chronicle.Excerpt;
    import vanilla.java.chronicle.impl.IndexedChronicle;
    import vanilla.java.chronicle.tools.ChronicleTest;
    
    import java.io.IOException;
    import java.util.*;
    
    public class Main {
        static final String TMP = System.getProperty("java.io.tmpdir");
    
        public static void main(String... args) throws IOException {
            String baseName = TMP + "/test";
            String[] keys = generateAndSave(baseName, 100 * 1000 * 1000);
    
            long start = System.nanoTime();
            SavedSortedMap map = new SavedSortedMap(baseName);
            for (int i = 0; i < keys.length / 100; i++) {
                long l = map.lookup(keys[i]);
    //            System.out.println(keys[i] + ": " + l);
            }
            map.close();
            long time = System.nanoTime() - start;
    
            System.out.printf("Load of %,d records and lookup of %,d keys took %.3f seconds%n",
                    keys.length, keys.length / 100, time / 1e9);
        }
    
        static SortedMap<String, Long> generateMap(int keys) {
            SortedMap<String, Long> ret = new TreeMap<>();
            while (ret.size() < keys) {
                long n = ret.size();
                String key = Long.toString(n);
                while (key.length() < 9)
                    key = '0' + key;
                ret.put(key, n);
            }
            return ret;
        }
    
        static void saveData(SortedMap<String, Long> map, String baseName) throws IOException {
            Chronicle chronicle = new IndexedChronicle(baseName);
            Excerpt excerpt = chronicle.createExcerpt();
            for (Map.Entry<String, Long> entry : map.entrySet()) {
                excerpt.startExcerpt(2 + entry.getKey().length() + 8);
                excerpt.writeUTF(entry.getKey());
                excerpt.writeLong(entry.getValue());
                excerpt.finish();
            }
            chronicle.close();
        }
    
        static class SavedSortedMap {
            final Chronicle chronicle;
            final Excerpt excerpt;
            final String midKey;
            final long size;
    
            SavedSortedMap(String baseName) throws IOException {
                chronicle = new IndexedChronicle(baseName);
                excerpt = chronicle.createExcerpt();
                size = chronicle.size();
                excerpt.index(size / 2);
                midKey = excerpt.readUTF();
            }
    
            // find exact match or take the value after.
            public long lookup(CharSequence key) {
                if (compareTo(key, midKey) < 0)
                    return lookup0(0, size / 2, key);
                return lookup0(size / 2, size, key);
            }
    
            private final StringBuilder tmp = new StringBuilder();
    
            private long lookup0(long from, long to, CharSequence key) {
                long mid = (from + to) >>> 1;
                excerpt.index(mid);
                tmp.setLength(0);
                excerpt.readUTF(tmp);
                if (to - from <= 1)
                    return excerpt.readLong();
                int cmp = compareTo(key, tmp);
                if (cmp < 0)
                    return lookup0(from, mid, key);
                if (cmp > 0)
                    return lookup0(mid, to, key);
                return excerpt.readLong();
            }
    
            public static int compareTo(CharSequence a, CharSequence b) {
                int lim = Math.min(a.length(), b.length());
                for (int k = 0; k < lim; k++) {
                    char c1 = a.charAt(k);
                    char c2 = b.charAt(k);
                    if (c1 != c2)
                        return c1 - c2;
                }
                return a.length() - b.length();
            }
    
            public void close() {
                chronicle.close();
            }
        }
    
        private static String[] generateAndSave(String baseName, int keyCount) throws IOException {
            SortedMap<String, Long> map = generateMap(keyCount);
            saveData(map, baseName);
            ChronicleTest.deleteOnExit(baseName);
    
            String[] keys = map.keySet().toArray(new String[map.size()]);
            Collections.shuffle(Arrays.asList(keys));
            return keys;
        }
    }
    

    generates 2 GB of raw data and performs a million lookups. It’s written in such a way that the loading and lookup uses very little heap. ( << 1 MB )

    ls -l /tmp/test*
    -rw-rw---- 1 peter peter 2013265920 Dec 11 13:23 /tmp/test.data
    -rw-rw---- 1 peter peter  805306368 Dec 11 13:23 /tmp/test.index
    
    /tmp/test created.
    /tmp/test, size=100000000
    Load of 100,000,000 records and lookup of 1,000,000 keys took 10.945 seconds
    

    Using a hash table lookup would be faster per lookup as it is O(1) instead of O(ln N), but more complex to implement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several file with the following format: lat,lon,value. I would like to plot
Greetings, I have file with the following strings: string.Format({0},{1}, Having \Two\ On The Same
I have an example string parsed from a log file in the following format:
I have a HashMap in the following format. HashMap<String, List<String>> map I'm trying without
I have a multiline TSV file with the following format: Type\tBasic Name\tAttribute\tA Long Description\n
I have the following entry in a properties file: some.key = \n [1:Some value]
I have a file with 200mb, the file with following format each line is
I have a file containing the following content 1000 line in the following format:
I have a file that has the following format: 12345 TAB_HERE Name : The
I have an xml file in the following format: <food> <desert> cake <desert> </food>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.