当滚动浏览java.util包的文档时,我很惊讶地发现它Optional<T>并且OptionalInt彼此没有任何关系.这似乎很难相信,因为它表明它们是不相关的类.
OptionalInt课程呢?为什么你不能使用Optional<Integer>?我认为这是因为它int是原始的,但没有OptionalChar这样的设计选择是不一致的.我正在学习更多关于Java 8及其功能的知识,我想用它做更多的练习.比方说,我有以下命令性代码,用于围绕屏幕边界包裹一个圆圈:
if (circle.getPosition().getX() > width + circle.getRadius()){
circle.getPosition().setX(-circle.getRadius());
}else if (circle.getPosition().getX() < -circle.getRadius()){
circle.getPosition().setX(width + circle.getRadius());
}
if (circle.getPosition().getY() > height + circle.getRadius()){
circle.getPosition().setY(-circle.getRadius());
}else if (circle.getPosition().getY() < -circle.getRadius()){
circle.getPosition().setY(height + circle.getRadius());
}
Run Code Online (Sandbox Code Playgroud)
java functional-programming imperative-programming declarative-programming java-8
我偶然发现了一些非常奇怪的代码,我很惊讶不会导致错误
public class WeirdCode {
public static int fooField = 42;
public WeirdCode getFoo(){
return null;
}
public static void main(String args[]) {
WeirdCode foo = new WeirdCode();
System.out.println(foo.getFoo().fooField);
}
}
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,它打印出42!谁能解释一下?
编写以下内容是否有更简洁,也许是单行方式:
ArrayList<Integer> myList = new ArrayList<>();
for (int i = 0; i < 100; i++){
myList.add(i);
}
Run Code Online (Sandbox Code Playgroud)
使用Java 8功能和功能上的insipred方法.我不期待像以下那样的Haskell解决方案:
ls = [1..100]
Run Code Online (Sandbox Code Playgroud)
但比传统的命令式风格更优雅.
在 ruby 中,我在 irb 中乱搞,发现两个代码示例应该工作相同但不
"a" * 4 #this is "aaaa"
4 * "a" #this is "String can't be coerced into a Fixnum"
Run Code Online (Sandbox Code Playgroud)
这不违反乘法的交换性质吗?
当属于变量的方法的引用被破坏时会发生什么?
public class Hey{
public double bar;
public Hey(){
bar = 2.0d;
}
public double square(double num){
return Math.pow(num , bar);
}
}
Function<Double, Double> square;
whatsGonnaHappen: {
Hey hey = new Hey();
square = hey::square;
}//is hey still kept around because its method is being referenced?
double ans = square.apply(23d);
Run Code Online (Sandbox Code Playgroud) 在Java 8中,您可以编写如下代码:
List<Integer> list = IntStream
.range(0, 100)
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
Run Code Online (Sandbox Code Playgroud)
如何创建自定义XStream类?例如,假设我有一个自然可以订购的类,你自然可以拥有一个对象的前辈和后继者.喜欢:
public class PurchaseOrder {
public long orderNumber;
public PurchaseOrder(){
orderNumber = 0L;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我可以有一个理论上的PurchaseOrderStream,您可以编写以下代码:
List<PurchaseOrder> list = PurchaseOrderStream
.range(0, 100)
.collect(Collectors.toCollection(ArrayList::new));
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?是否有我需要的任何类或接口PurchaseOrderStream到extend/ implement?我不是在寻找完整的源代码(虽然这样会很好),但只是推动正确的方向.
在编程中,我经常遇到需要将变量打印到控制台的情况.
int myVar = 23;
System.out.println("myVar" + myVar);
Run Code Online (Sandbox Code Playgroud)
我想通过将任务外包给Eclipse模板来自动完成任务.到目前为止,这是我准备的模板:
System.out.println((NAME OF VAR IN QUOTES?) + ${var});
Run Code Online (Sandbox Code Playgroud)
我怎么把名字var放在一个字符串中?
由于在这个问题回答在这里,一个变量内的范围case属于整个switch语句本身,而不仅仅是case。因此,这不会编译(重复的局部变量):
int key = 2;
switch (key) {
case 1:
String str = "1";
return str;
case 2:
String str = "2";
return str;
}
Run Code Online (Sandbox Code Playgroud)
我主要对两件事感兴趣...
我有以下数据类型。
data NestedList a = Elem a | List [NestedList a]
Run Code Online (Sandbox Code Playgroud)
不幸的是,创建这种数据类型的样本非常麻烦。
List [Elem 5, Elem 6, Elem 7, List [Elem 8, Elem 9, Elem 10]]
Run Code Online (Sandbox Code Playgroud)
我想创建一个fromList将使用任何类型的平面数组的辅助程序,并返回一个NestedList,该NestedList也是平面的但包含相同的数据。因此,我可以通过以下方式使用帮助程序制作上面的示例(不包括开始嵌套的部分):
fromList [5, 6, 7] == List [Elem 5, Elem 6, Elem 7]
Run Code Online (Sandbox Code Playgroud)
到目前为止,这就是我所拥有的。
fromList :: [a] -> NestedList a
fromList [] = List[]
fromList [a] = Elem a
fromList (x:xs) = List [Elem x] ++ List [fromList xs]
Run Code Online (Sandbox Code Playgroud)
错误消息是:
• Couldn't match expected type ‘[a0]’ with actual type ‘NestedList a’
• …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建自己的MP3音频文件。我将如何去做呢?
File mySong = new File("generated song.mp3");
FileOutputStream song = new FileOutputStream(mySong);
for (int n = 0; n < 3000; n++){
song.write(n % 256);
}
song.close();
Run Code Online (Sandbox Code Playgroud)
我尝试了上面的代码,但是Windows在尝试回放时给了我一个错误。我想象必须有一些开始和结束的字节序列,我需要将它们写入文件才能正确解码。那么,如何生成自己的.MP3文件?
我试图调用内建函数find以打印出子文件夹my-files中所有文本文件的内容。我知道可以使用更简单的方法来执行此操作,但是我需要使其与exec一起使用。我怀疑exec无法正确处理报价。我的初始代码如下:
fullCmd := "find my-files -maxdepth 1 -type f"
cmdParts := strings.Split(fullCmd, " ")
output, _ := exec.Command(cmdParts[0], cmdParts[1:]...).CombinedOutput()
fmt.Println("Output is...")
fmt.Println(string(output))
Run Code Online (Sandbox Code Playgroud)
这可以正常工作并打印出来
Output is...
my-files/goodbye.txt
my-files/badfile.java
my-files/hello.txt
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试开始添加“奇怪”字符时,它会崩溃。如果我将第一行更改为
fullCmd := "find my-files -maxdepth 1 -type f -iname \"*.txt\""
Run Code Online (Sandbox Code Playgroud)
什么都没打印出来。更糟糕的是,如果我将行更改为:
fullCmd := "find my-files -maxdepth 1 -type f -exec cat {} \\;"
Run Code Online (Sandbox Code Playgroud)
使用此标准输出查找错误:
Output is...
find: -exec: no terminating ";" or "+"
Run Code Online (Sandbox Code Playgroud)
我以为我正确地逃避了必要的角色,但我想不是。关于如何使命令起作用的任何想法?作为参考,当直接在命令行上输入此命令时,它确实满足我的要求:
find my-files -maxdepth 1 -type f -iname "*.txt" -exec cat {} \;
Run Code Online (Sandbox Code Playgroud)