标签: java-8

在 Jooq 中集成 Hikari Pool

我试图在我当前的项目(用 Java 编写)中第一次使用 jooq。我想在 Jooq DSL 上下文中集成 Hikari 连接池。我想明确定义最大连接数。任何推荐的文章,我可以遵循的代码来完成它。

谢谢

我已经设置了 jooq,现在我可以为我的数据库生成代码。

   public  static void init() {

        Target l_target =   new Target();
        System.out.println("My directory is::"+l_target.getPackageName());
        l_target.setDirectory("src/main/java");
        l_target.setPackageName("com.my.paas.css.entity");
        Configuration configuration = new Configuration()
                .withJdbc(new Jdbc()
                        .withDriver("com.mysql.jdbc.Driver")
                        .withUrl("jdbc:mysql://localhost:3306/paas")
                        .withUser("root"))
                .withGenerator(new Generator()
                        .withDatabase(new Database()
                                .withName("org.jooq.meta.mysql.MySQLDatabase")
                                .withIncludes(".*")
                                .withExcludes("")
                                .withInputSchema("paas"))
                        .withTarget(l_target));

        try {
            GenerationTool.generate(configuration);
        } catch (Exception ex) {
            System.out.println();
            ex.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

mysql database jooq java-8

1
推荐指数
1
解决办法
1943
查看次数

如何通过避免手动睡眠来对 CompletableFuture.thenAccept() 进行单元测试

如何避免单元测试中的手动睡眠。假设在下面的代码中,Processnotify需要大约 5 秒的时间进行处理。所以为了完成处理,我添加了 5 秒的睡眠。

public class ClassToTest {

    public ProcessService processService;
    public NotificationService notificationService;

    public ClassToTest(ProcessService pService ,NotificationService nService ) {
        this.notificationService=nService;
        this.processService = pService;
    }
    public CompletableFuture<Void> testMethod()
    {
          return CompletableFuture.supplyAsync(processService::process)
                        .thenAccept(notificationService::notify);
    }

}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来处理这个问题?

 @Test
    public void comletableFutureThenAccept() {
         CompletableFuture<Void> thenAccept = 
          sleep(6);
          assertTrue(thenAccept.isDone());  
          verify(mocknotificationService, times(1)).notify(Mockito.anystring());
    }
Run Code Online (Sandbox Code Playgroud)

java-8 completable-future

1
推荐指数
1
解决办法
1487
查看次数

将字符串解析为 LocalDateTime 时出现 DateTimeParseException

以下工作完美:

String startDate = "2019-10-12T00:00:00.000-07:00"

LocalDateTime startDateTime = LocalDateTime.parse(startDate,
    DateTimeFormatter.ISO_ZONED_DATE_TIME);
Run Code Online (Sandbox Code Playgroud)

但是,对于以下代码:

String startDate = "2019-10-12T00:00:00.000+07:00"

LocalDateTime startDateTime = LocalDateTime.parse(startDate,
    DateTimeFormatter.ISO_ZONED_DATE_TIME);
Run Code Online (Sandbox Code Playgroud)

我得到一个例外:

抛出 java.time.format.DateTimeParseException:无法在索引 23 处解析文本“2019-10-12T00:00:00.000 07:00”

有人可以帮助我了解这里可能有什么问题吗?

java datetime parsing date java-8

1
推荐指数
1
解决办法
716
查看次数

用于按频率排序的比较器,无需创建比较器实现类

只是想知道我们是否可以在不编写自定义比较器类的情况下使用 Java 8 根据重复数字的频率对列表进行排序。

我需要根据给定的整数的频率,然后按自然数字顺序对给定的整数进行排序。

我在Comparator.naturalOrder()处遇到错误

这是我尝试过的代码:

Integer[] given = new Integer[]{0,0,1,22,11,22,22,11,44,555,55,66,77,88,99};
List<Integer> intList = Arrays.asList(given);


Map<Integer, Long> frequencyMap = intList.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
List<Integer> newList = intList.stream().sorted(Comparator.comparing(frequencyMap::get).thenComparing(Comparator.naturalOrder())).collect(Collectors.toList());
System.out.println(newList.toString());
Run Code Online (Sandbox Code Playgroud)

预期的输出是

[1, 44, 55, 66, 77, 88, 99, 555, 0, 0, 11, 11, 22, 22, 22]
Run Code Online (Sandbox Code Playgroud)

PS:在第一行使用数组以避免在多行中使用 list.add() 并清楚理解。

java sorting comparator java-8

1
推荐指数
1
解决办法
200
查看次数

Liquibase Diff 将 LocalTime 映射到 Binary

我目前正在试验 liquibase。我的更改日志文件是通过liquibase-maven-plugin基于我的休眠实体类生成的。到目前为止它有效,但它映射java.time.LocalDateBINARY(255). 是否可以教 liquibase 使用DATE,还是需要手动完成?

我在用

  • 弹簧启动 2.1.4
  • Liquibase 3.6.3
  • 休眠 5.3.9

java binary liquibase java-8 localdate

1
推荐指数
1
解决办法
261
查看次数

在提取的实体列表中查找重复项

private boolean hasDuplicates(Recipe recipe) {
    List<Recipe> currentRecipes = new ArrayList<>();

    Stream.of(this.breakfast, this.lunch, this.dinner).forEach(meal -> {
        currentRecipes.add(meal.getRecipe());
        currentRecipes.add(meal.getSnack());
    });
    currentRecipes.add(this.snack);

    return currentRecipes.contains(recipe);
    };

}
Run Code Online (Sandbox Code Playgroud)

// 想象一下所有字段的 getter 和 setter。

public class Menuplan {
  private Meal breakfast;
  private Meal lunch;
  private Meal dinner;
  private Recipe snack;
}

public class Meal {
  private Recipe recipe;
  private Reicpe snack;
}
Run Code Online (Sandbox Code Playgroud)

如果 Menuplan 已经分配了给定的食谱(作为零食或食谱),我使用上述方法进行测试。

我想知道是否有更优雅/更短的方法来编写函数。

java java-8

1
推荐指数
1
解决办法
110
查看次数

记录密码查询

有没有办法记录我们使用 spring jpa 内置查询(如 findById!)时生成的密码查询?我有一个复杂的内置查询,我需要查看其密码

java java-8 spring-data-neo4j

1
推荐指数
1
解决办法
891
查看次数

如何比较两个地图并检索两个地图中出现的值的键?

newPropertiesFile.keySet().parallelStream()
    .filter(value -> oldPropertiesFile.keySet().parallelStream()
            .filter(entry -> oldPropertiesFile.get(entry).toString().equals(newPropertiesFile.get(value).toString()))
            .filter(values -> !values.equals(value)).count() > 0)
    .collect(Collectors.toMap(entryKey -> (String) entryKey, entryKey -> newPropertiesFile.get(entryKey).toString()));
Run Code Online (Sandbox Code Playgroud)

例如,我有mapA = {(1,'a'),(2,'b'),(3,'c')}mapB = {(5,'a'),(6,'d'),(7,'c')} 比较了两个地图的 valueList,值'a''c'inmapA出现在mapB,它们的键是57分别。

因此我需要的 o/p:
5,7

我已经完成了上述操作并获得了所需的输出。但是复杂度在 O(n^2) 上太高了。有什么优化的方法吗?

一个更简单的例子:

mapA.keySet().parallelStream()
    .filter(v->mapB.keySet().parallelStream()
            .filter(e->mapB.get(v).equals(mapA.get(v)))
            .filter(v->!v.equals(v)).count()>0)
    .forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

1
推荐指数
1
解决办法
1609
查看次数

使用 Optional 避免 NPE

我试图避免检查(if ... != null)try{...}catch(NullPointerException e) {...}

让我们举个例子来理解我的问题:

我有一个class Park包含List<Car> 我有一个class Car包含一个Motor object 我有一个class Motor包含一个Name String

我想从我的 Park 类中返回第一个字符串电机名称:

我想避免:

if (park != null) {
    if (park.getCars() != null) {
        for (Car car : park.getCars() {
              if (car.getMotor() != null) {
                  return car.getMotor().getName();
Run Code Online (Sandbox Code Playgroud)

我在想这样的事情:

Optional.ofNullable(park).map(Park::getCars).ifPresent(cars -> {
    return cars.stream().map(Car::getMotor).map(Motor::getName).findFirst().orElse(null);
});
Run Code Online (Sandbox Code Playgroud)

但它不编译。有任何想法吗 ??

java optional java-8 java-stream

1
推荐指数
1
解决办法
584
查看次数

Java 中的 lambda 表达式 ClassCastException

我正在尝试在 Java 8 中学习流媒体。以下是我的代码:

主程序

public class Main {
    public static void main(String[] args) {

        Person person = new Person("FirstName", "LastName");
        List<Person> personList = new ArrayList<>();
        personList.add(person);

        Place place = new Place("name", "country");
        List<Place> placeList = new ArrayList<>();
        placeList.add(place);


        List<List<Object>> objects = new ArrayList<>();
        objects.add(Collections.singletonList(personList));
        objects.add(Collections.singletonList(placeList));

        List<Object> persons = objects.get(0);
        List<String> firstNames = persons.stream()
                .map(o -> ((Person)o).getFirstName())
                .collect(Collectors.toList());

        firstNames.forEach(System.out::println);
    }
}
Run Code Online (Sandbox Code Playgroud)

人.java

@Data
public class Person {
    String firstName;
    String lastName;

    public Person(String firstName, String lastName) {
        setFirstName(firstName);
        setLastName(lastName);
    } …
Run Code Online (Sandbox Code Playgroud)

java casting java-8 java-stream

1
推荐指数
1
解决办法
3950
查看次数