我想知道如果终端操作是列表收集器,Java 8流如何处理内存分配.
例如,考虑一下
List<Integer> result = myList.stream().map(doWhatever).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
VS
List<Integer> result = new ArrayList<>(myList.size());
for(String s : myList) {
result.add(doWhatever.apply(s));
}
Run Code Online (Sandbox Code Playgroud)
如果使用流,则不知道列表将增长多少,这意味着必须进行某种重新分配.这个假设是真的吗?
结果列表的类型是某种链表,因此对元素的访问速度比ArrayList慢吗?
如果我从一开始就知道结果列表的大小,我是不是应该使用带有列表收集器的流?
我想使用 Mapstruct 将内部模型映射到 Kotlin 项目中由 OpenApi3 codegen 生成的模型。
当我编译项目时,Mapstruct 似乎无法找到 OpenApi3 codegen 插件生成的源,因为生成的实现包含 aNonExistentClass而不是我的 OpenApi 模型。
我的插件配置是
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>kapt</id>
<phase>process-sources</phase>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
<sourceDir>src/main/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin> …Run Code Online (Sandbox Code Playgroud) 我想获取目录中遵循特定模式的所有文件名。我发现我可以通过执行以下操作来实现此目的:
var fs = require('fs');
fs.readdir( dir, function(err, list) {
if(err)
throw err;
var regex = new RegExp(".*");
list.forEach( function(item) {
if( regex.test(item) )
console.log(item);
});
});
Run Code Online (Sandbox Code Playgroud)
我想知道是否还有另一种可能性可以在不使用循环的情况下执行此操作,例如将正则表达式传递给fs.readdir(..).
这是否可能,或者我必须使用循环?
在以下上下文中使用instanceof运算符是不好的做法吗?
public interface IWriter {
public abstract void write(Dto dto);
}
public abstract class Dto {
private long id;
public void setId(long id) {this.id = id;}
public long getId() {return id;}
}
public class DtoA extends Dto {
...
}
public class DtoB extends Dto {
...
}
public class MyWriterA implements IWriter {
@Override
public void writer(Dto dto) {
if (!(dto instanceof DtoA))
return;
...
}
}
public class MyWriterB implements IWriter {
@Override
public void writer(Dto dto) { …Run Code Online (Sandbox Code Playgroud) 我有几个实体,并使用Spring Data JPA存储库和规范查询我的数据库。因此,我创建了一个通用类SpecBuilder来根据查询描述 ( MyQueryDescriptor)构建我的查询。
public class Specs {
public static <T extends MyEntityIFace> Specification<T> myfind(final MyQueryDescriptor qDesc) {
return new Specification<T>() {
@Override
public Predicate toPredicate(Root<T> root,
CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
try {
return SpecBuilder.mySpec(root, criteriaQuery, criteriaBuilder, qDesc);
} catch (Exception e) {
...handle error...
}
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
我的存储库:
public interface Entity1DAO extends Repository<Entity1,Long>,
JpaSpecificationExecutor {
}
Run Code Online (Sandbox Code Playgroud)
和
public interface Entity2DAO extends Repository<Entity2,Long>,
JpaSpecificationExecutor {
}
Run Code Online (Sandbox Code Playgroud)
现在有 3 件事我不太确定: …
我使用Java 8流来处理文件,但到目前为止总是逐行处理.
我想要的是一个函数,它获取BufferedReader br并且应该读取特定数量的单词(分隔"\\s+")并且应该将BufferedReader保留在达到单词数的确切位置.
现在我有一个版本,它按行读取文件:
final int[] wordCount = {20};
br
.lines()
.map(l -> l.split("\\s+"))
.flatMap(Arrays::stream)
.filter(s -> {
//Process s
if(--wordCount[0] == 0) return true;
return false;
}).findFirst();
Run Code Online (Sandbox Code Playgroud)
这显然使Inputstream处于第20个单词的下一行的位置.
有没有办法从输入流中获取少于一行的流?
编辑
我正在解析一个文件,其中第一个单词包含下列单词的数量.我读了这个词然后读了具体的单词数.该文件包含多个这样的部分,其中每个部分在所描述的函数中被解析.
阅读完所有有用的注释后,我很清楚,使用a Scanner是这个问题的正确选择,Java 9将有一个Scanner提供流功能的类(Scanner.tokens()和Scanner.findAll()).
以我描述的方式使用Streams将无法保证读者将在流的终端操作(API文档)之后处于特定位置,因此使得流成为解析结构的错误选择,其中您只解析一个结构部分并且必须跟踪位置.
我偶然发现了图像在带有 .flexbox 的居中居中的问题direction:column。
想象一下,弹性盒中有两个元素,其中第一个元素包含图像:
<div class="container">
<div class="image-container">
<img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg">
</div>
<div class="another-flex-child">
Random content here
</div>
</div>
.container {
height: 300px;
background-color: green;
display: flex;
flex-direction: column;
.image-container {
flex: 1;
align-self: center;
.img {
height: 100%;
}
}
.another-flex-child {
flex: none;
background-color: red;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望图像在 div 内水平居中,但图像的左边框似乎正好位于 div 的中心。
当我用另一个包含一些文本的 div 替换图像时,它会按预期放置。
有人可以向我解释那里发生了什么吗?
我真的不懂Access-Control-Allow-Origin和CORS.
如果我允许来自任何域的请求到我的页面,这是否意味着我的页面存在任何安全问题?
我一直认为,SOP确保不能在页面上运行任何脚本,该脚本从另一台服务器请求数据,因为该数据可能是恶意的.但是,由于服务于恶意数据的服务器只能回复包含的标头Access-Control-Allow-Origin:*,所以可以从该服务器加载所有内容.
因此,只要有人设法将一段JS代码注入页面,就可以从属于攻击者的服务器加载每个恶意代码.
Unitl现在我假设,我必须启用跨域请求以允许我的页面上的代码从另一个域请求数据,但它似乎是相反的方式; 另一个域必须允许我的域请求数据.我真的没有看到这个概念的安全性好处.
任何人都可以解释这背后的概念,或者告诉我,如果我弄错了吗?
java ×4
java-8 ×2
java-stream ×2
javascript ×2
cors ×1
css ×1
flexbox ×1
html ×1
instanceof ×1
jpa ×1
kotlin ×1
less ×1
mapstruct ×1
memory ×1
meteor ×1
node.js ×1
openapi ×1
regex ×1
spring ×1
spring-data ×1