我正在处理源代码中的回归.我想告诉Git:"根据参数化日期/时间检查来源".这可能吗?
我也在目前的观点中进行了改变,我不想失去.理想情况下,我想在当前源和我之前根据之前的日期感兴趣的某些版本之间来回切换.
有没有一种优雅的方法可以跳过Java5 foreach循环中的第一次迭代?
示例伪代码:
for ( Car car : cars ) {
//skip if first, do work for rest
.
.
}
Run Code Online (Sandbox Code Playgroud) 我有zip存档,其中包含一堆纯文本文件.我想解析每个文本文件数据.这是我到目前为止所写的内容:
try {
final ZipFile zipFile = new ZipFile(chooser.getSelectedFile());
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
ZipInputStream zipInput = null;
while (entries.hasMoreElements()) {
final ZipEntry zipEntry = entries.nextElement();
if (!zipEntry.isDirectory()) {
final String fileName = zipEntry.getName();
if (fileName.endsWith(".txt")) {
zipInput = new ZipInputStream(new FileInputStream(fileName));
final RandomAccessFile rf = new RandomAccessFile(fileName, "r");
String line;
while((line = rf.readLine()) != null) {
System.out.println(line);
}
rf.close();
zipInput.closeEntry();
}
}
}
zipFile.close();
}
catch (final IOException ioe) {
System.err.println("Unhandled exception:");
ioe.printStackTrace();
return;
}
Run Code Online (Sandbox Code Playgroud)
我需要一个RandomAccessFile吗?我失去了我拥有ZipInputStream的地步.
我正在修改一些代码,我意识到我从来不知道的事情.正常的二进制搜索将在数据集中返回多次出现的键的随机索引.如何修改下面的代码以返回第一次出现?这是人们做的事吗?
//ripped from the JDK
public static int binarySearchValue(InvertedContainer.InvertedIndex[] a, long key) {
return bSearchVal(a, 0, a.length, key);
}
private static int bSearchVal(InvertedContainer.InvertedIndex[] a, int fromIndex,
int toIndex, long key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
long midVal = a[mid].val;
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else …Run Code Online (Sandbox Code Playgroud) 有没有标准的方法来做到这一点?我意识到这可能与平台有关.我们的产品现在只在Windows上支持 - 所以我想这就是我现在感兴趣的东西.我能想到的唯一事情是扫描注册表或抓取文件系统.扫描文件系统似乎需要很长时间 - 并且注册表可能不可靠.我应该两个都做吗?还有其他建议吗?我试图寻找一个API,没有运气.
在我的机器上,下面的程序打印:
OptionalLong[134043]
PARALLEL took 127869 ms
OptionalLong[134043]
SERIAL took 60594 ms
Run Code Online (Sandbox Code Playgroud)
我不清楚为什么串行执行程序比并行执行程序要快.我把两个程序-Xms2g -Xmx2g放在一个8gb相对安静的盒子上.有人可以澄清最新情况吗?
import java.util.stream.LongStream;
import java.util.stream.LongStream.Builder;
public class Problem47 {
public static void main(String[] args) {
final long startTime = System.currentTimeMillis();
System.out.println(LongStream.iterate(1, n -> n + 1).parallel().limit(1000000).filter(n -> fourConsecutives(n)).findFirst());
final long endTime = System.currentTimeMillis();
System.out.println(" PARALLEL took " +(endTime - startTime) + " ms");
final long startTime2 = System.currentTimeMillis();
System.out.println(LongStream.iterate(1, n -> n + 1).limit(1000000).filter(n -> fourConsecutives(n)).findFirst());
final long endTime2 = System.currentTimeMillis();
System.out.println(" SERIAL took …Run Code Online (Sandbox Code Playgroud) 我有一个包含嵌入式javascript的ant任务.我希望基于我在javascript中运行的一些逻辑使目标失败或成功:
<target name="analyze">
<script language="javascript">
<![CDATA[
importClass(java.io.File);
importClass(java.io.FileReader)
importClass(java.io.BufferedReader)
String.prototype.startsWith = function(str) {
return (this.indexOf(str) === 0);
}
String.prototype.endsWith = function(str) {
var lastIndex = this.lastIndexOf(str);
return (lastIndex != -1) && (lastIndex + str.length == this.length);
}
//setup the source directory
srcDir = project.getProperty("MY_HOME") + "/foo/src";
if(srcDir.startsWith("/foo") {
//TARGET SHOULD PASS
} else {
//TARGET SHOULD FAIL
}
]]>
</script>
</target>
Run Code Online (Sandbox Code Playgroud) 我觉得自己像是一个问这个问题的新手 - 但是为什么当我将下面的Set传递给我的方法并将其指向一个新的HashSet时,它仍然作为EmptySet出现?是因为局部变量是在堆栈上分配的,所以当我退出方法时,我的新东西会被吹走吗?我怎样才能实现功能等同?
import java.util.HashSet;
import java.util.Set;
public class TestMethods {
public static void main(final String[] args) {
final Set<Integer> foo = java.util.Collections.emptySet();
test(foo);
}
public static void test(Set<Integer> mySet) {
mySet = new HashSet<Integer>();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在观察一些奇特的行为Java8和新的行为Stream-API.
我希望以下两个陈述的表现相同,但事实并非如此.
LongStream.iterate(1, n -> n + 1).limit(5000)
.anyMatch(n -> isPerfectCube((n*n*n)+((n*n)*p)));
Run Code Online (Sandbox Code Playgroud)
与
LongStream.iterate(1, n -> n + 1)
.anyMatch(n -> isPerfectCube((n*n*n)+((n*n)*p)));
Run Code Online (Sandbox Code Playgroud)
两个语句都应该返回true,并且我不会期望任何性能差异,因为它们都可以在找到的第一个匹配项上发生短路.这些陈述的唯一区别是,一个是在要迭代的数字范围的上限,而另一个不是.
有人可以向我解释为什么一个人跑得更快并且使用的内存比另一个少?
好的 - 我知道我可以像这样运行我的java进程:
java -XX:+PrintCompilation <FooProgram>
Run Code Online (Sandbox Code Playgroud)
我可以看到如下输出:
c:\>java -XX:+PrintCompilation -jar c:\FooProgram.jar
1 java.lang.String::charAt (33 bytes)
2 java.lang.Math::max (11 bytes)
1% java.util.jar.JarFile::hasClassPathAttribute @ 78 (197 bytes)
3 java.lang.String::hashCode (60 bytes)
4 java.lang.String::indexOf (151 bytes)
5 java.util.Properties$LineReader::readLine (452 bytes)
6 java.lang.Object::<init> (1 bytes)
7 java.lang.String::indexOf (166 bytes)
8 java.lang.String::equals (88 bytes)
Run Code Online (Sandbox Code Playgroud)
让我知道哪些方法是编译的,未编译的,标记为僵尸等等.是否有监控编译这些方法的代码缓存的用法?当编译器代码缓存已满时,我看到一条消息,上面写着:"我已经满了,没有更多的编译对你......",但似乎这样的事情似乎是一个滞后指示我们遇到了问题.我知道如何打印代码缓存的最大大小,我正在寻找的是一种方式来查看它是如何填充的.
java ×8
java-8 ×2
java-stream ×2
performance ×2
algorithm ×1
allocation ×1
ant ×1
binary ×1
failonerror ×1
foreach ×1
git ×1
git-checkout ×1
heap ×1
install ×1
iteration ×1
javascript ×1
lambda ×1
search ×1
skip ×1
stack ×1
windows ×1
zip ×1