小编Oli*_*POP的帖子

Java 8中的::(双冒号)运算符

我正在探索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

java java-8

912
推荐指数
8
解决办法
31万
查看次数

ArrayList的ConcurrentModificationException

我有以下代码:

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 collections concurrency

79
推荐指数
5
解决办法
11万
查看次数

如何通过引用传递原始数据类型?

如何在java中通过引用传递原始类型?例如,如何将int传递给可修改的方法?

java pass-by-reference primitive-types

43
推荐指数
4
解决办法
3万
查看次数

如何使用AngularJS UI路由器.go()将搜索参数添加到URL?

有没有办法在使用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)

javascript angularjs angular-ui-router

34
推荐指数
2
解决办法
2万
查看次数

注册并使用自定义java.net.URL协议

我试图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)

java url uri protocols

33
推荐指数
2
解决办法
3万
查看次数

Java单元测试:如何测量方法调用的内存占用量

假设我有一个类进行一些繁重的处理,使用多个集合进行操作.我想要做的是确保这样的操作不会导致内存不足甚至更好我想设置一个可以使用多少内存的阈值.

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)

这样做的正确方法是什么?或者这不可能/不可行?

java junit testng unit-testing out-of-memory

31
推荐指数
3
解决办法
2万
查看次数

JDK 8内存布局和垃圾回收

据我所知,从JDK 8开始,PermGen将成为java历史上的一个页面.一切都很美好......但新的内存布局会是什么样子?这会影响新平台上的GC吗?

java garbage-collection java-8

30
推荐指数
2
解决办法
3万
查看次数

java的行为驱动开发 - 使用什么框架?

对于正在进行的项目和改进我们的开发过程,我们考虑采用TDD作为开发理念.在研究最佳实践以及如何向同事/开发人员"推销"新方法的过程中,我遇到了BDD,并发现它更适合我们需要的东西,并且不知何故是TDD的下一次迭代.问题是到目前为止我只尝试过Dan North,JBehave开发的工具,我不能说我很惊讶.

设置在我看来很麻烦,我找不到非常合适的文档.在另一方面我试着也斯波克的常规工具,到现在我挺喜欢.

问:有没有适合BDD的工具?
问:你会使用spock来处理引入另一种语言的开销吗?

testing bdd automated-tests jbehave spock

29
推荐指数
3
解决办法
3万
查看次数

有没有办法在eclipse中使用方法提取类?

我正在尝试将一个类重构为2个类.不幸的是,eclipse的提取类函数似乎只支持那些并非真正有用的变量.有没有办法提取方法或有插件吗?

eclipse refactoring eclipse-plugin

15
推荐指数
2
解决办法
6579
查看次数

Mockito - "通缉但未调用;但是还有其他与此模拟的交互"错误

我正在验证是否使用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 junit verify mockito

13
推荐指数
1
解决办法
3万
查看次数