上下文:给定一个目录,我想在列表中列出其名称中包含模式的所有文件,按lastModified时间戳排序,并将此列表格式化为Json字符串,我将获取每个文件的名称和时间戳:
[{"name": "somefile.txt", "timestamp": 123456},
{"name": "otherfile.txt", "timestamp": 456789}]
Run Code Online (Sandbox Code Playgroud)
我有以下代码:
private StringBuilder jsonFileTimestamp(File file) {
return new StringBuilder("{\"name\":\"")
.append(file.getName())
.append("\", \"timestamp\":")
.append(file.lastModified())
.append("}");
}
public String getJsonString(String path, String pattern, int skip, int limit) throws IOException {
return Files.list(Paths.get(path))
.map(Path::toFile)
.filter(file -> {
return file.getName().contains(pattern);
})
.sorted((f1, f2) -> {
return Long.compare(f2.lastModified(), f1.lastModified());
})
.skip(skip)
.limit(limit)
.map(f -> jsonFileTimestamp(f))
.collect(Collectors.joining(",", "[", "]"));
}
Run Code Online (Sandbox Code Playgroud)
这很好用.我只关心StringBuilder实例化(或字符串连接)的性能.只要文件数量很少就可以了(这是我的情况,所以我很好),但我很好奇:你会建议什么作为优化?我觉得我应该使用reduce正确的累加器和组合器,但我不能让我的大脑围绕它.
谢谢.
我终于进行了以下"优化":
private StringBuilder jsonFileTimestampRefactored(StringBuilder res, File file) {
return res.append(res.length() …Run Code Online (Sandbox Code Playgroud) 在C#中,当我们想要创建可以将lambda表达式作为参数的方法时,我们可以使用Action或Func<T>根据情况使用.新的Java 8增加了对lambdas的支持,但我找不到任何关于如何使用它的体面的例子.所以假设我想在Java中创建一个类似于这个C#的方法:
public static Boolean Check (String S, Func<String, Boolean> AnAction) {
return AnAction(S);
}
Run Code Online (Sandbox Code Playgroud)
那怎么用Java编写呢?
我正在阅读Java:Herbert Schildt的完整参考资料,现在这里有一件事对我来说并不是很清楚.在关于整数的章节中,它说当需要大的整数时应该使用long类型.书中的代码示例:
// Compute distance light travels using long variables
class Light {
public static void main(String args[]) {
int lightspeed;
long days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 18600;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days + " days …Run Code Online (Sandbox Code Playgroud) 我想阅读/ etc/passwd文件的内容并获取一些数据:
public void getLinuxUsers()
{
try
{
// !!! firstl line of the file is not read
BufferedReader in = new BufferedReader(new FileReader("/etc/passwd"));
String str;
str = in.readLine();
while ((str = in.readLine()) != null)
{
String[] ar = str.split(":");
String username = ar[0];
String userID = ar[2];
String groupID = ar[3];
String userComment = ar[4];
String homedir = ar[5];
System.out.println("Usrname " + username +
" user ID " + userID);
}
in.close();
}
catch (IOException e)
{
System.out.println("File Read …Run Code Online (Sandbox Code Playgroud) 给定一种Collection类型T,将Java 8转换List为类型的Java 8惯用方法是什么T?是以下吗?:
Collection<Foo> f;
f.stream().collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 我在test.js文件中有以下方法:
function avg(input, period) {
var output = [];
if (input === undefined) {
return output;
}
var i,j=0;
for (i = 0; i < input.length- period; i++) {
var sum =0;
for (j = 0; j < period; j++) {
//print (i+j)
sum =sum + input[i+j];
}
//print (sum + " -- " + sum/period)
output[i]=sum/period;
}
return output;
}
Run Code Online (Sandbox Code Playgroud)
我想将一个数组从java传递给这个函数,并在java中获取js输出数组.我使用了以下java代码:
double[] srcC = new double[] { 1.141, 1.12, 1.331, 1.44, 1.751, 1.66, 1.971, 1.88, 1.191, 1.101 };
try …Run Code Online (Sandbox Code Playgroud) 此Q用于验证.
当文字值以0开头时,JDK 8似乎正在处理八进制:
System.out.print(011);
Run Code Online (Sandbox Code Playgroud)
印刷9,和
System.out.print(08);
Run Code Online (Sandbox Code Playgroud)
给出一个检查错误.
这不是在文档中 - 还是(?)
这是jdk8中的新功能吗?如果是这样的话 - 在八进制的情况下是否有一些细节,除了HEX和二进制的那些?
TIA
// =============================
编辑:
Q是关于JDK处理八进制 - 从什么时候开始.dosc仅显示HEX和二进制.
第二个代码行是为了显示它的八进制JDK,当值前面有一个0时.
有没有办法在Consumer表达式上访问方法的注释?
public void <T> addListener(Consumer<T> consumer)
{
consumer.getClass().getAnnotation(Handler.class); // Like this
}
@Handler
public void myListener(Integer x) {
}
addListener<Integer>(this::myListener);
Run Code Online (Sandbox Code Playgroud) 我有Flyweight模式,我尝试将循环转换为流,但结果是不同的:
public Line getLine(Color color) {
for(Line line: pool) {
if(line.getColor().equals(color)) {
return line;
}
}
return createLine(color);
}
factory.getLine(Color.RED);
factory.getLine(Color.RED);
System.out.println(getPool().size()); // print 1
Run Code Online (Sandbox Code Playgroud)
重构代码:
public Line getLine(Color color) {
return pool.stream()
.filter(l -> l.getColor().equals(color))
.findFirst()
.orElse(createLine(color));
}
factory.getLine(Color.RED);
factory.getLine(Color.RED);
System.out.println(getPool().size()); // print 2
Run Code Online (Sandbox Code Playgroud)
流有什么问题?
我似乎无法使它工作.
Function<Integer, Integer> test = x -> x+x;
Function<String, String> test = x -> x+x;
Run Code Online (Sandbox Code Playgroud)
产量
重复的局部变量
test
如何才能使其test.apply(5)返回10并test.apply("5")返回"55"?
java ×10
java-8 ×10
lambda ×3
java-stream ×2
arrays ×1
for-loop ×1
javascript ×1
literals ×1
nashorn ×1
octal ×1
optimization ×1
overloading ×1