从映射对象中获取键和值并将它们发送到不同的服务器

发布于 2024-12-29 08:35:17 字数 1122 浏览 1 评论 0原文

我有一个客户端-服务器体系结构,其中有一个客户端连接到中央服务器,然后该中央服务器连接到两个服务器(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落叶缤纷 2025-01-05 08:35:17

如果您的问题是关于“有序”映射,那么您可以查看 LinkedHashMap (如果您关心插入顺序)或 TreeMap (在其中定义compareTo())方法有排序规则)。
然后您可以简单地使用方法循环键

Set keys = map.keySet();

,然后您可以迭代(先前排序的)键集。

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.

装纯掩盖桑 2025-01-05 08:35:17

您能澄清一下您的问题是什么吗?您说您想获取键和值,但看起来您在代码中同时执行了这两项操作。您能指出代码没有执行您想要的操作的问题吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文