我曾经把java方法引用看作是一个语法糖,它是作为lambda表达式的一个补充而引入的.但显然事实并非如此.
在下面的示例中,与lambda表达式不同,方法引用会产生错误.
有人可以解释一下奇怪的行为吗?
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
System.out.println(getMapUsingLanmdaApproach(MyEnum.class)); // works as expected: {1=A, 2=B}
System.out.println(getMapUsingMethodReferenceApproach(MyEnum.class)); // throws java.lang.BootstrapMethodError
}
static <K, V extends Enum<?> & HasProperty<K>> Map<K, V> getMapUsingLanmdaApproach(Class<V> aClass) {
return Stream.of(aClass.getEnumConstants())
.collect(Collectors.toMap(e -> e.getProperty(), Function.identity()));
}
static <K, V extends Enum<?> & HasProperty<K>> Map<K, V> getMapUsingMethodReferenceApproach(Class<V> aClass) {
return Stream.of(aClass.getEnumConstants())
.collect(Collectors.toMap(HasProperty::getProperty, Function.identity()));
}
}
enum MyEnum implements HasProperty<Integer> {
A(1),
B(2);
private final Integer …Run Code Online (Sandbox Code Playgroud) 我正在尝试EntityManagerFactory为一个 Spring Boot 应用程序配置两个。其中每一个EntityManagerFactory都应该使用不同的数据库。如果将其中之一用作默认值(例如,当没有提供明确的信息来说明应使用哪一个时),那就太好了。
这是我的代码:
pkg/Application.java
package pkg;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
pkg/PrimaryDbConfig.java
package pkg;
import org.h2.jdbcx.JdbcDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
public class PrimaryDbConfig {
public static final String DB = "primary";
@Bean
@Primary
public DataSource primaryDataSource() {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:file:~\\db\\test;AUTO_SERVER=TRUE");
ds.setUser("sa");
ds.setPassword("sa");
return ds; …Run Code Online (Sandbox Code Playgroud) 有什么方法可以在运行时使用-p或--module-path参数获取模块路径中包含的目录列表,类似于我使用 获取所有类路径目录的方式System.getProperty("java.class.path")?
我知道在Java 8 HashMap中针对分布不佳进行了优化hashCode.并且在超过阈值的情况下,它将桶中的节点从链表重建为树.此外,它表明这种优化不适用于不具有可比性的密钥(在性能方面没有得到改善).在下面的示例中,我没有放入Comparable密钥HashMap
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
class Main {
public static void main(String[] args) throws InterruptedException {
Map<Key, Integer> map = new HashMap<>();
IntStream.range(0, 15)
.forEach(i -> map.put(new Key(i), i));
// hangs the application to take a Heap Dump
TimeUnit.DAYS.sleep(1);
}
}
final class Key {
private final int i;
public Key(int i) {
this.i = i;
}
@Override
public boolean equals(Object o) {
if (this == …Run Code Online (Sandbox Code Playgroud) 我需要编写一个插入查询,其中一个值是3个元素的数组。我试图这样做:
INSERT INTO some_table (id, col) VALUES (1, '{1, 2, 3}');
Run Code Online (Sandbox Code Playgroud)
我用于'col'值的sintax是PostrgreSQL中的有效数组文字,但是H2将其视为数组的单个值,而不是具有3个元素的数组。
表DDL:
CREATE TABLE some_table (
id INT PRIMARY KEY ,
col ARRAY
);
Run Code Online (Sandbox Code Playgroud) java ×4
generics ×1
h2 ×1
hashmap ×1
java-8 ×1
java-9 ×1
java-module ×1
java-stream ×1
jpa ×1
lambda ×1
module-path ×1
spring-boot ×1
sql ×1