Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何改进此映射算法_Java_Algorithm_Mapping - Fatal编程技术网

Java 如何改进此映射算法

Java 如何改进此映射算法,java,algorithm,mapping,Java,Algorithm,Mapping,我有一个n项目列表,每个项目都有一个产品代码和m人(可能在几个项目中) 大概是这样的: items: [ { code: 'Tuna', people: [ 'Adam', 'Eric' ] }, { code: 'Corn', people: [ 'Eric' ] }, ... ] people: { Adam: ['Tuna'], Eric: ['Tuna', 'Corn'] } 我想把它映射到一个不同的人和他们拥有的产品的列表,或者类似的东西: items: [

我有一个
n
项目列表,每个项目都有一个产品代码和
m
人(可能在几个项目中)

大概是这样的:

items: [
  { code: 'Tuna', people: [ 'Adam', 'Eric' ] },
  { code: 'Corn', people: [ 'Eric' ] },
  ...
]
people: {
  Adam: ['Tuna'],
  Eric: ['Tuna', 'Corn']
}
我想把它映射到一个不同的人和他们拥有的产品的列表,或者类似的东西:

items: [
  { code: 'Tuna', people: [ 'Adam', 'Eric' ] },
  { code: 'Corn', people: [ 'Eric' ] },
  ...
]
people: {
  Adam: ['Tuna'],
  Eric: ['Tuna', 'Corn']
}
在过去的两个小时里,我一直在绞尽脑汁,试图想出一个优雅的解决方案,这一定是一个非常直截了当的映射算法

我在Java8中完成了这项工作,因此流API已经准备就绪。秩序不重要

以下是我目前最大的努力:

Map<Person, List<String>> partyProductMap = getItems().stream()
        .flatMap(item -> item.getPeople().stream())
        .distinct()
        .collect(Collectors.toMap(
            Function.identity(),
            person -> getItems().stream()
                    .filter(item -> item.getPeople().contains(person))
                    .map(item -> item.getProductCode())
                    .distinct()
                    .collect(Collectors.toList())
        ));
Map partyProductMap=getItems().stream()
.flatMap(项目->项目.getPeople().stream())
.distinct()
.collect(collector.toMap)(
Function.identity(),
person->getItems().stream()
.filter(item->item.getPeople().contains(person))
.map(项目->项目.getProductCode())
.distinct()
.collect(收集器.toList())
));

我很想听到一些关于如何改进的想法

我认为最优雅的解决方案是简单的:

final Map<Person, Set<String>> partyProductMap = new HashMap<>();
for (final Item item : getItems())
    for (final Person person : item.getPeople()) {
        Set<String> codes = partyProductMap.get(person);
        if (codes == null) partyProductMap.put(person, codes = new HashSet<>());
        codes.add(item.code);
    }
final Map partyProductMap=new HashMap();
对于(最终项目:getItems())
for(最终人员:item.getPeople()){
设置代码=partyProductMap.get(个人);
if(code==null)partyProductMap.put(person,code=newhashset());
代码.添加(项目.代码);
}
注意:这里是Set,而不是List

当然,像Item/Person这样的类有适当的hashCode实现,等于,可能比较如下:

Map<Person, List<String>> partyProductMap = getItems().stream().
                flatMap(item -> item.getPeople().stream().map(
                        people -> Arrays.asList(
                                people, item.getProductCode()))).
                        collect(Collectors.groupingBy(x -> x.get(0),
                                Collectors.mapping(
                                        x-> x.get(1), Collectors.toList())));
Map partyProductMap=getItems().stream()。
flatMap(item->item.getPeople().stream().map)(
人员->数组.asList(
人员,item.getProductCode())。
收集(收集器.groupingBy(x->x.get(0)),
图(
x->x.get(1),Collectors.toList();