Wednesday, 28 August 2013

Will value objects in a copy of a static ConcurrentHashMap reference the same value objects as the original?

Will value objects in a copy of a static ConcurrentHashMap reference the
same value objects as the original?

I have a two part question.
I have: private static ConcurrentHashMap<Integer, Servers> servers= null;
which I later populate. In method getAllServers, I'm doing this:
public static synchronized ConcurrentHashMap<Integer, Server>
getAllServers() {
ConcurrentHashMap<Integer, Server> toReturn = new ConcurrentHashMap<>();
for(Integer i = 0; i < servers.size(); i ++ ) {
Server s = servers.get(i);
if(s.isAlive()) {
s.incrementConns();
toReturn.put(i, s);
}
}
return toReturn;
}
Q: Will modifications to s be reflected in servers?
Q: Will toReturn reference the same Server objects as in servers? If so,
would any class that creates and modifies getAllServers's returned map be
modifiying the same objects as servers?

No comments:

Post a Comment