首页 \ 问答 \ 使用Java客户端的好Zookeeper Hello world程序(Good Zookeeper Hello world Program with Java client)

使用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
更新时间:2022-01-21 15:01

最满意答案

以下代码有什么作用?

它似乎用于从列表中删除重复项而不更改顺序

  1. 删除重复项(LinkedHashSet是一个Set)
  2. 维护插入顺序( LinkedHashSet具有可预测的迭代顺序)
  3. 转换回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

  1. removes duplicates (LinkedHashSet is a Set)
  2. maintains insertion order (LinkedHashSet has predictable iteration order)
  3. convert back to a List

Can the LinkedHashSet be replaced with a HashSet?

No, it will not keep the order guaranteed (#2)

相关问答

更多
  • 从您向我们展示的代码中,下面是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更昂贵,因为前者有一个链接列表,它可能也必须重构/重新计算。 ...
  • 答案在于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中,它 ...
  • toString()方法依赖于您正在声明的myHashSet字段,但不添加元素。 事实上,在这里: MyHashSet myHashSet = new MyHashSet<>(); myHashSet.add("A"); myHashSet.add("B"); myHashSet.add("C"); myHashSet.add("D"); myHashSet.add("E"); 您可以通过依赖类继承的LinkedHashSet ...
  • 以下代码有什么作用? 它似乎用于从列表中删除重复项而不更改顺序 删除重复项(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 ...
  • 保持插入顺序与其相关的成本,无论是在需要更多的内存和额外的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,因为它的 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。