我希望能够像这样使用Stream :: flatMap
public static List<String> duplicate(String s) {
List<String> l = new ArrayList<String>();
l.add(s);
l.add(s);
return l;
}
listOfStrings.stream().flatMap(str -> duplicate(str)).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但是我得到以下编译器错误
Test.java:25:错误:不兼容的类型:无法推断类型变量R listOfStrings.stream().flatMap(str - > duplicate(str)).collect(Collectors.toList());
(参数不匹配; lambda表达式中的错误返回类型List无法转换为Stream)
其中R,T是类型变量:R extends方法flatMap中声明的Object(Function>)T extends Interface在Interface Stream中声明的Object
在scala我可以做我认为相同的事情
scala> List(1,2,3).flatMap(duplicate(_))
res0: List[Int] = List(1, 1, 2, 2, 3, 3)
Run Code Online (Sandbox Code Playgroud)
为什么这不是java中flatMap的有效用法?
嘿,社区我想知道是否有可能创建一个程序,允许用户从硬盘驱动器(桌面,文档文件夹,视频文件夹)的任何位置拖动文件,并将其放入程序的窗口.
我正在创建一个媒体播放器,我希望能够通过将MP4拖放到窗口中来播放视频.我是否需要将文件存储在变量中,或者只将文件的位置存储到变量中.此外,重要的是我保持对跨平台的支持.
我正在使用JavaFx与java 7更新79 jdk.
提前致谢.
我喜欢番石榴,我会继续使用番石榴.但是,在有意义的地方,我尝试使用Java 8中的"新东西" .
"问题"
让我们说我想加入url属性String.在Guava我会这样做:
Map<String, String> attributes = new HashMap<>();
attributes.put("a", "1");
attributes.put("b", "2");
attributes.put("c", "3");
// Guava way
String result = Joiner.on("&").withKeyValueSeparator("=").join(attributes);
Run Code Online (Sandbox Code Playgroud)
凡result为a=1&b=2&c=3.
题
在Java 8(没有任何第三方库)中,最优雅的方法是什么?
我已经创建了计算字母表中每个字符的方法.我正在学习流(函数式编程)并尝试尽可能多地使用它们,但在这种情况下我不知道该怎么做:
private Map<Character, Integer> numerateAlphabet(List<Character> alphabet) {
Map<Character, Integer> m = new HashMap<>();
for (int i = 0; i < alphabet.size(); i++)
m.put(alphabet.get(i), i);
return m;
}
Run Code Online (Sandbox Code Playgroud)
那么,如何使用Java 8流重写它呢?
Java 8添加了一个新功能,通过它我们可以在接口中提供方法实现.在Spring 4中是否有任何方法可以在接口中注入bean,可以在方法体内使用?下面是示例代码
public interface TestWiring{
@Autowired
public Service service;// this is not possible as it would be static.
//Is there any way I can inject any service bean which can be used inside testWiringMethod.
default void testWiringMethod(){
// Call method of service
service.testService();
}
}
Run Code Online (Sandbox Code Playgroud) 我刚刚安装了Ubuntu 15.10和他们的openjdk-8-jdk(通过apt-get).
现在我错过了cacerts文件.
通常的位置有一个链接:
ls -l /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/cacerts
lrwxrwxrwx 1 root root 27 Oct 22 01:47 /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/cacerts -> /etc/ssl/certs/java/cacerts
Run Code Online (Sandbox Code Playgroud)
但在/ etc/ssl/certs/java/cacerts中没有任何内容:
stat /etc/ssl/certs/java/cacerts
stat: cannot stat ‘/etc/ssl/certs/java/cacerts’: No such file or directory
Run Code Online (Sandbox Code Playgroud) 我有一个Java程序(使用JDK 7u80编译),它广泛使用"JavaScript"ScriptEngine(JSR-223).我注意到,与Java 7运行时环境(JRE 7u80)相比,在Java 8运行时环境(JRE 8u65)下执行时,我的程序运行速度极慢.
我已经整理了以下SSCCE来演示问题,然后在同一台Windows PC上的Java 7和Java 8下执行它:
import javax.script.*;
public class SSCCE {
public SSCCE() {
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine js = sem.getEngineByName("JavaScript");
long t = 0;
int i = 0;
String gJs = "function ip2long(ip) {";
gJs += "var aIP = ip.split(\".\");";
gJs += "return (aIP[0] * Math.pow(256, 3)) + (aIP[1] * Math.pow(256, 2)) + (aIP[2] * 256) + (aIP[3] * 1);";
gJs += "}";
gJs += "function long2ip(l) {";
gJs += "if (!isNaN(l) …Run Code Online (Sandbox Code Playgroud) 如何通过每个条目限制groupBy?
例如(基于此示例:stream groupBy):
studentClasses.add(new StudentClass("Kumar", 101, "Intro to Web"));
studentClasses.add(new StudentClass("White", 102, "Advanced Java"));
studentClasses.add(new StudentClass("Kumar", 101, "Intro to Cobol"));
studentClasses.add(new StudentClass("White", 101, "Intro to Web"));
studentClasses.add(new StudentClass("White", 102, "Advanced Web"));
studentClasses.add(new StudentClass("Sargent", 106, "Advanced Web"));
studentClasses.add(new StudentClass("Sargent", 103, "Advanced Web"));
studentClasses.add(new StudentClass("Sargent", 104, "Advanced Web"));
studentClasses.add(new StudentClass("Sargent", 105, "Advanced Web"));
Run Code Online (Sandbox Code Playgroud)
此方法返回简单组:
Map<String, List<StudentClass>> groupByTeachers = studentClasses
.stream().collect(
Collectors.groupingBy(StudentClass::getTeacher));
Run Code Online (Sandbox Code Playgroud)
如果我想限制返回的集合怎么办?让我们假设我只想要每个老师的前N个课程.怎么做到呢?
我正在阅读 JDK 11 中的 LinkedHashMap 源代码,我发现了一段死代码(我不确定)
众所周知,LinkedHashMap 使用双向链表来保存所有元素的顺序。它有一个成员叫做 accessOrder
final boolean accessOrder;
Run Code Online (Sandbox Code Playgroud)
默认情况下它是 false,但如果它被设置为 true,每次运行时get,它都会将它到达的元素移动到链表的末尾。这就是函数afterNodeAccess所做的。
//if accessOrder were set as true, after you visit node e, if e is not the end node of the linked list,
//it will move the node to the end of the linkedlist.
void afterNodeAccess(Node<K, V> e) {
LinkedHashMap.Entry<K, V> last;
if(accessOrder && (last = tail) != e) {
//if enter `if` ?it indicates that e is not the end of …Run Code Online (Sandbox Code Playgroud) 我正在研究功能组合并有一个例子:
Function<String, String> test = (s) -> s.concat("foo");
String str = test.andThen(String::toUpperCase).apply("bar");
Run Code Online (Sandbox Code Playgroud)
该示例按预期编译并运行。但是,如果我使用 更改组合的顺序compose(),则需要显式转换:
String str = test.compose((Function <String, String>)
String::toUpperCase).apply("bar");
Run Code Online (Sandbox Code Playgroud)
如果没有显式转换String::toUpperCase为Function <String, String>,则会出现编译器错误:
Error:
incompatible types: cannot infer type-variable(s) V
(argument mismatch; invalid method reference
incompatible types: java.lang.Object cannot be converted to java.util.Locale)
String s = test.compose(String::toUpperCase).apply("bar");
^-------------------------------^
Run Code Online (Sandbox Code Playgroud)
问题是为什么compose()需要显式强制转换,而andThen()在这种情况下不需要?
java-8 ×10
java ×8
java-stream ×4
collectors ×1
dead-code ×1
dictionary ×1
flatmap ×1
grouping ×1
java-11 ×1
java-7 ×1
javafx ×1
jsse ×1
media-player ×1
openjdk ×1
performance ×1
scriptengine ×1
spring-4 ×1
ubuntu ×1
ubuntu-15.10 ×1