读zookeeper的源码(zookeeper.java)时发现的,平时没有怎么注意:
private final ZKWatchManager watchManager; List<String> getDataWatches() { synchronized(watchManager.dataWatches) { List<String> rc = new ArrayList<String>(watchManager.dataWatches.keySet()); return rc; } } List<String> getExistWatches() { synchronized(watchManager.existWatches) { List<String> rc = new ArrayList<String>(watchManager.existWatches.keySet()); return rc; } } List<String> getChildWatches() { synchronized(watchManager.childWatches) { List<String> rc = new ArrayList<String>(watchManager.childWatches.keySet()); return rc; } }
另数组与list的转换:
数组-->list
String[] userid = {"aa","bb","cc"}; List<String> userList = new ArrayList<String>(); Collections.addAll(userList, userid);
数组-->list
String[] userid = {"aa","bb","cc"}; List<String> userList = Arrays.asList(userid); 另:Arrays.asList()返回一个受指定数组支持的固定大小的列表。所以不能做Add、Remove等操作。 List list = new ArrayList(Arrays.asList(userid));这样操作就可以了。
list-->数组
List<String> strList = new ArrayList<String>(); strList.add("aa"); strList.add("bb"); Object[] objs = strList.toArray();
参考文献:
【1】http://jingyan.baidu.com/article/86f4a73e607f2937d7526972.html
【2】https://github.com/apache/zookeeper