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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:09:43+00:00 2026-06-01T22:09:43+00:00

I am using LongIntParallelHashMultimap java code which I got as my previous question’s answer

  • 0

I am using LongIntParallelHashMultimap java code which I got as my previous question’s answer here. I save the map in the disk and load it in to memory. However, after loading the map in memory, when I execute the get method the code stops it’s execution and goes into never ending loop. Can anybody help me why is this happening and how to solve this ? Any hints will be helpful for me.

My code can be found here and also given in the below:

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Arrays;
import java.util.Random;
import java.nio.*;
import java.nio.channels.FileChannel;
import java.io.RandomAccessFile ;


public class Test{
public static void main(String args[]){

LongIntParallelHashMultimap abc = new LongIntParallelHashMultimap(10, "a.txt","b.txt");
Random randomGenerator = new Random();


for(int i = 1 ; i < 5 ; i++ ){
long b = (long) Math.floor(i/2) + 1 ;
int  c = randomGenerator.nextInt();
abc.put(b,c) ;
}

int[]tt = abc.get(1);
System.out.println(tt[0]);
abc.save();

abc.load();

int[]tt1 = abc.get(1);
System.out.println(tt1[0]);

 }
}

class LongIntParallelHashMultimap {

private static final long NULL = -1L ;

private final long[] keys;
private final int[] values;
private int size;
private int savenum = 0;
private String str1 = "";
private String str2 = "";

public LongIntParallelHashMultimap(int capacity, String st1, String st2 ) {
    keys = new long[capacity];
    values = new int[capacity];
     Arrays.fill(keys, NULL);
     savenum = capacity ;
     str1 = st1;
     str2 = st2;
}

public void load(){
try{
FileChannel channel2 = new RandomAccessFile(str1, "r").getChannel();
MappedByteBuffer mbb2 = channel2.map(FileChannel.MapMode.READ_ONLY, 0,     channel2.size());
 mbb2.order(ByteOrder.nativeOrder());
  assert mbb2.remaining() == savenum * 8;
 for (int i = 0; i < savenum; i++) {
long l = mbb2.getLong();
keys[i] = l ;
 }
channel2.close();

FileChannel channel3 = new RandomAccessFile(str2, "r").getChannel();
MappedByteBuffer mbb3 = channel3.map(FileChannel.MapMode.READ_ONLY, 0, channel3.size());
mbb3.order(ByteOrder.nativeOrder());
assert mbb3.remaining() == savenum * 8;
for (int i = 0; i < savenum; i++) {
long l = mbb3.getLong();
keys[i] = l ;
 }
channel3.close();
}

catch(Exception e){
        System.out.println(e) ;
    }
}

public void put(long key, int value) {
    int index = indexFor(key);
    while (keys[index] != NULL) {
        index = successor(index);
    }
    keys[index] = key;
    values[index] = value;
    ++size;
}

public int[] get(long key) {
    int index = indexFor(key);
    int count = countHits(key, index);
    int[] hits = new int[count];
    int hitIndex = 0;

    while (keys[index] != NULL) {
        if (keys[index] == key) {
            hits[hitIndex] = values[index];
            ++hitIndex;
        }
        index = successor(index);
    }

    return hits;
}

private int countHits(long key, int index) {
    int numHits = 0;
    while (keys[index] != NULL) {
        if (keys[index] == key) ++numHits;
        index = successor(index);
    }
    return numHits;
}

private int indexFor(long key) {
return Math.abs((int) ((key * 5700357409661598721L) % keys.length));
}

private int successor(int index) {
    return (index + 1) % keys.length;
}

public int size() {
    return size;
}

     public void save() {
    try {
        long l = 0 ;
 FileChannel channel = new RandomAccessFile(str1, "rw").getChannel();
 MappedByteBuffer mbb = channel.map(FileChannel.MapMode.READ_WRITE, 0, savenum * 8);
 mbb.order(ByteOrder.nativeOrder());

for(int i = 0 ; i < savenum ; i++){
l = keys[i] ;
  mbb.putLong(l);
}
channel.close();

FileChannel channel1 = new RandomAccessFile(str2, "rw").getChannel();
MappedByteBuffer mbb1 = channel1.map(FileChannel.MapMode.READ_WRITE, 0, savenum * 8);
mbb1.order(ByteOrder.nativeOrder());

for(int i = 0 ; i < savenum ; i++){
 l = values[i] ;
 mbb1.putLong(l);
 }
 channel1.close();
    }
catch (Exception e){
   System.out.println("IOException : " + e);
      }
    }
 }
  • 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-01T22:09:44+00:00Added an answer on June 1, 2026 at 10:09 pm

    Steping through the code with my debugger I can see that your load() sets the keys[] twice when you should be setting the keys[] and values[] (copy and paste error)

    When I change the second load the test appears to run fine.


    If you want to access the keys and values without creating a copy you can start with

    private final LongBuffer keys;
    private final IntBuffer values;
    private int size;
    private int savenum = 0;
    private final FileChannel channel1;
    private final FileChannel channel2;
    
    public LongIntParallelHashMultimap(int capacity, String st1, String st2) throws IOException {
        boolean newFile = !new File(st1).exists();
    
        channel1 = new RandomAccessFile(st1, "rw").getChannel();
        MappedByteBuffer mbb1 = channel1.map(FileChannel.MapMode.READ_WRITE, 0, capacity * 8);
        mbb1.order(ByteOrder.nativeOrder());
    
        keys = mbb1.asLongBuffer();
    
        channel2 = new RandomAccessFile(st2, "rw").getChannel();
        MappedByteBuffer mbb2 = channel2.map(FileChannel.MapMode.READ_WRITE, 0, capacity * 4);
        mbb2.order(ByteOrder.nativeOrder());
    
        values = mbb2.asIntBuffer();
    
        if (newFile)
            for(int i=0;i<capacity;i++)
                keys.put(i, NULL);
    
        savenum = capacity;
    }
    
    public void close() throws IOException {
        channel1.close();
        channel2.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Using LINQ on collections, what is the difference between the following lines of code?
Using android 2.3.3, I have a background Service which has a socket connection. There's
Using MVC2 I have an AJAX form which is posting to a bound model.
Using Delphi 2010. I am looking for (possibly) a function or procedure which can
Using the URL Rewrite module, I've got a rule setup that is defined as:
Using Rails 3.2.0.rc2 and ruby 1.9.3p0 In app/views/requests/_form.html.erb I have the following code for
Using the http://www.ifans.com/forums/showthread.php?t=132024 post from another question i am allowing the user to enter
Using Java, how can I extract all the links from a given web page?
Using the code available on the link: http://msdn.microsoft.com/en-us/library/aa969393.aspx HRESULT CreateLink(LPCWSTR lpszPathObj1, LPCSTR lpszPathLink, LPCWSTR

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.