使用Java客户端的好Zookeeper Hello world程序(Good Zookeeper Hello world Program with Java client)
我试图在我们的项目中使用Zookeeper。 可以运行服务器..甚至使用zkcli.sh测试它..一切都很好..但是找不到一个很好的教程让我用Java连接到这个服务器! 我在Java API中需要的只是一种方法
public String getServiceURL ( String serviceName )
我试过https://cwiki.apache.org/confluence/display/ZOOKEEPER/Index - >对我不好。
http://zookeeper.apache.org/doc/trunk/javaExample.html :排序好的; 但是不能清楚地理解概念! 我觉得它没有得到很好的解释..
I was trying to use Zookeeper in our project. Could run the server..Even test it using zkcli.sh .. All good.. But couldn't find a good tutorial for me to connect to this server using Java ! All I need in Java API is a method
public String getServiceURL ( String serviceName )
I tried https://cwiki.apache.org/confluence/display/ZOOKEEPER/Index --> Not good for me.
http://zookeeper.apache.org/doc/trunk/javaExample.html : Sort of ok; but couldnt understand concepts clearly ! I feel it is not explained well..
原文:https://stackoverflow.com/questions/33524537
最满意答案
以下代码有什么作用?
它似乎用于从列表中删除重复项而不更改顺序
- 删除重复项(LinkedHashSet是一个Set)
- 维护插入顺序( LinkedHashSet具有可预测的迭代顺序)
- 转换回List
LinkedHashSet可以替换为HashSet吗?
不,它不会保证订单(#2)
What does the following code do?
It seems like it is used to remove duplicates from a list without changing the order
- removes duplicates (LinkedHashSet is a Set)
- maintains insertion order (LinkedHashSet has predictable iteration order)
- convert back to a List
Can the LinkedHashSet be replaced with a HashSet?
No, it will not keep the order guaranteed (#2)
相关问答
更多-
不同的默认'initialCapacity'HashSet和LinkedHashSet(Different default 'initialCapacity' HashSet and LinkedHashSet)[2023-05-14]
从您向我们展示的代码中,下面是HashSet和LinkedHashSet的规格: data structure | initial capacity | load factor HashSet | max(1.333 * size, 16) | 0.75 LinkedHashSet | max(2 * size, 11) | 0.75 关于我的头顶,重组一个LinkedHashSet可能比一个普通的HashSet更昂贵,因为前者有一个链接列表,它可能也必须重构/重新计算。 ... -
HashSet和LinkedHashSet(HashSet vs LinkedHashSet)[2023-11-18]
答案在于LinkedHashSet用于构造基类的构造函数: public LinkedHashSet(int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor, true); // <-- boolean dummy argument } ... public LinkedHashSet(int initialCapacity) { super(initialCapacity, .75f ... -
有很多选择, 主要取决于您将如何使用所获取的数据 - 例如,如果您不关心修改数据的调用代码,那么使用公共成员设计结构将导致最小的CPU压力。 响应中的最大可能键值为365,每个键只有2个值。 我懂了。 我建议删除LinkedHashSet并编写一个自定义类来保存那两个未整理的整数(在将int转换为Integer并返回时不要浪费CPU): public class ExpectedVsActual { // if you don't care too much of your data integrity ...
-
看看你的代码: class LinkedHashSet { public static void main(String[] args) { //LinkedHashSet
lHs = new LinkedHashSet (); LinkedHashSet lHs = new LinkedHashSet(); lHs.add("Beta"); lHs.add("Alpha"); lHs. ... -
是的,当你在没有向下钻取的情况下查看源时,它有点不稳定。 请注意,它调用包受保护的HashSet构造函数,该构造函数接受一个名为dummy的无意义布尔值: public LinkedHashSet(int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor, true); } 然后使用LinkedHashMap而不是HashMap来支持该集合。 实际上,LinkedHashSet实际上是在HashSet中,它 ...
-
为什么我的LinkedHashSet在toString()方法中是空的?(Why is my LinkedHashSet empty within the toString() method?)[2023-04-16]
toString()方法依赖于您正在声明的myHashSet字段,但不添加元素。 事实上,在这里: MyHashSetmyHashSet = new MyHashSet<>(); myHashSet.add("A"); myHashSet.add("B"); myHashSet.add("C"); myHashSet.add("D"); myHashSet.add("E"); 您可以通过依赖类继承的LinkedHashSet ... -
可以用HashSet替换此代码中的LinkedHashSet吗?(Can LinkedHashSet in this code be replaced with a HashSet?)[2023-12-13]
以下代码有什么作用? 它似乎用于从列表中删除重复项而不更改顺序 删除重复项(LinkedHashSet是一个Set) 维护插入顺序( LinkedHashSet具有可预测的迭代顺序) 转换回List LinkedHashSet可以替换为HashSet吗? 不,它不会保证订单(#2) What does the following code do? It seems like it is used to remove duplicates from a list without changing the or ... -
LinkedHashMap vs HashMap,LinkedHashSet vs HashSet(LinkedHashMap vs HashMap, LinkedHashSet vs HashSet)[2022-10-31]
保持插入顺序与其相关的成本,无论是在需要更多的内存和额外的CPU周期: 您需要额外的内存来保留额外的链接, 您需要额外的CPU周期来维护它。 虽然渐近的复杂性是相同的,但增加的便利性不是免费的。 如果您不需要维护插入顺序,则不必为其付费,而是使用轻量级HashSet和HashMap 。 Keeping the insertion order has its associated costs, both in terms of needing more memory, and spending ... -
如果您使用Java 8并且可以返回HashSet (而不是LinkedHashSet ),则可以使用Stream API: Set
newSet = set.stream() .skip(set.size() - 5) .collect(Collectors. toSet()); I ended up using this: com.google.common.collect.Ev ... -
您错过了使用集合的关键点:您不能修改存储在集合中的值(至少不会以改变其相等性的方式)。 如果修改已存储在HashSet中的值,并且该修改更改了值的hashCode,则无法再在该集合中找到该值,因为它不再位于与其hashCode对应的存储桶中。 将一个集合与另一个集合时,您更改其hashCode,从而完全破坏整个数据结构的正确性。 例: 创建一个包含1的LinkedHashSet inner1 .hashCode为5 将其存储在outerSet中 outerSet在bucket 5中存储inner1,因为它的 ...