Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a Map Object in Java

I tried to follow the same way as in <How do I copy an object in Java?>.

But it does not work with Map object in the following codes. I want to copy the original map data to currMap. The current output is

0
1
2
3
null
null
null

I want it to be

0
1
2
3
0
2
3

What am I missing here?

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;

class mapCopy{
    private Map<Character, Queue<Integer>> map;
    mapCopy(Map<Character, Queue<Integer>> map){
        this.map=map;
    }
    
    mapCopy(mapCopy mapcopy){
        this.map=mapcopy.map;
    }
    
    Map<Character, Queue<Integer>> getMap(){
        return this.map;
    }
}

public class Test {

    static Map<Character, Queue<Integer>> BuildMap(){
        String toMatch="able";
        Map<Character, Queue<Integer>> map = new HashMap<>();
        
        int i=0;
        for(var c:toMatch.toCharArray()) {
            Queue<Integer> q = map.get(c);
            if(q==null)
                q=new ArrayDeque<Integer>();
            
            q.add(i);
            map.put(c, q);          
            i++;
        }
        
        return map;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Map<Character, Queue<Integer>> map = BuildMap();
        
        List<String> dic = Arrays.asList("able", "ale");        
                        
        for(var d:dic) {
            var copy1 = new mapCopy(map);
            var copy2 = new mapCopy(copy1);
            
            var currMap = copy2.getMap();
            
            for(var c:d.toCharArray()) {        
                    System.out.println(currMap.get(c).poll());
            }
        }
    }

}

Update 1:

iota's answer is what I look for. Here are the actually codes implemented by adding a copyMap function and adding it to currMap (var currMap = copyMap(map);)

class mapCopy is not needed.

static Map<Character, Queue<Integer>> copyMap(Map<Character, Queue<Integer>> mapcopy){
    Map<Character, Queue<Integer>> map = new HashMap<>(mapcopy.size());
    mapcopy.forEach((k,v)->{
        map.put(k, new ArrayDeque<>(v));
    });
    
    return map;
}

Update 2:
Add complete codes

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public class Test {

    static Map<Character, Queue<Integer>> BuildMap(){
        String toMatch="able";
        Map<Character, Queue<Integer>> map = new HashMap<>();
        
        int i=0;
        for(var c:toMatch.toCharArray()) {
            Queue<Integer> q = map.get(c);
            if(q==null)
                q=new ArrayDeque<Integer>();
            
            q.add(i);
            map.put(c, q);          
            i++;
        }
        
        return map;
    }
    
    static Map<Character, Queue<Integer>> copyMap(Map<Character, Queue<Integer>> mapcopy){
        Map<Character, Queue<Integer>> map = new HashMap<>(mapcopy.size());
        mapcopy.forEach((k,v)->{
            map.put(k, new ArrayDeque<>(v));
        });
        
        return map;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Map<Character, Queue<Integer>> map = BuildMap();
        
        List<String> dic = Arrays.asList("able", "ale");        
                        
        for(var d:dic) {
            var currMap = copyMap(map);

            for(var c:d.toCharArray()) {        
                    System.out.println(currMap.get(c).poll());
            }
        }
    }
}
like image 273
Don Avatar asked Oct 15 '22 22:10

Don


1 Answers

You can iterate over the Map directly and copy each value into a new Map.

mapCopy(mapCopy mapcopy){
    this.map = new HashMap<>(mapcopy.map.size());
    mapcopy.map.forEach((k,v)->{
        map.put(k, new ArrayDeque<>(v));
    });
}
like image 185
Unmitigated Avatar answered Oct 18 '22 18:10

Unmitigated