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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:32:41+00:00 2026-06-06T00:32:41+00:00

I have a problem concerning a HashMap in Java. To explain the problem in

  • 0

I have a problem concerning a HashMap in Java. To explain the problem in a detailed way, i will first post some code you can refer to.

public void BLASTroute(String args[]) throws IOException, InterruptedException{
            ...
            correctMapping CM = new correctMapping();
            CM.correctMapping(RB.BLASTresults, exists);
            CalculateNewConsensusSequence CNCS = 
                new CalculateNewConsensusSequence();
            char[] consensus = CNCS.calculateNewConsensusSequence(
                CM.newSeq, CM.remindGaps, EMBLreaderReference.sequence, exists);

            HashMap<Integer, ArrayList<String>> gapsFused = 
                new HashMap<Integer, ArrayList<String>>();
            for (Integer i : CM.remindGaps.keySet()) {
                ArrayList<String> newList = CM.remindGaps.get(i);
                gapsFused.put(i, newList);
            }

            GenerateGeneLists GGL = new GenerateGeneLists(
                EMBLreaderReference, CM.newSeq, gapsFused, exists, 
                GQList, allMappedPositions);
            System.out.println(CM.remindGaps.hashCode());
            gapsFused=GGL.generateGeneListSNP(gapsFused);
            System.out.println(CM.remindGaps.hashCode());
            System.out.println(gapsFused.hashCode());
            GGL.generateGeneListFrameShift(gapsFused);

        }

The following occurs:

in my class correctMapping, i fill a global variable called remindGaps. I use it later in some functions, and nothing happens/everything works as expected.

Then, i make a copy of the HashMap called gapsFused (i don’t know if this has something to do with my problem).

Now comes the interesting part: In the class GenerateGeneLists, i don’t do a thing with the remindGaps HashMap.

However, after the function generateGeneListSNP is performed, remindGaps changed! I’ll post the code for you as well, so that you can help me better:

public GenerateGeneLists(EMBL_reader EMBLreaderReference,
           HashMap<String,ArrayList<String>> newSeq,
           HashMap<Integer,ArrayList<String>> gapsFused, File exists,
           ArrayList<GeneQualifier> GQlist, 
           HashMap<Integer,Integer> allMappedPositions) 
throws InterruptedException{
     this.EMBLreaderReference=EMBLreaderReference;
     this.newSeq=newSeq;
     //this.gapsFused=gapsFused;
     this.exists=exists;
     this.GQlist=GQlist;
     this.allMappedPositions=allMappedPositions;
     for (GeneQualifier GQ : this.GQlist){
        startlist.add(GQ.start);
        stoplist.add(GQ.stop);
        startMap.put(GQ.start,GQ);
    }
}

public HashMap<Integer,ArrayList<String>> generateGeneListSNP(
           HashMap<Integer,ArrayList<String>> gapsFused) 
