我读了这个 Java 8 官方文档:
Streams may or may not have a defined encounter order. Whether or not a stream has an encounter order depends on the source and the intermediate operations. Certain stream sources (such as List or arrays) are intrinsically ordered, whereas others (such as HashSet) are not.
If a stream is ordered, repeated execution of identical stream pipelines on an identical source will produce an identical result; if it is not ordered, repeated execution might produce different results.
试图通过这段代码理解上述行为
public class StreamOrderValidator
{
public static void main( String[] args )
{
String[] colors=new String[] {"red","green","blue","orange"};
List<String> colorsList=Arrays.asList(colors);
HashSet<String> colorsSet=new HashSet<>();
colorsSet.addAll(colorsList);
System.out.println(colorsSet); // [red, orange, green, blue]
List<String> processedColorsSet = processStream(colorsSet.stream());
System.out.println(processedColorsSet); // [RED, ORANGE, GREEN, BLUE]
}
private static List<String> processStream(Stream<String> colorStream) {
List<String> processedColorsList = colorStream.filter(s->s.length()<=6).
map(String::toUpperCase).collect(Collectors.toList());
return processedColorsList;
}
}
我多次运行此代码,结果流中元素的顺序始终相同(显示为注释)。我无法弄清楚这如何证明上面引用的关于“未为无序集合保留顺序”的文本。
我肯定误解了从 javadocs 中提取的文本。
最佳答案
这里确实有一点误解。 HashSet 或任何 Set 与顺序无关,除非 TreeSet 是基于 Comparator 排序的。
目前,在 java-8 下,一旦您将元素放入 HashSet 中(并且不要更改它)- 将会有一个元素如何排列的顺序布置好了;但同样,前提是您不添加或删除它们中的任何一个。这可以随时更改,所以不要依赖它。
例如运行这个:
String[] colors = new String[] { "red", "green", "blue", "orange" };
List<String> colorsList = Arrays.asList(colors);
HashSet<String> colorsSet = new HashSet<>();
colorsSet.addAll(colorsList);
System.out.println(colorsSet);
无论 当前 在 java-8 下运行多少次,您都总是得到相同的输出:
[red, orange, green, blue]
但是一旦你进行了一些内部重新洗牌:
for (int i = 0; i < 1000; ++i) {
colorsSet.add("" + i);
}
for (int i = 0; i < 1000; ++i) {
colorsSet.remove("" + i);
}
System.out.println(colorsSet); // [blue, red, green, orange]
您可以看到输出发生了变化,因为 Set 没有顺序。
要采取的关键点是没有顺序,您确实看到一个顺序的事实并不能保证每次都会发生 - java-8 中可能会有一个构建会破坏这个顺序。事实上,使用 java-9 很容易观察到这一点 - 例如,新的 Set 有一个随机化模式。
如果多次运行,结果会不同:
Set<String> set = Set.of("red", "green", "blue", "orange");
System.out.println(set);
很明显,您从这样一个 Set 中stream 的顺序将无法保证,因此您确实会在每次运行中看到不同的结果。
关于java - 了解从 HashSet 生成的流中元素的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45907797/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我是Rails的新手,所以请原谅简单的问题。我正在为一家公司创建一个网站。那家公司想在网站上展示它的客户。我想让客户自己管理这个。我正在为“客户”生成一个表格,我想要的三列是:公司名称、公司描述和Logo。对于名称,我使用的是name:string但不确定如何在脚本/生成脚手架终端命令中最好地创建描述列(因为我打算将其设置为文本区域)和图片。我怀疑描述(我想成为一个文本区域)应该仍然是描述:字符串,然后以实际形式进行调整。不确定如何处理图片字段。那么……说来话长:我在脚手架命令中输入什么来生成描述和图片列? 最佳答案 对于“文本”数
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
有这些railscast。http://railscasts.com/episodes/218-making-generators-in-rails-3有了这个,你就会知道如何创建样式表和脚手架生成器。http://railscasts.com/episodes/216-generators-in-rails-3通过这个,您可以了解如何添加一些文件来修改脚手架View。我想把两者结合起来。我想创建一个生成器,它也可以创建脚手架View。有点像RyanBates漂亮的生成器或web_app_themegem(https://github.com/pilu/web-app-theme)。我