小编Flo*_*cht的帖子

Java 8 Stream indexOf基于谓词的方法

我刚刚遇到的情况是我需要知道列表中元素的索引(位置),但只有一个谓词表达式来标识元素.我看过像Stream这样的Stream函数

int index = list.stream().indexOf(e -> "TESTNAME".equals(e.getName()));
Run Code Online (Sandbox Code Playgroud)

但无济于事.当然,我可以像这样写:

int index = list.indexOf(list.stream().filter(e -> "TESTNAME".equals(e.getName()))
    .findFirst().get());
Run Code Online (Sandbox Code Playgroud)

但这会a)迭代列表两次(在最坏的情况下元素将是最后一个)和b)如果没有元素匹配谓词(我更喜欢-1索引)将失败.

我为此功能编写了一个实用工具方法:

public static <T> int indexOf(List<T> list, Predicate<? super T> predicate) {
    int idx = 0;
    for (Iterator<T> iter = list.iterator(); iter.hasNext(); idx++) {
        if (predicate.test(iter.next())) {
            return idx;
        }
    }

    return -1;
}
Run Code Online (Sandbox Code Playgroud)

但是,由于这似乎是一个非常简单的算法,我原本期望它在Java 8 Stream API中的某个地方.我只是想念它,还是真的没有这样的功能?(额外的问题:如果没有这样的方法,是否有充分的理由?在函数式编程中使用索引可能是反模式吗?)

java java-8 java-stream

6
推荐指数
1
解决办法
1716
查看次数

Linux平台中Java代码的调用约定是什么?

我们知道调用约定"前六个整数或指针参数在寄存器RDI,RSI,RDX,RCX(Linux内核接口中的R10:124),R8和R9"中传递给c/c ++代码Linux平台基于以下文章. https://en.wikipedia.org/wiki/X86_calling_conventions#x86-64_calling_conventions

然而,Linux平台中Java代码的调用约定是什么(假设JVM是热点)?以下是示例,什么寄存器存储这四个参数?

protected void caller( ) {
callee(1,"123", 123,1)
}

protected void callee(int a,String b, Integer c,Object d) {

}
Run Code Online (Sandbox Code Playgroud)

java jvm

4
推荐指数
1
解决办法
1129
查看次数

标签 统计

java ×2

java-8 ×1

java-stream ×1

jvm ×1