English 中文(简体)
2. 将2 HashMaps与Java 溪流二比,替换一个Hasmaap的属性价值
原标题:Compare two HashMaps with the Java streams API and replace attributes values in one HashMap

我有2个地图。

HashMap<DedupeTableEntity,List<DedupeTableEntity>> deltaHashmap 

HashMap<DedupeTableEntity,List<DedupeTableEntity>> existingHashmap 

残疾者具有id、国家、实体。

  • I want to iterate through the values of delta hash map (both hashmaps has only 1 key and list of values under that key)

  • Check whether that object is in existing hash map if 2 attributes match (country and id)

  • 如果有

  • Take attribute value of (entity flag) that object from existinghashmap and replace the attribute (entity flag) value of the same object in the delta hash map

    public void compareTwoHashmaps(HashMap<DedupeTableEntity,List<DedupeTableEntity>> deltaHashmap 
    , HashMap<DedupeTableEntity,List<DedupeTableEntity>> existingHashmap){
            //iterate through deltahashmap values
            return deltaHashmap.values().stream()
                    .flatMap(Collection::stream)
                    .filter(c -> (existingHashmap.values().stream().flatMap(Collection::stream).filter(e->e.getId().equals(c.getId()) && e.getObjectCountry().equals(c.getObjectCountry())))
                            .map(k-> k.setEntityFlag(existingHashmap.values().stream().flatMap(Collection::stream).filter(e->e.getId().equals(k.getId()) && e.getObjectCountry().equals(k.getObjectCountry())).map(r ->r.getEntityFlag()).toString()));
        }
    

这是我迄今为止所走过的。 但有一个错误,即:如何纠正。

希望得到任何帮助。

最佳回答
  • Performing a linear scan through existingHashmap for every element in deltaHashmap is extremely wasteful.
  • map is not for performing an action on each element in a Stream. map is for changing the element returned in a Stream. For performing an action, you should use forEach.
  • Calling toString() on a Stream is unlikely to produce useful information.

我首先将绘制以下图,即用于配对财产的<代码>现有HashMap的数值。 易于执行复合钥匙的方法是界定一个。 班级。 绘制这种地图时,可进行对比,可以更快地寻找对应物,而不必检查现有的Hashmap在德尔塔哈什图中的每一价值。 (校对班自动界定平等、传统和根据成员制定方法)。

不应在此加以利用,因为它具有价值——但是没有回报的价值。 你们正在某个实体中设定财产。 这样做不会归还任何东西。

public void compareTwoHashmaps(
    HashMap<DedupeTableEntity,List<DedupeTableEntity>> deltaHashmap,
    HashMap<DedupeTableEntity,List<DedupeTableEntity>> existingHashmap) {

    record Identifiers(String id, String country) {
        static Identifiers of(DedupeTableEntity entity) {
            return new Identifiers(entity.getId(),
                                   entity.getObjectCountry());
        }
    }

    Map<Identifiers, DedupeTableEntity> allExisting =
        existingHashmap.values().stream().flatMap(Collection::stream)
            .collect(Collectors.toMap(Identifiers::of, e -> e));

    deltaHashmap.values().stream().flatMap(Collection::stream)
        .forEach(delta -> {
            DedupeTableEntity existing =
                allExisting.get(Identifiers.of(delta));

            if (existing != null) {
                delta.setEntityFlag(existing.getEntityFlag());
            }
        });
}
问题回答

似乎在流体中存在一些不正确的母体。 还有一些可能的重复守则,我试图减少。 我也做了一些开采,最后是:

public void updateFlag(
        Map<DedupeTableEntity, List<DedupeTableEntity>> deltaHashmap,
        Map<DedupeTableEntity, List<DedupeTableEntity>> existingHashmap
) {
    deltaHashmap.values().stream()
            .flatMap(Collection::stream)
            .forEach(k -> extractFlag(existingHashmap, k)
                    .ifPresent(k::setEntityFlag));
}

private Optional<String> extractFlag(Map<DedupeTableEntity, List<DedupeTableEntity>> existingHashmap, DedupeTableEntity k) {
    return existingHashmap.values().stream()
            .flatMap(Collection::stream)
            .filter(e -> equalsByIdAndCountry(k, e))
            .findFirst().map(DedupeTableEntity::getEntityFlag);
}

private boolean equalsByIdAndCountry(DedupeTableEntity c, DedupeTableEntity e) {
    return e.getId().equals(c.getId()) && e.getObjectCountry().equals(c.getObjectCountry());
}

如果数据确实正确,我就没有任何数据可以检测,因此我留给你。

你的法典有一些错误:

  • The method has a return type of void, but it attempts to return a stream.
  • The call to map on the filter operation is not terminal, meaning that it won t actually execute the mapping operation.
  • To fix these issues, the method could be modified to return the modified deltaHashmap

a 固定版本,包括返回表:

public HashMap<DedupeTableEntity,List<DedupeTableEntity>> compareTwoHashmaps(
    HashMap<DedupeTableEntity,List<DedupeTableEntity>> deltaHashmap,
    HashMap<DedupeTableEntity,List<DedupeTableEntity>> existingHashmap) {
deltaHashmap.values().stream()
        .flatMap(Collection::stream)
        .filter(c -> existingHashmap.values().stream()
                .flatMap(Collection::stream)
                .anyMatch(e -> e.getId().equals(c.getId()) && e.getObjectCountry().equals(c.getObjectCountry())))
        .forEach(k -> {
            Optional<String> entityFlag = existingHashmap.values().stream()
                    .flatMap(Collection::stream)
                    .filter(e -> e.getId().equals(k.getId()) && e.getObjectCountry().equals(k.getObjectCountry()))
                    .map(r -> r.getEntityFlag())
                    .findFirst();
            entityFlag.ifPresent(k::setEntityFlag);
        });
return deltaHashmap;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签