在Java8中使用过滤器和映射提取数据库列数据 [英] Extracting db column data with filter and map in Java8

查看:88
本文介绍了在Java8中使用过滤器和映射提取数据库列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于员工的表具有以下数据:员工ID,员工名称,其经理的ID和其团队成员的ID.根据团队规模,每位员工将有多个条目.假设一个团队中有5个其他成员的员工将有5行,其中员工和经理的ID重复一次,每行都有一个团队成员的ID.

A table about employees has following data: employee Id, employee name, their manager's Id and their team members' Id. Each employee would have multiple entries based on their team size. Say an employee with 5 other members in their team would have five rows with the employee and manager id repeated each with one team member's id.

样品表:

employeeId | employeeName | managerId | teamEmployeeId |
-------------------------------------------------------
1000       | Alex         | 4000      | 1101           |
1200       | Bran         | 4100      | 1301           |
1200       | Bran         | 4100      | 1302           |
1000       | Alex         | 4000      | 1102           |
1200       | Bran         | 4100      | 1303           |
1000       | Alex         | 4000      | 1103           |
1200       | Bran         | 4100      | 1304           |
1200       | Bran         | 4100      | 1305           |
1000       | Alex         | 4000      | 1104           |

目标是将每个员工的团队ID(也包括经理ID)分成一个单独的数组,以供日后使用.

The goal is to split each employees' team IDs (with the manager id too) into a separate array to be used later.

预期结果:

allIds:
1000
1200

teamIds for "Alex" :
1101
1102
1103
1104
4000

teamIds for "Bran" :
1301
1302
1303
1304
1305
4100

获取唯一员工ID的第一部分正在工作(基于此

The first part of getting unique employee ids is working (based on this answer). But trying to split the team members by using the employee id returns the first value, but the correct number of times. Say: a team with 5 returns an array of five with the first member's id in all. Manager's id isn't being added.

我正在使用的代码:

List<ViewData> list = getDataList();

String[] allIds = list.parallelStream()
    .map(ViewData::getId).distinct().toArray(String[]::new);

System.out.println(allIds.length + "\n");

for (String id : allIds) {

    String[] teamIds = list.parallelStream()
        .filter(row -> row.getId().equals(id))
        .map(ViewData::getTeamId).distinct()
        .toArray(String[]::new);

    teamIds = Arrays.copyOf(teamIds, teamIds.length+1 );
    teamIds[teamIds.length] = list.parallelStream()
        .filter(obj -> obj.getId().equals(id))
        .map(ViewData::getManagerId).toString();
    System.out.println(teamIds.length + "\n");
}

我了解这是一个逻辑错误.我为filter()引用的所有文档均显示语法正确.我的理解是filter()返回其ID与我要遍历的那一行匹配的整行,而map()从该行中取出团队成员的ID,最后所有内容都以字符串数组形式返回.

I understand this is a logical error. All the docs I have referred for filter() shows the syntax is correct. My understanding is the filter() returns the entire row whose id matches with the one I am looping through and the map() takes out the team member's id from that row and finally everything is returned as a string array.

我哪里出问题了?编写代码或了解这些功能如何工作?

Where have I gone wrong? Writing the code or understanding how these functions work?

如果这样的表导致重复/重复行(尤其是应该存在的确切时间):

if a table like this causes duplicates/repeat rows(particularly the exact number of time it is supposed to be present):

没有主键(即没有保证的唯一列)的视图需要一个组合主键,该组合主键是通过合并两个唯一的列而得出的.

a view without a primary key (i.e., no guaranteed unique column ) needs a composite primary key derived by combining two columns that would then be unique.

  • 为组合键组合创建单独的@Embeddable

将其添加到主模型类中: @EmbeddedId private UniqueId uniqueId;

Add it to main model class: @EmbeddedId private UniqueId uniqueId;

按照答案中的现有逻辑进行操作:

Proceed with the exisiting logic as in the answer:

for (String id : allIds) {
    String[] teamIds = list.stream()
        .filter(row -> row.getUniqueId().getId().equals(id))
        .map(obj -> obj.getUniqueId().getTeamEmployeeId())
        .toArray(String[]::new);

    teamIds = Arrays.copyOf(teamIds, teamIds.length + 1);
    teamIds[teamIds.length - 1] = list.stream()
        .filter(obj -> obj.getUniqueId().getId().equals(id))
        .map(ViewData::getManagerId).findFirst().orElse("");            

    String empName = list.stream()
        .filter(obj -> obj.getUniqueId().getId().equals(id))
        .map(ViewData::getName).findFirst().orElse("");
}

推荐答案

基于问题和评论,您需要的是:

Based on the question and comments, what you need is:

for (String id : allIds) {
    String[] teamIds = list.parallelStream()
                           .filter(row -> row.getId().equals(id))
                           .map(ViewData::getTeamId).distinct()
                           .toArray(String[]::new);

    teamIds = Arrays.copyOf(teamIds, teamIds.length + 1);
    teamIds[teamIds.length - 1] = list.parallelStream()
                                      .filter(obj -> obj.getId().equals(id))
                                      .map(ViewData::getManagerId)
                                      .findFirst()
                                      .orElse(null);

    System.out.println(teamIds.length + "\n");
}

这篇关于在Java8中使用过滤器和映射提取数据库列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