基于两个条件对数组进行去重。

huangapple go评论67阅读模式
英文:

Dedupe array on basis of two things

问题

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class Dedupe {
    public static class Pro {
        String name;
        Long firstPresent;

        public Pro(String name, Long firstPresent) {
            this.name = name;
            this.firstPresent = firstPresent;
        }
    }

    public static void main(String[] args) throws Exception {
        List<Pro> pros = new ArrayList<>();
        pros.add(new Pro("John", (long) 3));
        pros.add(new Pro("Umar", (long) 1));
        pros.add(new Pro("John", (long) 2));
        pros.add(new Pro("John", (long) 0));

        Map<String, Long> prosMap = new HashMap<>();
        for (Pro pro : pros) {
            String name = pro.name;
            Long firstPresent = pro.firstPresent;
            Long data = prosMap.getOrDefault(name, Long.MAX_VALUE);
            if (data > firstPresent) {
                prosMap.put(name, firstPresent);
            }
        }

        List<Pro> result = new ArrayList<>();
        for (Map.Entry<String, Long> entry : prosMap.entrySet()) {
            result.add(new Pro(entry.getKey(), entry.getValue()));
        }

        // 'result' now contains the desired deduplicated array
    }
}
英文:
import java.util.ArrayList;
import java.util.List;
public class Dedupe {
public static class Pro {
String name;
Long firstPresent;
public RouteHijack(String name, Long firstPresent, String matchedPrefix) {
this.name = name;
this.firstPresent = firstPresent;
}
}
public static void main(String[] args) throws Exception {
List&lt;Pro&gt; pros = new ArrayList&lt;&gt;();
pros.add(new Pro(&quot;John&quot;, (long) 3));
pros.add(new Pro(&quot;Umar&quot;, (long) 1));
pros.add(new Pro(&quot;John&quot;, (long) 2));
pros.add(new Pro(&quot;John&quot;, (long) 0));
}
}

Hi,
I want to dedupe the above array on basis of [name + Smallest value of firstPresent]. The desired resultant arry should contain
[Pro&lt;&quot;John&quot;, 0&gt;, &lt;&quot;Umar&quot;, 1&gt;]

I am looking forward some simpler way using language feature like stream or something similar. Do you know any simple way to achieve this in java8? Thanks for the help.

p.s: One solution is as below

Map&lt;String, String&gt; prosMap = new HashMap&lt;&gt;();
for (Pro pro : pros) {
String name = pro.getName();
Long firstPresent = pro.getFirstPresent();
String data =  prosMap.getOrDefault(name, 0);
if (data &lt; firstPresent) {
prosMap.put(name, firstPresent);
}
}
// Change the Map to array.

答案1

得分: 1

在Java 8的流API中有一个很棒的toMap收集器:

pros.stream().collect(Collectors.toMap(pro -> pro.name, pro -> pro,
                (a, b) -> a.firstPresent > b.firstPresent ? b : a
))

第三个参数是一个BinaryOperator合并函数,它接受两个Pro对象,然后仅返回其中一个。

在收集到映射之后,你可以简单地调用values()来获取Collection<Pro>

英文:

There is a wonderful toMap collector in Java 8 stream API:

pros.stream().collect(Collectors.toMap(pro -&gt; pro.name, pro -&gt; pro,
				(a, b) -&gt; a.firstPresent &gt; b.firstPresent ? b : a
))

The third argument is a BinaryOperator merge function that takes two Pro objects and returns only one of them.

After collecting to a map you can simply call values() to get Collection&lt;Pro&gt;

huangapple
  • 本文由 发表于 2020年9月23日 07:23:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64018943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定