Java 的哈希表 - 如何获取任何条目

发布于 2024-12-23 20:39:27 字数 293 浏览 0 评论 0原文

我正在开发一个聊天服务器,并将客户端放入哈希表中。 该哈希表由 , 组成,其中 Connection 具有 Socket 和输入输出流。

我可以发送消息,只是在哈希表中查找缺口,但如何将其发送给所有人呢?

我可以“侦察”(这是一个未知术语)每个哈希表的条目吗? (就像一个数组,我想“侦察”每个条目,所以我将执行一个循环并将消息发送给每个人)。

提前致谢。

I'm working on a chat server, and I'm putting the Clients into a Hashtable.
This Hashtable is composed by <String name, Connection c>, where Connection has Socket and in-out flows.

I can send messages just looking for a nick in the Hashtable, but how can I send it to all the people?

Can I "Scout" (this was the unknown term) every Hashtable's entry? (like an array, I want to "SCOUT" each entry, so I'll do a loop and I'll send the message to everyone).

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

邮友 2024-12-30 20:39:27

您可以通过阅读 javadocs 来回答您自己的问题 <代码>HashMap。 “阅读 javadocs”是重要的一课每个 Java 初学者都应该学习和记住

在本例中,javadoc 将向您展示 3 种可能有用的方法:

  • keys() 方法返回由表中的键组成的集合。
  • values() 方法返回一个由表中的值组成的集合。
  • entries() 方法返回一个表示表中键/值对的集合。

您可以像任何其他集合一样迭代这些集合。其他答案里有例子。


但是,我的印象是您的应用程序是多线程的。如果是这种情况,则需要处理其他两个问题才能使程序可靠:

  1. 如果两个或多个线程可以使用相同的对象或数据结构,则它们需要采取必要的步骤来确保它们已正确同步。如果不这样做,则某些操作序列将导致数据结构进入不一致状态的非零概率,或者一个或多个线程将看到< /em> 不一致的状态(由于内存缓存、寄存器中保存的值等)。

  2. 线程正在使用 HashMap 的集合迭代器之一,而另一个线程添加或删除条目,则第一个线程可能会收到 ConcurrentModificationException

  3. 如果您通过在“发送给所有人”操作进行时锁定 HashMap 上的所有其他操作来解决上述两个问题,则会无意中造成性能瓶颈。基本上,其他一切都会停止,直到操作完成。如果您简单地在 HashMap 周围放置一个同步包装器,您会得到类似的效果(但规模更小)。

您需要阅读并了解这些内容。 (在一个 SO 答案中需要解释的东西太多了)。一个简单(但不是通用)的解决方案可以解决所有 3 个问题,可能将在您的用例中使用 ConcurrentHashMap 而不是普通的 HashMap.

You could answer to your own question by reading the javadocs for HashMap. "Read the javadocs" is an important lesson that every beginner in Java should learn and remember.

In this case, the javadocs will show you 3 methods that could be useful:

  • The keys() method returns a collection consisting of the keys in the table.
  • The values() method returns a collection consisting of the values in the table.
  • The entries() method returns a collection representing the key/value pairs in the table.

You can iterate these collections as any other collection. There are examples in the other answers.


However, I get the impression that your application is multi-threaded. If that is the case the there are two other problems that you need to deal with to make your program reliable:

  1. If two or more threads could use the same object or data structure, they need to take the necessary steps to ensure that they are properly synchronized. If they don't then there is a non-zero probability that some sequence of operations will result in the data structure being put into an inconsistent state, or that one or more threads will see an inconsistent state (due to memory caches, values saved in registers, etc).

  2. If one thread is using one of a HashMap's collection iterators and another adds or removes an entry, then the first one is likely to get a ConcurrentModificationException.

  3. If you solve the above two problems by locking out all other operations on the HashMap while your "send to all" operation is going on, you are unintentionally creating a performance bottleneck. Basically, everything else stops until the operation has finished. You get a similar effect (but on a finer scale) if you simply put a synchronization wrapper around the HashMap.

You need to read and learn about these things. (And there's far too much to explain in a single SO Answer). A simple (but not universal) solution to all 3 problems that probably will work in your use-case it to use a ConcurrentHashMap instead of a plain HashMap.

掩耳倾听 2024-12-30 20:39:27

我可以发送消息,只是在哈希表中查找缺口,但如何将其发送给所有人?

然后对哈希表中的所有昵称执行相同的操作:

for (String name : yourTable.keySet())
    yourTable.get(name).send("your message");

或者,或者:

for (Connection conn : yourTable.values())
    conn.send("your message");

I can send messages just looking for a nick in the Hashtable, but how can I send it to all the people?

Then do the same for all nicknames in the hash table:

for (String name : yourTable.keySet())
    yourTable.get(name).send("your message");

or, alternatively:

for (Connection conn : yourTable.values())
    conn.send("your message");
樱桃奶球 2024-12-30 20:39:27

您可以迭代哈希表中的所有值并对所有值执行您希望的操作:

Map<String, Connection> users;
for (Connection connection : users.values()) {
    // Send the message to each Socket here.
}

You can iterate over all the values in the Hashtable and do what you wish to all of them:

Map<String, Connection> users;
for (Connection connection : users.values()) {
    // Send the message to each Socket here.
}
是你 2024-12-30 20:39:27

Hashtable 有 keySet(),它返回该表中的所有键条目。我是通过手机发布此内容的,无法为您提供示例链接。如果你想要所有连接列表,你可以使用entrySet()。

Hashtable has keySet() which returns all key entries in that table. I am posting this from mobile, couldnt get you example link. If you want all connection list you can use entrySet().

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