我意识到Java 8 lambda实现可能会发生变化,但在lambda build b39中,我发现只有当lambda表达式返回非void类型时才能省略大括号.例如,这编译:
public class Collections8 {
public static void main(String[] args) {
Iterable<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.filter(e -> e.length() > 4).forEach(e -> { System.out.println(e); });
}
}
Run Code Online (Sandbox Code Playgroud)
但删除这样的大括号:
names.filter(e -> e.length() > 4).forEach(e -> System.out.println(e));
Run Code Online (Sandbox Code Playgroud)
给出了错误
Collections8.java:6: error: method forEach in interface Iterable<T> cannot be applied to given types;
names.filter(e -> e.length() > 4).forEach(e -> System.out.println(e));
^
required: Block<? super String>
found: lambda
reason: incompatible return type void in lambda expression
where T is a …Run Code Online (Sandbox Code Playgroud) 我很好奇在同一个匿名类中创建java8 lambda实例的性能.(在win32 java build 1.8.0-ea-b106上执行测量).我创建了一个非常简单的示例,并测量了java是否new在创建lambda表达式时提出了一些运算符优化:
static final int MEASURES = 1000000;
static interface ICallback{
void payload(int[] a);
}
/**
* force creation of anonymous class many times
*/
static void measureAnonymousClass(){
final int arr[] = {0};
for(int i = 0; i < MEASURES; ++i){
ICallback clb = new ICallback() {
@Override
public void payload(int[] a) {
a[0]++;
}
};
clb.payload(arr);
}
}
/**
* force creation of lambda many times
*/
static void measureLambda(){
final int arr[] = …Run Code Online (Sandbox Code Playgroud) 我想为我的挥杆应用做出光滑的圆角,但我无法得到我想要的结果......
这是截图:
1. setShape()对于JFrame:

2. paintComponent()JPanel的覆盖方法,而不是使用setShape():

3. setBackground(new Color(0, 0, 0, 0))对于JFrame:

好吧,但文字质量有问题:
在第3步之前:

在第3步之后:

伙计们我很困惑,我已多次搜索,但没有任何帮助我...我该怎么办?请帮我
这是完整的代码:
public class WelcomePage extends JFrame {
private Point initialClick;
private boolean end = false;
private JLabel jLabelAppTitle;
private JPanel jPanelExit;
private JLabel jLabelHint;
private int r = 220, g = 0, b = 0;
private int r2 = 10, g2 = 10, b2 = 10;
private boolean flag = false;
public WelcomePage() {
initComponents();
// setShape(new RoundRectangle2D.Double(0, 0, …Run Code Online (Sandbox Code Playgroud) 正如您所知,在Java 8中,接口可以拥有静态方法,这些方法本身就具有实现.
正如我在相关教程中所读到的,实现此类接口的类可以使用其静态方法.但是,我有一个问题,在这里,我用一个比我更简单的例子来展示它
public interface Interface1{
public static void printName(){
System.out.println("Interface1");
}
}
Run Code Online (Sandbox Code Playgroud)
当我实现这样的接口
public class Class1 implements Interface1{
public void doSomeThing() {
printName();
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到编译错误.
The method printName() is undefined for the type Class1
Run Code Online (Sandbox Code Playgroud)
有什么问题?
我想排序seq1升序和seq2降序所以我这样做:
list = list.stream().sorted(comparing(AClass::getSeq1).thenComparing(
AClass::getSeq2).reversed()).collect(toList());
Run Code Online (Sandbox Code Playgroud)
但结果出来了,因为seq1和seq2都按降序排序.
我可以这样做以使seq1升序和seq2降序:
sorted(comparing(AClass::getSeq1)
.reversed().thenComparing(AClass::getSeq2).reversed()
Run Code Online (Sandbox Code Playgroud)
这是真正的正确方法吗?
在下面的代码示例中,做了::什么:
public static void main(String[] args) {
List<Integer> l = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
Integer s = l.stream().filter(Tests::isGT1)
.filter(Tests::isEven)
.map(Tests::doubleIt)
.findFirst()
.orElse(100);
System.out.println(s);
}
private static boolean isGT3(int number){
return number > 3;
}
private static boolean isEven(int number){
return number % 2 ==0;
}
private static int doubleIt(int number){
return number * 2;
}
Run Code Online (Sandbox Code Playgroud) 我试图了解是否有办法终止还原操作而不检查整个流,我无法找到方法.
用例大致如下:让一长串的Integers需要折叠成一个Accumulator.每个元素检查都可能很昂贵,所以在内部Accumulator,我对传入进行检查Accumulator,看看我们是否需要执行昂贵的操作 - 如果我们不这样做,那么我只需返回累加器.
对于小(呃)列表来说,这显然是一个很好的解决方案,但是巨大的列表会产生我不想要的不必要的流元素访问成本.
这是一个代码草图 - 仅假设连续减少.
class Accumulator {
private final Set<A> setA = new HashSet<>;
private final Set<B> setB = new HashSet<>;
}
class ResultSupplier implements Supplier<Result> {
private final List<Integer> ids;
@Override
public Result get() {
Accumulator acc = ids.stream().reduce(new Accumulator(), f(), (x, y) -> null);
return (acc.setA.size > 1) ? Result.invalid() : Result.valid(acc.setB);
}
private static BiFunction<Accumulator, Integer, Accumulator> f() {
return (acc, element) -> {
if …Run Code Online (Sandbox Code Playgroud) 假设我要删除我的所有非字母String.
String s = "abc-de3-2fg";
Run Code Online (Sandbox Code Playgroud)
我可以用a IntStream来做到这一点:
s.stream().filter(ch -> Character.isLetter(ch)). // But then what?
Run Code Online (Sandbox Code Playgroud)
为了将此流转换回String实例,我该怎么办?
另外,为什么我不能将a String视为类型的对象流Character?
String s = "abc-de3-2fg";
// Yields a Stream of char[], therefore doesn't compile
Stream<Character> stream = Stream.of(s.toCharArray());
// Yields a stream with one member - s, which is a String object. Doesn't compile
Stream<Character> stream = Stream.of(s);
Run Code Online (Sandbox Code Playgroud)
根据javadoc,其Stream创建签名如下:
Stream.of(T ...值)
我能想到的唯一(糟糕的)方式是:
String s = "abc-de3-2fg";
Stream<Character> stream = Stream.of(s.charAt(0), s.charAt(1), s.charAt(2), ...)
Run Code Online (Sandbox Code Playgroud)
当然,这还不够好......我错过了什么?
我是Java 8的新手,我遇到了Consumer java doc,它说:"预计消费者将通过副作用进行操作." 有人可以解释为什么这么说?
在Java中,Optional是作为public final class Optional<T> { ... }和不作为的密封层次Some和None.
为什么这不是这种情况?这是sealedJava 缺席的解决方法吗?背后有更深层次的推理吗?
如果您看一下方法实现,您会看到通过这种方式它具有丑陋的空检查:
public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
Objects.requireNonNull(mapper);
if (!isPresent())
return empty();
else {
return Optional.ofNullable(mapper.apply(value));
}
}
Run Code Online (Sandbox Code Playgroud)
它们不仅丑陋,而且如果你有一个更长的方法链,isPresent则需要在每次调用时进行评估,即使Optional从一开始就是空的.
如果我们可以通过链中传递固定的实现,则可以避免.
optional
.map(i -> i) // isPresent()
.map(Object::toString) // isPresent()
.map(String::length) // isPresent()
.map(...) // isPresent()
Run Code Online (Sandbox Code Playgroud)
为什么没有用于模拟空和非空案例的子类型?
我并没有特别问为什么 Optional是最终的,而是为什么它没有实现,Some和None许多其他语言一样,所以为什么可选的声明为最终类并不是真的有用.
java-8 ×10
java ×9
java-stream ×3
lambda ×3
closures ×1
comparator ×1
consumer ×1
fold ×1
inheritance ×1
interface ×1
jbutton ×1
optional ×1
performance ×1
reduce ×1
sorting ×1
string ×1
swing ×1