从映射对象中获取键和值并将它们发送到不同的服务器
我有一个客户端-服务器体系结构,其中有一个客户端连接到中央服务器,然后该中央服务器连接到两个服务器(Server1 和 Server2)。我使用 Serialized 类将 Map 对象从客户端传递到 CentralServer。然后,在中央服务器中我想获取这个映射对象并从中获取键和值。为此,我使用以下代码:
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
}
}
如果我错了,请纠正我。我是否正在做正确获取密钥和值的第一部分?
我的问题的第二部分是如何将例如以“af”开头的密钥发送到 Server1 并将以“fz”开头的密钥发送到 Server2。我怎样才能做到这一点?
在我的 main 中,我有以下代码:
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();
这是我从客户端获取 MapObject 的地方。现在我想从这个对象中获取键和值。 此致
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的问题是关于“有序”映射,那么您可以查看 LinkedHashMap (如果您关心插入顺序)或 TreeMap (在其中定义compareTo())方法有排序规则)。
然后您可以简单地使用方法循环键
,然后您可以迭代(先前排序的)键集。
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
And then you can iterate over the (previously ordered) Set of keys.
您能澄清一下您的问题是什么吗?您说您想获取键和值,但看起来您在代码中同时执行了这两项操作。您能指出代码没有执行您想要的操作的问题吗?
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?