Scala编程:综合循序渐进指南,第2版,第546页:
在Iterable下面的继承层次结构中,您会发现三个特征:Seq,Set和Map.这三个特征的一个共同方面是它们都使用apply和isDefinedAt方法实现PartialFunction特性.
但是,此代码无法编译(尝试2.8.2和2.10.2):
Set(1, 2, 3).isDefinedAt(1)
Run Code Online (Sandbox Code Playgroud)
有错误:
value isDefinedAt is not a member of scala.collection.immutable.Set[Int]
Run Code Online (Sandbox Code Playgroud)
这是书中的错误吗?
我们正在迁移旧代码,实际上相当旧的代码到 Java 11。我在编译其中一个类时遇到问题。示例代码是:
package XXXX;
import java.lang.ref.*;
import sun.security.action.*;
import java.security.*;
import java.io.*;
class Converters
{
private static Object lock;
private static String converterPackageName;
private static String defaultEncoding;
public static final int BYTE_TO_CHAR = 0;
public static final int CHAR_TO_BYTE = 1;
private static final String[] converterPrefix;
private static SoftReference[] classCache;
static /* synthetic */ Class class$sun$io$Converters;
private static String getConverterPackageName() {
final String converterPackageName = Converters.converterPackageName;
if (converterPackageName != null) {
return converterPackageName;
}
String converterPackageName2 = AccessController.doPrivileged((PrivilegedAction<String>)new GetPropertyAction("file.encoding.pkg")); …Run Code Online (Sandbox Code Playgroud) 我有一个DTO看起来像-
@Getter
@Builder
class Person{
private String id;
private String name;
private Integer age;
}
Run Code Online (Sandbox Code Playgroud)
创建一个新的arraylist为-
List<Person> persons = new ArrayList<Person>();
persons.add(Person.builder().id("001").name("alpha").build());
persons.add(Person.builder().id("002").name("beta").build());
persons.add(Person.builder().id("003").name("gamma").build());
Run Code Online (Sandbox Code Playgroud)
另一个列表存在-
List<Person> ages = new ArrayList<Person>();
ages.add(Person.builder().id("001").age(25).build());
ages.add(Person.builder().id("002").age(40).build());
Run Code Online (Sandbox Code Playgroud)
什么是Java中8得到的一个子集的最佳方式persons,其中person.id().equals(age.id())每个项目person中persons,并age在ages?
考虑一个代表简单单元格的类:
class Cell {
private int x;
Cell(int x) {
this.x = x;
}
int getX() {
return x;
}
void setX(int x) {
this.x = x;
}
}
Run Code Online (Sandbox Code Playgroud)
如果要使其成为线程安全的,是否应该仅使方法同步或使构造函数同步?
class Cell {
private int x;
Cell(int x) {
synchronized(this) { // <- Is this synchronization necessary?
this.x = x;
}
}
synchronized int getX() {
return x;
}
synchronized void setX(int x) {
this.x = x;
}
}
Run Code Online (Sandbox Code Playgroud)
如果是,为什么构造函数中没有synchronized块java.util.Vector?
有:
case class A(x: Int) {
def >>= (y: A) = A(x * y.x)
def >> (y: A) = A(x + y.x)
}
Run Code Online (Sandbox Code Playgroud)
既然>>=和>>都开始>,它们应该具有相同的优先级和左关联性.
然而:
A(5) >>= A(3) >> A(2)
Run Code Online (Sandbox Code Playgroud)
返回25.
但应该返回17,因为:
A(5) >>= A(3) >> A(2) == (A(5) >>= A(3)) >> A(2) == 17
Run Code Online (Sandbox Code Playgroud)
看起来Scala编译器将parantheses放在错误的位置.
错误?
为什么我不能这样写:
data Color = R | G | B deriving Show
showColor :: Show Color
showColor = Show Color
main = do
putStrLn (showColor.show R)
putStrLn (showColor.show G)
Run Code Online (Sandbox Code Playgroud)
为什么类的实例不是Haskell中的一等公民?
我不明白试图解决稳定排序算法的根本问题.
Arrays.sort(Object[]) Javadoc说:
这种类型保证是稳定的:相同的元素不会因排序而重新排序.
但如果元素是平等的,它们就不会彼此分裂!如果你交换两个相等的元素,这不应该影响任何东西.这是平等的定义.
那么,为什么我们需要稳定呢?
更新:我的问题是关于Collections.sort(List<T>)/ Objects.sort(Object[])方法,不是Collections.sort(List<T>, Comparator<T>),Objects.sort(Comparator<T>).后者有点不同.但是它们仍然不需要稳定性:如果你想要可预测的化合物排序,那么你就可以创建合适的化合物比较器.
我创建了一个在 SWT 中设置图像的按钮。我想删除按钮的边框。所以它看起来像一个标签。请帮助任何人。
下面的代码片段:
breakNodeButton = new Button(this, SWT.TRANSPARENT);
breakNodeButton.setBackground(new Color(getDisplay(), 204, 204, 204));
Image breakNodeLabelImg = ...
breakNodeButton.setImage(breakNodeLabelImg);
Run Code Online (Sandbox Code Playgroud) double average = LongStream
.of(-4480186093928204294L, -1340542863544260591L, -6004296286240039273L)
.average()
.getAsDouble()
Run Code Online (Sandbox Code Playgroud)
这会返回,2.20723960999901594E18但我希望它会返回-3.9416750812375014E18.
另一方面,这会返回正确的结果(-1.4628605853333333E9):
double average = IntStream
.of(-1282256274, -1645263673, -1461061809)
.average()
.getAsDouble()
Run Code Online (Sandbox Code Playgroud)
为什么IntStream.average()总是返回正确的平均值,LongStream.average()有时却没有?
我想限制在我的项目中使用某些库方法(因此当我使用这些方法时会收到警告)。IDEA 中是否有自定义弃用方法的列表?
示例:我永远不应该使用PrimitiveIterator.OfInt.next(). 我应该nextInt()改用。每次使用时我都想看到警告next()。
java ×7
java-8 ×2
java-stream ×2
scala ×2
concurrency ×1
haskell ×1
java-11 ×1
java-9 ×1
sorting ×1
swt ×1
synchronized ×1