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 8362691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:59:55+00:00 2026-06-09T11:59:55+00:00

I am trying to write a code for a custom hashmap (array of linked

  • 0

I am trying to write a code for a custom hashmap (array of linked lists) which can store 500 millions of values (keys are linked list array number) and can save the index to the disk. The code is following:

  • 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-09T11:59:56+00:00Added an answer on June 9, 2026 at 11:59 am

    This is how I would implement it

    import java.io.File;
    import java.io.IOException;
    import java.nio.*;
    import java.nio.channels.FileChannel;
    import java.io.RandomAccessFile;
    import java.util.Arrays;
    
    class LongIntParallelHashMultimap {
        public static final int BUFFER_BITS = 24;
        private final FileChannel fc;
        private final ByteBuffer[] keys, data;
        private final int topBits, topMask, offsetMask;
    
        public LongIntParallelHashMultimap(String fileName, int sizeBits, boolean load) throws IOException {
            fc = new RandomAccessFile(fileName, "rw").getChannel();
            long totalSize = 4L << sizeBits;
            int bufferIndex = (int) (totalSize >> BUFFER_BITS);
            keys = new ByteBuffer[bufferIndex];
            data = new ByteBuffer[bufferIndex];
            int bufferSize = 1 << BUFFER_BITS;
            long offset = 0;
            for (int i = 0; i < bufferIndex; i++) {
                MappedByteBuffer kmap = fc.map(FileChannel.MapMode.READ_WRITE, offset, bufferSize);
                kmap.load();
                keys[i] = kmap.order(ByteOrder.nativeOrder());
                MappedByteBuffer dmap = fc.map(FileChannel.MapMode.READ_WRITE, offset + bufferSize, bufferSize);
                dmap.load();
                data[i] = dmap.order(ByteOrder.nativeOrder());
                offset += bufferSize * 2;
            }
            topBits = sizeBits + 2 - BUFFER_BITS;
            topMask = (1 << topBits) - 1;
            offsetMask = bufferSize - 4;
        }
    
        public void put(int key, int value) {
            int buffer = key & topMask;
            int key2 = (key >> topBits) + 1;
            assert key2 != 0;
            ByteBuffer keys2 = keys[buffer];
            ByteBuffer data2 = data[buffer];
            int offset = (key2 * 101) & offsetMask;
            while (keys2.getInt(offset) != 0) {
                offset += 3 * 4;
                offset &= offsetMask;
            }
            keys2.putInt(offset, key2);
            data2.putInt(offset, value);
        }
    
        public int get(int key, int[] values) {
            int buffer = key & topMask;
            int key2 = (key >> topBits) + 1;
            assert key2 != 0;
            ByteBuffer keys2 = keys[buffer];
            ByteBuffer data2 = data[buffer];
            int offset = (key2 * 101) & offsetMask;
            for (int count = 0; count < values.length; ) {
                int key3 = keys2.getInt(offset);
                if (key3 == 0)
                    return count;
                if (key3 == key2)
                    values[count++] = data2.getInt(offset);
    
                offset += 3 * 4;
                offset &= offsetMask;
            }
            return values.length;
        }
    
        private final int[] getValues = new int[1000];
        private static final int[] NO_VALUES = { };
    
        public int[] get(int key) {
            int len = get(key, getValues);
            return len == 0 ? NO_VALUES : Arrays.copyOf(getValues, len);
        }
    
        public static void main(String... args) throws IOException {
            int keys = 500 * 1000 * 1000;
    
            new File("abc.bin").delete();
            long startTime = System.nanoTime();
            LongIntParallelHashMultimap lph = new LongIntParallelHashMultimap("abc.bin", 30, true);
            long time = System.nanoTime() - startTime;
            System.out.printf("Load time was %.3f sec%n", time / 1e9);
    
            timePut(keys, lph);
    
            timeGet(keys, lph);
    
            timeGet2(keys, lph);
    
            startTime = System.nanoTime();
            System.gc();
            time = System.nanoTime() - startTime;
            System.out.printf("Time to Full GC was %.3f sec%n", time / 1e9);
        }
    
        private static void timePut(int keys, LongIntParallelHashMultimap lph) {
            long startTime;
            long time;
            startTime = System.nanoTime();
            for (int i = 0; i < keys; i++) {
                lph.put(i, i + 100);
                if ((i + 1) % 100_000_000 == 0)
                    System.out.printf("%,d ", i + 1);
            }
            time = System.nanoTime() - startTime;
            System.out.printf("%nput time was %.3f sec%n", time / 1e9);
        }
    
        private static void timeGet(int keys, LongIntParallelHashMultimap lph) {
            long startTime;
            long time;
            startTime = System.nanoTime();
            int[] values = new int[2];
            for (int i = 0; i < keys; i++) {
                lph.get(i, values);
                if ((i + 1) % 100_000_000 == 0)
                    System.out.printf("%,d ", i + 1);
            }
            time = System.nanoTime() - startTime;
            System.out.printf("%nget(key, values) time was %.3f sec%n", time / 1e9);
        }
    
        private static void timeGet2(int keys, LongIntParallelHashMultimap lph) {
            long startTime;
            long time;
            startTime = System.nanoTime();
            for (int i = 0; i < keys; i++) {
                lph.get(i);
                if ((i + 1) % 100_000_000 == 0)
                    System.out.printf("%,d ", i + 1);
            }
            time = System.nanoTime() - startTime;
            System.out.printf("%nget(key) time was %.3f sec%n", time / 1e9);
        }
    }
    

    Note: there is no additional step to load or save the data. This means when you return from put(key, value) the data is saved even if you program crashes (assuming your OS doesn’t crash)

    This adds 500m entries. run with -ea -verbosegc -mx16m That’s a maximum heap of 16 MB.

    Load time was 0.121 sec
    100,000,000 200,000,000 300,000,000 400,000,000 500,000,000 
    put time was 47.017 sec
    100,000,000 200,000,000 300,000,000 400,000,000 500,000,000 
    get(key, values) time was 50.858 sec
    [ GC goes bananas for get(key) ]
    100,000,000 200,000,000 300,000,000 400,000,000 500,000,000 
    get(key) time was 87.634 sec
    Time to Full GC was 0.015 sec
    

    Note: it only GCed because I called System.gc(); and look at the size of the heap used!


    I would use a int[] instead of a linked list of nodes.

    import sun.nio.ch.DirectBuffer;
    
    import java.io.IOException;
    import java.util.*;
    import java.nio.*;
    import java.nio.channels.FileChannel;
    import java.io.RandomAccessFile;
    
    class LongIntParallelHashMultimap {
        private static final int[] NO_INTS = {};
    
        final int[][] data;
    
        public LongIntParallelHashMultimap() {
            data = new int[Integer.MAX_VALUE][];
        }
    
        public void put(int key, int value) {
            int[] ints = data[key];
            if (ints == null) {
                data[key] = new int[]{value};
            } else {
                int[] ints2 = Arrays.copyOf(ints, ints.length + 1);
                ints2[ints.length] = value;
                data[key] = ints2;
            }
        }
    
        public int[] get(int key) {
            int[] ints = data[key];
            return ints == null ? NO_INTS : ints;
        }
    
        private FileChannel channel;
        private MappedByteBuffer mbb;
    
        public void save() throws IOException {
            channel = new RandomAccessFile("abc.bin", "rw").getChannel();
            mbb = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1 << 24);
            mbb.order(ByteOrder.nativeOrder());
    
            for (int i = 0; i < Integer.MAX_VALUE - 32; i += 32) {
                int bits = 0;
                for (int j = 0; j < 32; j++) {
                    if (data[i + j] != null) bits |= 1;
                    bits <<= 1;
                }
                getMbb().putInt(bits);
            }
    
            for (int i = 0; i < Integer.MAX_VALUE; i++) {
                int arr[] = get(i);
                if (arr.length == 0) continue;
                getMbb().putInt(arr.length);
                for (int a : arr)
                    getMbb().putInt(a);
            }
            channel.close();
            cleanMbb();
        }
    
        private ByteBuffer getMbb() throws IOException {
            if (mbb.remaining() <= 0) {
                cleanMbb();
                mbb = channel.map(FileChannel.MapMode.READ_WRITE, channel.size(), 1 << 24);
                mbb.order(ByteOrder.nativeOrder());
            }
            return mbb;
        }
    
        private void cleanMbb() {
            ((DirectBuffer) mbb).cleaner().clean();
        }
    
    
        public static void main(String... args) throws IOException {
            int keys = 50 * 1000 * 1000;
    
            long startTime = System.nanoTime();
            LongIntParallelHashMultimap lph = new LongIntParallelHashMultimap();
            long time = System.nanoTime() - startTime;
            System.out.printf("Create time %.3f sec%n", time / 1e9);
    
            startTime = System.nanoTime();
            for (int i = 0; i < keys; i++) {
                lph.put(i, i + 100);
                if (i % 10000000 == 0 && i != 0)
                    System.out.print(" " + i + " ");
            }
            time = System.nanoTime() - startTime;
            System.out.printf("%nput time was %.3f sec%n", time / 1e9);
    
            startTime = System.nanoTime();
            lph.save();
            time = System.nanoTime() - startTime;
            System.out.printf(" time to save was %.3f sec%n", time / 1e9);
    
            startTime = System.nanoTime();
            for (int i = 0; i < keys; i++) {
                int k[] = lph.get(i);
            }
            time = System.nanoTime() - startTime;
            System.out.printf("get time was %.3f sec%n", time / 1e9);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a custom code generator that can parse stored procedures.
Alright, here I am again trying to write code from scratch and I can't
I'm trying to write a code to actually sort my array in an ascending
I am trying to write a simple custom button in wx.Python. My code is
I'm trying to write a custom FxCop code analysis rule that will warn developers
I was trying to write a custom caching mechanism for my ajax calls, which
I'm trying to implement a custom TrackingParticipant for WF 4. I can write the
I'm trying to write custom authentication domain service. I think I understood all code
I am trying to write an app that uses a custom keyboard. How can
I'm trying to write an custom ruleset.xml for php code sniffer but calling it

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.