throws IOException{
    File GQSNP = new File (exists+"/GQsnp.txt");
    BufferedWriter SNP = new BufferedWriter(new FileWriter(GQSNP));
    SNP.write("#Gene_start\tGene_stop\tlocus_tag\tproduct" + 
              "\tputative_SNP_positions(putative_changes)\n");
    HashMap<GeneQualifier,ArrayList<Integer>> GQreminder = 
        new HashMap<GeneQualifier,ArrayList<Integer>>();
    for (String s : newSeq.keySet()){
        ArrayList<String> blub = newSeq.get(s);
        char[] qrySeq = blub.get(0).toCharArray();
        char[] refSeq = blub.get(1).toCharArray();
        int start = Integer.valueOf(blub.get(2));
        int stop = Integer.valueOf(blub.get(3));
        for (int i=0;i<refSeq.length;i++){
            if (qrySeq[i]!=refSeq[i]&&qrySeq[i]!='-'&&qrySeq[i]!='.'){
                if (mismatchList.containsKey(start+i)){
                    ArrayList<Character> blah = mismatchList.get(start+i);
                    blah.add(qrySeq[i]);
                    mismatchList.put(start+i, blah);
                }
                else {
                    ArrayList<Character> blah = new ArrayList<Character>();
                    blah.add(qrySeq[i]);
                    mismatchList.put(start+i,blah);
                }
            }
            else if (qrySeq[i]!=refSeq[i]&&(qrySeq[i]=='-'||qrySeq[i]=='.')){
                if (!gapsFused.containsKey(start+i)){
                    ArrayList<String> qwer = new ArrayList<String>();
                    qwer.add(String.valueOf(qrySeq[i]));
                    gapsFused.put(start+i,qwer);
                }
                else {
                    ArrayList<String> qwer = gapsFused.get(start+i);
                    qwer.add(String.valueOf(qrySeq[i]));
                    gapsFused.put(start+i,qwer);
                }
                if (!deletionPositionsAndCount.containsKey((start+i))){
                    int count = 1;
                    deletionPositionsAndCount.put(start+i, count);
                }
                else {
                    int count = deletionPositionsAndCount.get(start+i);
                    count = count+1;
                    deletionPositionsAndCount.put(start+i, count);
                }
            }
        }
    }
    for (Integer a : mismatchList.keySet()){
        for (int i=0;i<startlist.size();i++){
            int start = startlist.get(i);
            int stop = stoplist.get(i);
            if (a>=start && a<=stop){
                GeneQualifier GQ = startMap.get(start);
                if (!GQreminder.containsKey(GQ)){
                    ArrayList save = new ArrayList<Integer>();
                    save.add(a);
                    GQreminder.put(GQ,save);
                }
                else {
                    ArrayList save = GQreminder.get(GQ);
                    save.add(a);
                    GQreminder.put(GQ,save);
                }
                break;
            }
        }
    }
    for (GeneQualifier GQ : GQreminder.keySet()) {
        ArrayList<Integer> save = GQreminder.get(GQ);
        int start = GQ.start;
        int stop = GQ.stop;
        String locus_tag = 
            GQ.geneFeatures.get("locus_tag").get(0).replace("\n", "");
        String product = 
            GQ.geneFeatures.get("product").get(0).replace("\n", "");
        SNP.write(start + "\t" + stop + "\t" + locus_tag + 
                  "\t" + product + "\t");
        boolean end = false;
        for (int i = 0; i < save.size(); i++) {
            if (i==save.size()-1) end=true;

            int posi = save.get(i);
            SNP.write(posi + "(");
            ArrayList<Character> mismatches = mismatchList.get(posi);
            for (int j = 0; j < mismatches.size(); j++) {
                char snipp = mismatches.get(j);
                if (j == mismatches.size() - 1) {
                    SNP.write(snipp + ")");
                } else {
                    SNP.write(snipp + ",");
                }
            }
            if (end == false){
                SNP.write(",");
            }
        }
        SNP.write("\n");
    }
    SNP.close();
    return gapsFused;
}

As you can see, remindGaps is not used in this class, but still it undergoes changes. Do you have an idea why this is the case?

What I tested is, whether remindGaps changes if i manually change gapsFused (the made copy of the first HashMap). This is not the case, so i don’t think that the copying process went wrong (for example only points to the other HashMap or references it).

I would really appreciate your ideas and help in order to solve this problem.

  • 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-06T00:32:44+00:00Added an answer on June 6, 2026 at 12:32 am

    You have to remember that in Java all objects are passed as reference. So, when you did:

    ArrayList<String> newList = CM.remindGaps.get(i);
    

    you basically pointed newList to the same list as contained in the remindGaps map. Now, even though you work with the gapsFused, any changes to its values effect the same underlying list in the memory – to which both remindGaps and gapsFused are pointing.

    Change your copy code to the following and see if it makes a difference:

    ArrayList<String> newList = new ArrayList<String>(CM.remindGaps.get(i));
    

    By doing this, you are creating a new list that newList will be pointing to and thus the changes will be encapsulated.

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

Sidebar

Related Questions

i have the following problem concerning the index:: my source code:: <table style=width: 80%>
I would like to ask for some advices concerning my problem. I have a
I have problem concerning python packages and testing. I'm writing an application using wx
I have a problem concerning the width of a DialogPreference in Android. According to
i have a problem concerning a select query in MYSQL i have two different
I am using LaTeX and I have a problem concerning string manipulation. I want
i am currently working on a facebook game and i have a problem concerning
I have quite a problem concerning the use of relational database concepts in Delphi
I'm a beginner concerning socket programming and I have a problem when I send
I have a problem concerning folder structure of a built InstallShield suite project. I

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.