Java是这么说的: Collectors toSet() returns a Collector that accumulates the input elements into a new Set. There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned. This is an unordered Collector i.e, the collection operation does not commit to preserving the encounter order of input elements. 所以它可以给你A,B,E中的任何一个,都不算出错。
回复 1楼hannsg的帖子 This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. 只要有NJTC就是对的吧,难道不是多选题?
Given: public static Function<String,String> swap = s -> { if(s.equals("Australia")) return "New Zealand"; else return s; }; And given: Set<String> islandNations = Set.of("Australia", "Japan", "Taiwan", "Cyprus", "Cuba"); islandNations = islandNations.stream() .map(swap) .map(n -> n.substring(0, 1)) .collect(Collectors.toSet()); for(String s : islandNations){ System.out.print(s); }
What is the output? a.TJNC b.CTJN c.TCNJC d.TCAJ e.TCNJ f.TCAJC
Set<String> islandNations = Set.of("Australia", "Japan", "Taiwan", "Cyprus", "Cuba"); islandNations = islandNations.stream() .map(swap) //Set.of("New Zealand", "Japan", "Taiwan", "Cyprus", "Cuba"); .map(n -> n.substring(0, 1)) //("N", "J", "T", "C", "C") .collect(Collectors.toSet()); //("N", "J", "T", "C") for(String s : islandNations){ System.out.print(s); }
太牛了!
所以它可以给你A,B,E中的任何一个,都不算出错。
直接找到ide run 一下不就行了吗
This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.
只要有NJTC就是对的吧,难道不是多选题?
Set<String> islandNations = Set.of("Australia", "Japan", "Taiwan", "Cyprus", "Cuba"); var list = islandNations.stream() .map(swap) .map(n->n.substring(0,1)) .collect(Collectors.toList()); for (var s:list){ System.out.print(s); }
换成Collectors.toList()以后结果就是随机的,比如: TCCJN
说起IDE,我的NetBeans,文字输入一切换到中文就死啊。一切换回英文有一切正常。死了无数次才意识到的。 这年头java也太诡异了
这道题目本身出得太有问题了,我觉得不应该多想。 正确的答案应该是那三个都对。
但是如果你真拿到这代码去IDE里面run,出来的的确又是B。哈哈。 但如果真要考到B这么艰深,我觉得根本就没有必要。 B的原因是因为单字母在java set里面用的是MAP二叉树, 然后用ASCII码做成随机码,再用后序遍历打印出来的
不信你可以加个Uganda(其实就是字母U),出来的顺序一定是CTUJN
for (int i = 0; i < 1000; ++i) { islandNations.add("" + i); } for (int i = 0; i < 1000; ++i) { islandNations.remove("" + i); } for(String s : islandNations){ System.out.print(s); }
这太神奇了。谢谢指教!
华人很强大!
牛,懂这么多。
eclipse啊 话说做这些八股就是文科生的事情。类似回字的四种写法。理科生直接run一下,简单直接
好问题
再说一遍,set 无序,这个弱智考题宗旨就是让马公麻婆承认台湾是一个国家。
第一,这样写通不过code review的吧。这个我们公司碰到好多次了,developer一直说code review的重要性,怕outsource的人写的太乱。 第二,看了大家回应,谁会直接用toset()的结果,除非顺序无所谓,不需要知道,否则肯定会先sort。
最后想说为啥要考那么无聊的题目?没人会这样写,而且即使不知道这种无聊的答案还有debug啊,不应该放更多精力在其他地方吗?这样考进来的职员不会很机车吗?用心的重点都不一样。
这个是oracle university上的java 11 developer mock exam上的。
虽然中国大陆不承认,台湾事实上就是个国家啊,还有邦交国的呢。
现在还有人考java certification吗,我老一堆java certification都是20年前Sun时代的了。。。
觉得Java毁在Oracle 手里了
湾区的马工麻婆大佬进来走两步。
即使知道也没有意义啊,除非是你写过这个library的才知道,一般用的人call toset()直接用肯定不考虑排序,考虑排序的肯定会sort. 如果连这个都要知道真的太浪费时间了,而且随时会更新library啊,更新过如果里面有更多优化结果可能就不一样了。
碰到要考这个的老板以后有的苦让你吃。这不是各种为了刁难你而刁难吗?
我后来想了想,这个set是不允许有重复的,所以后台一定是二叉树,C最小,所以是二叉树最下端左边的叶子,遍历的时候第一个打印出来。排除法下来只有一个选项。其他什么顺序就搞不懂了.
而且set,tree都还有load factor,会有空叶子,更加搞不明白