我试图在Java 8中了解泛型和通配符.但我无法理解为什么不能模拟这个存储库方法.代码非常简单,因此应该很容易重现.
我在"thenReturn"部分的"when"中得到了这个编译错误
The method thenReturn(Stream<capture#1-of ? extends Something>) in the type
OngoingStubbing<Stream<capture#1-of ? extends Something>> is not applicable
for the arguments (Stream<capture#3-of ? extends Something>)
Run Code Online (Sandbox Code Playgroud)
考试:
@Test
public void ItShourReturnStreamFromRepository() {
List<Something> list = new ArrayList<Something>();
list.add(new Something());
Stream<? extends Something> stream = list.stream();
when(someRepository.getStream()).thenReturn(stream);
}
Run Code Online (Sandbox Code Playgroud)
班级:
public class Something {}
Run Code Online (Sandbox Code Playgroud)
存储库:
public interface SomeRepository{
Stream<? extends Something> getStream();
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?谢谢!
Java Streams基于硬件上的并行数量.但是如果我想要始终拥有最大的并行度呢?
请考虑以下代码.我希望10个任务中的每个任务同时运行100毫秒.
long runUntil = System.currentTimeMillis() + 100;
IntStream.range(0, 10).parallel().forEach(i ->
{
int cnt = 0;
while(System.currentTimeMillis() < runUntil)
cnt++;
System.out.println(i + ": " + cnt);
});
Run Code Online (Sandbox Code Playgroud)
但是,我得到的结果是:
2: 56443
1: 67506
4: 74693
6: 70549
0: 0
3: 0
5: 0
7: 0
8: 0
9: 0
Run Code Online (Sandbox Code Playgroud)
因此,只有4个任务并行执行,第五个任务仅在前4个任务中的一个完成时开始.我希望所有任务大约在同一时间开始,而不是等待彼此.
我不同意它是Java 8并行流中的自定义线程池的副本,因为这个问题是关于阻止其他任务的慢速运行任务,而在我的情况下,我只是想知道如何(如果可以)最大化使用Stream API时的并行性.
使用Java Stream API,有没有办法进行额外的处理来调整传递给方法引用的任何值?
我举两个例子.
例1.
在第一个例子中,我从a开始Stream<Path>,我想返回一个Map<String, Path>地图中的键是使用另一个采用String文件名(而不是a Path)的函数处理文件名的版本.特别:
public Map<String, Path> createMap(Path sourceFolder, PathMatcher filter) {
return stream.filter(filter::matches)
.collect(Collectors.toMap(FilenameHelper::parseFilename, Function.identity()));
Run Code Online (Sandbox Code Playgroud)
parseFilename(String filename)采用String文件名,但当然方法引用获取Path.我想说些什么,FilenameHelper::parseFilename(((Path)Function.identity()).toFile().getName())但这不起作用(Eclipse说:"作业的左侧必须是变量").我可以通过创建一个新方法来解决这个问题,这个方法Path只需要做,return parseFilename(path.toFile().toName())但这并不酷.
例2.
在第二个例子中,我有rows一个List<List<String>>>表示数据表(行,然后是列)的表示.我有一个方法应该List<String>为每个第n行返回该表中特定列的组成.我想做的事情如下:
public List<String> getDataFromColumn(String columnName, int nth) {
/// Without a clause at ???, this returns a List<List<String>>
return IntStream.range(0, rows.size())
.filter(n -> n % nth == 0) // Get every nth row
.mapToObj(rows::get)
.???
.collect(Collectors.toList()); …Run Code Online (Sandbox Code Playgroud) AFAIK总结流的唯一方法BigDecimal是:
BigDecimal result = stream.reduce(BigDecimal.ZERO, BigDecimal::add);
Run Code Online (Sandbox Code Playgroud)
这里的问题是每次调用BigDecimal::add都会创建一个新的BigDecimal而不是更改一个可变类型.
是否有一个可变减少操作又名Collector为Stream<BigDecimal>?
根据:
https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
看起来它是可能的,但是尝试真正返回编译错误.由于我们无法使用静态方法实现接口,因此它更有意义.
public class SomeClass {
static public boolean getB(){
return false;
}
}
List<SomeClass> list = new ArrayList<>();
list.add(new SomeClass());
// below causes compile error, which I expect, however documentation indicates oherwise
list.stream().filter(SomeClass::getB).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
这是编译错误:
"此行的多个标记 - 应该以静态方式访问SomeClass类型的方法getB() - Stream类型中的方法过滤器(Predicate)不适用于参数(SomeClass :: getB)"
如果我正在编写一个过滤函数,我依赖于我的过滤器的外部变量.例如:
public List<String> getMatches(String search) {
return stringList.stream().filter(str -> str.contains(search)).collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
此过滤器取决于外部参数.我只是想知道如果你事先不知道标准,如何使用纯函数编写过滤器.
我想知道stream().toArray [x - > new Integer [x]]如何知道来自哪个数组的大小?我写了一个片段,其中我创建了一个大小为4的整数列表并过滤了值,并创建了一个长度为过滤流的数组,我无法看到任何方法来获取流的大小.
List<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
intList.add(3);
intList.add(4);
Integer[] array = intList.stream()
.filter(x -> x > 2)
.toArray(x -> {
System.out.println("x --> " + x);
return new Integer[x];
});
System.out.println("array length: " + array.length);
Run Code Online (Sandbox Code Playgroud)
上述代码的输出:
x --> 2
array length: 2
Run Code Online (Sandbox Code Playgroud)
最初,片段就像
Integer[] array = intList.stream()
.filter(x -> x > 2)
.toArray(x -> new Integer[x]);
Run Code Online (Sandbox Code Playgroud)
只是为了理解它传递的x的值,我必须改变它以在lambda中打印x
很想知道是否有更优雅的方法试图在Java 8中找到字符串中奇数位置的数字总和.
这是我目前的职能
/**
* Explode string into char array, get sum of ASCII values at odd positions and divide by 10 to convert
* to the integer value from that character
* @param ccNumber string
* @return int sum
*/
int calculateSumOfOddDigits(final String ccNumber) {
final char[] digits = ccNumber.toCharArray();
return (digits[0] + digits[2] + digits[4] + digits[6] + digits[8] + digits[10] + digits[12] + digits[14]) / 10;
}
Run Code Online (Sandbox Code Playgroud)
还不熟悉Streams和Java 8,想想也许你可以这样做:
ccNumber.chars().mapToObj(x -> (char) x {
..add odd position …Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题但是,我试图理解为什么以下代码:
public static void main(String[] args) {
List<String> testData = Lists.newArrayList("a", "b", "c");
Map<String, List<String>> grouped = testData.stream().collect(Collectors.groupingBy(item -> "z"));
System.out.println(grouped);
}
Run Code Online (Sandbox Code Playgroud)
会返回一张地图:
{z=[a, b, c]}
Run Code Online (Sandbox Code Playgroud)
我正在尝试按列表中没有的值对列表进行分段.因此,我希望回到以下方面:
{z=[]}
Run Code Online (Sandbox Code Playgroud)
在我的情况下,分类器函数并不那么简单,但简而言之,它可能会产生一个不在列表值之间的分类器值.
我在这里错过了什么?
我有以下结构:
class A {
List<B> bs;
}
class B {
List<C> cs;
}
class C {
List something.
}
Run Code Online (Sandbox Code Playgroud)
我List的A班,我要进去的所有元素的SUMM something名单.我试着做以下事情:
totalCount = as
.stream()
.map(a -> a.getBs()
.stream()
.mapToInt(b -> b.getSomething().size())
.sum());
Run Code Online (Sandbox Code Playgroud)
但那不编译.我的错误在哪里?
编译错误是:
Error:(61, 21) java: incompatible types: no instance(s) of type variable(s) R exist so that java.util.stream.Stream<R> conforms to java.lang.Integer