Reputation: 1022
I have a client-server architecture where there is a client connected to a Central Server and then I have this central Server connected to two Servers (Server1 and Server2). I pass a Map Object from the client to the CentralServer using the Serializable class. Then, in the CentralServer I want to get this Map Object and get the Key and the value from them. For that I am using the following code:
public void readObject(Map<String, byte[]> parObj)
{
for (String key : keys)
{
if(parObj.containsKey(key) == true){
foundAnyKey = true;
byte[] values = parObj.get(key);
//Do Something here
}
}
if(foundAnyKey == true){
//Do something here
}
}
Correct me if I'm wrong. Am I doing this first part of getting the key and the value right?
The second part of my question is How can I send the keys that for instance begin with "a-f" to the Server1 and the keys that begin in "f-z" to the Server2. How can I do that?
In my main I have the following code:
System.out.println("Starting Server");
ServerSocket ss = new ServerSocket(7776);
System.out.println("Server Started");
Socket s = ss.accept();
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
MapObject mo = (MapObject) ois.readObject();
s.close();
This is where I get my MapObject form the client. Now I want to get the key and the value from this object. Best regards
Upvotes: 0
Views: 116
Reputation: 2406
If you question is about an "ordered" map then you take a look to LinkedHashMap (if you care about insertion order) or a TreeMap (where you define a compareTo() method to have a sorting rule). Then you can loop the keys simply with the method
Set keys = map.keySet();
And then you can iterate over the (previously ordered) Set of keys.
Upvotes: 1
Reputation: 7860
Can you clarify what the problem is in your question? You say you want to get the key and the value, but it looks like you're doing both in the code. Can you point out the problem where the code isn't doing what you want it to do?
Upvotes: 0