Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

hashmap - 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());
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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));
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...