我一直在打击这个问题一段时间,并认为可能会有一些新鲜的眼睛看到这个问题; 谢谢你的时间.
import java.util.*;
class Tbin<T> extends ArrayList<T> {}
class TbinList<T> extends ArrayList<Tbin<T>> {}
class Base {}
class Derived extends Base {}
public class Test {
public static void main(String[] args) {
ArrayList<Tbin<? extends Base>> test = new ArrayList<>();
test.add(new Tbin<Derived>());
TbinList<? extends Base> test2 = new TbinList<>();
test2.add(new Tbin<Derived>());
}
}
Run Code Online (Sandbox Code Playgroud)
使用Java 8.在我看来,直接创建容器test
就等同于容器test2
,但编译器说:
Test.java:15: error: no suitable method found for add(Tbin<Derived>)
test2.add(new Tbin<Derived>());
^
Run Code Online (Sandbox Code Playgroud)
我怎么写Tbin
,TbinList
所以最后一行是可以接受的?
请注意,我实际上将添加类型Tbin
s,这就是我Tbin<Derived>
在最后一行中指定的原因.
我需要生成所有可能的配对,但是约束条件是特定配对仅在结果中出现一次.例如:
import itertools
for perm in itertools.permutations(range(9)):
print zip(perm[::2], perm[1::2])
Run Code Online (Sandbox Code Playgroud)
生成所有可能的双配对排列; 这是输出的一小部分:
...
[(8, 4), (7, 6), (5, 3), (0, 2)]
[(8, 4), (7, 6), (5, 3), (1, 0)]
[(8, 4), (7, 6), (5, 3), (1, 2)]
[(8, 4), (7, 6), (5, 3), (2, 0)]
[(8, 4), (7, 6), (5, 3), (2, 1)]
[(8, 5), (0, 1), (2, 3), (4, 6)]
[(8, 5), (0, 1), (2, 3), (4, 7)]
[(8, 5), (0, 1), (2, 3), (6, 4)]
[(8, 5), (0, …
Run Code Online (Sandbox Code Playgroud) 我想转换这个:
static Set<String> methodSet(Class<?> type) {
Set<String> result = new TreeSet<>();
for(Method m : type.getMethods())
result.add(m.getName());
return result;
}
Run Code Online (Sandbox Code Playgroud)
哪个编译得很好,更现代的Java 8流版本:
static Set<String> methodSet2(Class<?> type) {
return Arrays.stream(type.getMethods())
.collect(Collectors.toCollection(TreeSet::new));
}
Run Code Online (Sandbox Code Playgroud)
这会产生错误消息:
error: incompatible types: inference variable T has incompatible bounds
.collect(Collectors.toCollection(TreeSet::new));
^
equality constraints: String,E
lower bounds: Method
where T,C,E are type-variables:
T extends Object declared in method <T,C>toCollection(Supplier<C>)
C extends Collection<T> declared in method <T,C>toCollection(Supplier<C>)
E extends Object declared in class TreeSet
1 error
Run Code Online (Sandbox Code Playgroud)
我可以看到为什么编译器会遇到这个问题---没有足够的类型信息来确定推理.我看不到的是如何解决它.有人知道吗?
以下代码在JUnit4中开始,并且大部分都被翻译成JUnit5,除了main()
.我这样写它的原因是我正在演示TDD并且我有多个版本的StringInverter
实现,每个版本都实现了更多的功能并通过了更多的测试.这是StringInverter
界面:
interface StringInverter {
public String invert(String str);
}
Run Code Online (Sandbox Code Playgroud)
这里是几乎与JUnit5类编译:
import java.util.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.platform.runner.JUnitPlatform;
public class StringInverterTest {
static StringInverter inverter;
@Test
public final void basicInversion_Succeed() {
String in = "Exit, Pursued by a Bear.";
String out = "eXIT, pURSUED BY A bEAR.";
assertEquals(inverter.invert(in), out);
}
@Test
public final void basicInversion_Fail() {
expectThrows(RuntimeException.class, () -> {
assertEquals(inverter.invert("X"), "X");
});
}
@Test
public final void allowedCharacters_Fail() {
expectThrows(RuntimeException.class, () -> …
Run Code Online (Sandbox Code Playgroud) invokeAll()
直到Callable
提交中的所有sCollection
都完成后才返回,那么结果Future
s的原因是什么?
我问,因为我想知道我是否可以使用文本文件作为简单应用程序的数据存储.如果每个处理程序运行完成,那么看起来我应该能够在该请求期间修改文本文件而不用担心冲突,假设我在每个请求结束时关闭文件.
这可行吗?为了在Flask应用程序中将文本文件用作数据存储,我需要做些什么特别的事情吗?
我有两个问题:1.Callable
在Java 8中运行作为任务的最简单的规范形式是什么,捕获和处理结果?2.在下面的示例中,在所有任务完成之前,保持主进程打开的最佳/最简单/最清晰的方法是什么?
这是我到目前为止的例子 - 这是Java 8中最好的方法还是有更基本的东西?
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
public class SimpleTask implements Supplier<String> {
private SplittableRandom rand = new SplittableRandom();
final int id;
SimpleTask(int id) { this.id = id; }
@Override
public String get() {
try {
TimeUnit.MILLISECONDS.sleep(rand.nextInt(50, 300));
} catch(InterruptedException e) {
System.err.println("Interrupted");
}
return "Completed " + id + " on " +
Thread.currentThread().getName();
}
public static void main(String[] args) throws Exception {
for(int i = 0; i < 10; i++)
CompletableFuture.supplyAsync(new …
Run Code Online (Sandbox Code Playgroud) 我发现的所有示例都使用其他类型信息来推断其类型Stream.empty()
.似乎应该有语法允许我直接投射它.这个例子有效:
import java.util.*;
import java.util.stream.*;
class OptionalBasics {
static void test(Optional<String> optString) {
if(optString.isPresent())
System.out.println(optString.get());
else
System.out.println("Nothing inside!");
}
public static void main(String[] args) {
Stream<String> s = Stream.empty();
test(s.findFirst());
test(Stream.of("Epithets").findFirst());
}
}
/* Output:
Nothing inside!
Epithets
*/
Run Code Online (Sandbox Code Playgroud)
但请注意,我必须s
单独创建才能提供类型信息Stream.empty()
.我想在调用中创建它
test()
,就像这样(这不起作用):
test(Stream<String>.empty().findFirst());
Run Code Online (Sandbox Code Playgroud)
是否有用于设置调用类型的语法Stream.empty()
?
我首先要承认擦除可能会使这变得不可能.这是我正在尝试做的,这不起作用:
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Suppliers {
public static <CollectionType, T> CollectionType<T>
fill(CollectionType colltype, Supplier<T> gen, int n) {
return Stream.generate(gen)
.limit(n)
.collect(colltype::new, colltype::add, colltype::addAll);
}
}
Run Code Online (Sandbox Code Playgroud)
目标是生成一个实例,CollectionType
并使用n
项目填充它gen
.这种形式collect()
确实实例化了一个新集合,但我不清楚该类型是否可以通过参数列表传递fill()
.fill()
只要它实例化一个新集合并填充它并返回结果,该方法就不必完全按照这种方式查看.