我正在探索Java 8源代码,发现代码的这一特定部分非常令人惊讶:
//defined in IntPipeline.java
@Override
public final OptionalInt reduce(IntBinaryOperator op) {
return evaluate(ReduceOps.makeInt(op));
}
@Override
public final OptionalInt max() {
return reduce(Math::max); //this is the gotcha line
}
//defined in Math.java
public static int max(int a, int b) {
return (a >= b) ? a : b;
}
Run Code Online (Sandbox Code Playgroud)
是Math::max
什么样的方法指针?普通static
方法如何转换为IntBinaryOperator
?
我有以下代码:
private String toString(List<DrugStrength> aDrugStrengthList) {
StringBuilder str = new StringBuilder();
for (DrugStrength aDrugStrength : aDrugStrengthList) {
if (!aDrugStrength.isValidDrugDescription()) {
aDrugStrengthList.remove(aDrugStrength);
}
}
str.append(aDrugStrengthList);
if (str.indexOf("]") != -1) {
str.insert(str.lastIndexOf("]"), "\n " );
}
return str.toString();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我得到ConcurrentModificationException
,任何人都可以解释它为什么会发生,即使代码在同一个线程中运行?我怎么能避免它呢?
如何在java中通过引用传递原始类型?例如,如何将int
传递给可修改的方法?
有没有办法在使用UI-Router时向URL添加搜索参数$state.go()
?我想在URL中使用带有附加信息的已定义状态,而不是使用相同的配置定义新路由.
我有一个简单的视图定义:
.when('page-with-form', {
template: 'views/page-with-form.html',
url: '/page-with-form'
})
Run Code Online (Sandbox Code Playgroud)
我希望当有人导航时路由正常工作,/page-with-form
但是当我在应用程序中有其他东西时会将用户重定向到该路由,并带有一些/page-with-form?error=true
类似这样的附加信息:
$state.go('page-with-form', '?error=true');
Run Code Online (Sandbox Code Playgroud) 我试图custom url
从我的java程序中调用一个,因此我使用了这样的东西:
URL myURL;
try {
myURL = new URL("CustomURI:");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我得到以下例外:
java.net.MalformedURLException:未知协议:来自java.net.URL的java.net.URL.(未知来源)中的CustomURI.(来自未知来源)位于com.demo.TestDemo的java.net.URL.(未知来源).主(TestDemo.java:14)
如果我URI
从浏览器触发它然后它按预期工作但如果我尝试Java Program
从那时调用它我得到上述异常.
编辑:
以下是我尝试的步骤(我肯定错过了一些东西,请告诉我):
第1步:添加自定义URI java.protocol.handler.pkgs
第2步:从URL触发自定义URI
码:
public class CustomURI {
public static void main(String[] args) {
try {
add("CustomURI:");
URL uri = new URL("CustomURI:");
URLConnection uc = uri.openConnection();
uc.connect();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void add( String handlerPackage ){
final String key = …
Run Code Online (Sandbox Code Playgroud) 假设我有一个类进行一些繁重的处理,使用多个集合进行操作.我想要做的是确保这样的操作不会导致内存不足甚至更好我想设置一个可以使用多少内存的阈值.
class MyClass()
{
public void myMethod()
{
for(int i=0; i<10000000; i++)
{
// Allocate some memory, may be several collections
}
}
}
class MyClassTest
{
@Test
public void myMethod_makeSureMemoryFootprintIsNotBiggerThanMax()
{
new MyClass().myMethod();
// How do I measure amount of memory it may try to allocate?
}
}
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?或者这不可能/不可行?
据我所知,从JDK 8开始,PermGen将成为java历史上的一个页面.一切都很美好......但新的内存布局会是什么样子?这会影响新平台上的GC吗?
我正在尝试将一个类重构为2个类.不幸的是,eclipse的提取类函数似乎只支持那些并非真正有用的变量.有没有办法提取方法或有插件吗?
我正在验证是否使用Mockito调用了一个函数,但是Mockito告诉我,我正在验证的函数从未被调用,并且其他函数被调用.但在我看来,我正在调用正确的功能......
这是我得到的错误的堆栈跟踪:
Wanted but not invoked:
relationshipAutoIndexer.getAutoIndex();
-> at org.whispercomm.manes.server.graph.DataServiceImplTest.testInitIndices(DataServiceImplTest.java:117)
However, there were other interactions with this mock:
-> at org.whispercomm.manes.server.graph.DataServiceImpl.updateIndexProperties(DataServiceImpl.java:136)
-> at org.whispercomm.manes.server.graph.DataServiceImpl.updateIndexProperties(DataServiceImpl.java:144)
-> at org.whispercomm.manes.server.graph.DataServiceImpl.updateIndexProperties(DataServiceImpl.java:148)
-> at org.whispercomm.manes.server.graph.DataServiceImpl.updateIndexProperties(DataServiceImpl.java:149)
-> at org.whispercomm.manes.server.graph.DataServiceImpl.initIndices(DataServiceImpl.java:121)
at org.whispercomm.manes.server.graph.DataServiceImplTest.testInitIndices(DataServiceImplTest.java:117)
Run Code Online (Sandbox Code Playgroud)
它出现在
verify(relAutoIndexer).getAutoIndex();
Run Code Online (Sandbox Code Playgroud)
下面显示的测试类代码.
这是我的代码(我倾向于意外地将事情遗漏.请问我任何你认为我缺少的代码,我会添加它):
public DataServiceImpl(GraphDatabaseService graphDb) {
super();
this.graphDb = graphDb;
unarchivedParent = new UnarchivedParent(graphDb.createNode());
archivedParent = new ArchivedParent(graphDb.createNode());
packetParent = new PacketParent(graphDb.createNode());
userParent = new UserParent(graphDb.createNode());
this.initIndices();
}
/**
* Initializes the node and relationship indexes.
*
* Updates the set of indexed …
Run Code Online (Sandbox Code Playgroud) java ×7
java-8 ×2
junit ×2
angularjs ×1
bdd ×1
collections ×1
concurrency ×1
eclipse ×1
javascript ×1
jbehave ×1
mockito ×1
protocols ×1
refactoring ×1
spock ×1
testing ×1
testng ×1
unit-testing ×1
uri ×1
url ×1
verify ×1