使用Java 8,创建排序和分组的字符串列表的最简洁方法是什么?使用Lambdas和Collections and Streams框架显示旧方法和新方法.
您可以使用旧的(或新的)方式使用第三方库(流行的).
但是,我建议使用vanilla Java,因为它显示了Java 8中的语言更改带来的任务更改表.
Input: List<String>
Output: Map<Character<List<String>>
The key of map is 'A' to 'Z'
Each list in the map are sorted.
Run Code Online (Sandbox Code Playgroud)
它将被分类和分组,以便......
鉴于此列表:"啤酒","苹果","香蕉","凤梨","芒果","蓝莓"
甲Map意愿产生包含第一信作为密钥.地图中的值将List是以该键(字母)开头的所有单词的排序:
使用Java 8的新功能,转换a的所有值的最简洁方法是List<String>什么?
鉴于这种:
List<String> words = Arrays.asList("Apple", "Ananas", "Mango", "Banana", "Beer");
Run Code Online (Sandbox Code Playgroud)
我目前正在这样做:
for (int n = 0; n < words.size(); n++) {
words.set(n, words.get(n).toUpperCase());
}
Run Code Online (Sandbox Code Playgroud)
Java 8中的新Lambdas,Collections和Streams API如何帮助:
就地转换值(不创建新列表)
将值转换为新的结果列表.
例:
interface Outer {
default String get() {
return "hi";
}
class Inner {
String got() {
return get();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这会产生错误
java:非静态方法get()不能从静态上下文中引用.
内部接口/类始终是静态的; 与外部类不同,除非声明为静态,否则它是非静态的.
这就是今天和即将发布的Java 8中的事情.外部类和外部接口之间存在这种差异的根本原因是什么?
更新:阅读@ Radiodef的评论后,我将内部接口更改为内部类.外部类不能包含非静态内部接口,因此该示例令人困惑.无论如何,内心阶层真的是我想要的.
更新:供参考.这是完全合法的:
class Outer {
String get() {
return "hei";
}
class Inner {
String got() {
return get();
}
}
}
Run Code Online (Sandbox Code Playgroud) 在Java 8中,Stream类实现了AutoCloseable.这意味着应该显式关闭流实例.
我理解为什么文件处理程序和数据库连接是可关闭的.但为什么流?
为了减少由匿名类型的大量实例化引起的混乱,我正在探索利用Java 8 lambdas的可能性.
在生产环境中使用Java 8和lambdas之前的一个重要考虑因素是,使用lambda表达式的JDK8编译代码是否可以在早期版本的Java运行时上执行.我特别感兴趣的是JRE6和JRE7作为目标平台.
一方面,我理解lambdas只是包含一个方法的匿名类实例化的语法糖.另一方面,我不确定这种等价意味着为每个生成的字节码在JRE8以外的JVM版本中是相同的和/或兼容的.
例如,给定单方法接口:
public interface Action<T> {
public void perform(T argument);
}
Run Code Online (Sandbox Code Playgroud)
以下两个片段在功能上是等效的:
使用lambda:
final Action<String> y = i -> System.out.println(i);
Run Code Online (Sandbox Code Playgroud)
使用匿名类实例:
final Action<String> y = new Action<String>() {
@Override
public void perform(final String i) {
System.out.println(i);
}
};
Run Code Online (Sandbox Code Playgroud)
我的具体问题是两种结构的语义等价是否扩展到其编译表示的等价性.此外,如果它们确实等价地编译,那么这个等价是否表明lambda表达式的编译形式可以托管在早期版本的Java运行时而不进行修改?
我想使用Java 8新引入的函数对象将一些参数部分应用于遗留方法.
这是有问题的方法:
/**
* Appends a {@code character} a couple of {@code times} to a {@code string}.
*
* @return the string with the appended characters as a StringBuilder
*/
private static StringBuilder appendChar(char character, int times, String string) {
StringBuilder strBuilder = new StringBuilder(string);
for (int i = 0; i < times; i++) {
strBuilder.append(character);
}
return strBuilder;
}
Run Code Online (Sandbox Code Playgroud) 在程序中如
entities.stream().filter(m->m.getId()==id).findAny().get();
Run Code Online (Sandbox Code Playgroud)
这里entities是一个List.将所有库和其他SDK设置为Java 8.后,我们收到错误:
use -source 8 or higher to enable lambda expressions
Run Code Online (Sandbox Code Playgroud) 我对Interface和BeanInfo Introspector中的默认方法有一点问题.在这个例子中,有接口:Interface
public static interface Interface {
default public String getLetter() {
return "A";
}
}
Run Code Online (Sandbox Code Playgroud)
以及ClassA和ClassB两个类:
public static class ClassA implements Interface {
}
public static class ClassB implements Interface {
public String getLetter() {
return "B";
}
}
Run Code Online (Sandbox Code Playgroud)
在main方法应用程序中打印来自BeanInfo的PropertyDescriptors:
public static String formatData(PropertyDescriptor[] pds) {
return Arrays.asList(pds).stream()
.map((pd) -> pd.getName()).collect(Collectors.joining(", "));
}
public static void main(String[] args) {
try {
System.out.println(
formatData(Introspector.getBeanInfo(ClassA.class)
.getPropertyDescriptors()));
System.out.println(
formatData(Introspector.getBeanInfo(ClassB.class)
.getPropertyDescriptors()));
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
class
class, letter …Run Code Online (Sandbox Code Playgroud) 有没有什么简短的方法可以将java.time.ZonedDateTime转换为XMLGregorianCalendar?
也许我需要一些中间步骤,比如将ZonedDateTime转换为java.util.Date,但这会使代码过于混乱.
问题出现在JAX-WS Web服务中,datetime作为XMLGregorianCalendar传递.
我尝试使用parallelStream()带有Spring @Transactional注释的DAO 并得到这样的问题:
@Transactional
public void processCollection(Collection<Object> objects) {
objects.parallelStream()
.forEach(this::processOne); //throw exception
}
@Transactional
public void processOne(Object o) {
...
}
Run Code Online (Sandbox Code Playgroud)
工作正确:
@Transactional
public void processCollection(Collection<Object> objects) {
objects.stream()
.forEach(this::processOne); //work correctly
}
@Transactional
public void processOne(Object o) {
...
}
Run Code Online (Sandbox Code Playgroud)
例外:
org.hibernate.HibernateException: No Session found for current thread
org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978)
Run Code Online (Sandbox Code Playgroud)
我如何使用带@Transactional注释的方法parallelStream()?
更新 为什么会发生这种情况Spring事务管理器和多线程 但是我希望Spring 4支持java 8可以为此提供一些解决方案.有任何想法吗?