草庐IT

output_stream

全部标签

java - 使用 Java8 Streams 从另外两个列表创建对象列表

我有以下Java6和Java8代码:Listlst1=//alistofObjectType1objectsListlst2=//alistofObjectType1objects,samesizeoflst1Listlst3=newArrayLis(lst1.size());for(inti=0;i在Java8中有什么方法可以使用Lambda以更简洁的方式处理前面的for吗? 最佳答案 Stream与给定的iterable/Collection相关联,因此您不能真正并行地“迭代”两个集合。一种解决方法是创建一个索引流,但它不一定比

Java 8 Stream API - 选择分组后的最低键

我有一个Foo对象流。classFoo{privateintvariableCount;publicFoo(intvars){this.variableCount=vars;}publicIntegergetVariableCount(){returnvariableCount;}}我想要一个Foo的列表都是具有最低variableCount的。例如newFoo(3),newFoo(3),newFoo(2),newFoo(1),newFoo(1)我只希望流返回最后2个Foos,因为它们的值最低。我试过通过分组进行收集.collect(Collectors.groupingBy((Foo

java - TreeSet 与 Java 8 Streams 性能对比

哪种方式处理不同且已排序的集合最有效?1.使用TreeSet增强循环Setret=newTreeSet();for(Foofoo:foos)ret.add(newMyObj(foo));2.简单流Listret=foos.stream().map(MyObj::new).distinct().sorted().collect(Collectors.toList());3.TreeSet流Setret=foos.stream().map(MyObj::new).collect(Collectors.toCollection(TreeSet::new));第一种方式似乎最不优雅但易于阅读。

java - 将 CompletableFuture<Stream<T>> 转换为 Publisher<T> 是否正确?

允许对来自CompletableFuture>的结果流进行多次迭代我正在考虑以下方法之一:将结果future转换为CompletableFuture>通过:teams.thenApply(st->st.collect(toList()))将结果future转换为Flux带缓存:Flux.fromStream(teams::join).cache();Flux是Publisher的实现在项目react器中。用例:我想从提供Stream的数据源中获取包含英超球队名称的序列(例如League)带有Standing[]的对象(基于足球数据RESTfulAPI,例如http://api.foot

java - 为什么 `Stream.collect` 是类型安全的而 `Stream.toArray(IntFunction<A[]>)` 不是?

考虑以下代码片段Stringstrings[]={"test"};finalListcollect=java.util.Arrays.stream(strings).collect(java.util.stream.Collectors.toList());finalDouble[]array=java.util.Arrays.stream(strings).toArray(Double[]::new);为什么Java可以在收集情况下保证正确的类型(将收集的泛型类型更改为例如Double会导致编译时错误),但在数组情况下却不能(编译良好,尽管apply(intDouble[]::new

java - 为什么上传到 S3 的文件的内容类型为 application/octet-stream,除非我将文件命名为 .html?

即使我将内容类型设置为text/html,它在S3上最终会变成application/octet-stream。ByteArrayInputStreamcontentsAsStream=newByteArrayInputStream(contentAsBytes);ObjectMetadatamd=newObjectMetadata();md.setContentLength(contentAsBytes.length);md.setContentType("text/html");s3.putObject(newPutObjectRequest(ARTIST_BUCKET_NAME,

java - Stream foreach Java 8 中的递增计数器

我想在使用foreach循环时增加一个counter,它是一个AtomicIntegerpublicclassConstructorTest{publicstaticvoidmain(String[]args){AtomicIntegercounter=newAtomicInteger(0);ListfooList=Collections.synchronizedList(newArrayList());ListuserList=Collections.synchronizedList(newArrayList());userList.add("username1_id1");user

Java 8 Stream API : Filter on instance, 和 cast

这个问题在这里已经有了答案:IsitpossibletocastaStreaminJava8?(5个答案)关闭6年前。我有一个对象列表:ListmyList;我想获取此列表中可用的子类型列表:ListmyChildList=myList.stream().filter(e->einstanceofSomeChildType).collect(??????)我不知道如何收集以获得正确的列表类型。

java - 给定字符集将 InputStream 转换为 Stream<String>

我想转换一个InputStreamis进入Streamstream给出Charsetcs以这样的方式stream由is行组成.此外一行is不应立即阅读,但仅在以防万一stream需要它。 最佳答案 我想你可以试试:Streamlines=newBufferedReader(newInputStreamReader(is,cs)).lines(); 关于java-给定字符集将InputStream转换为Stream,我们在StackOverflow上找到一个类似的问题:

python - Amazon + Django 每12小时出现[Errno 5] Input/output error

我最近设置并部署了一个AmazonEC2实例来部署我的django项目。当我在浏览器中收到此错误时,我正在通过浏览器与我的应用程序交互:errno5input/outputerrordjango此错误确实引用了我的应用程序的某些功能Environment:RequestMethod:GETRequestURL:http://localhost:8000/accounts/profile/DjangoVersion:1.9PythonVersion:3.4.3InstalledApplications:['django.contrib.admin','django.contrib.aut