Java合并两个流量没有重复

时间:2018-12-20 15:13:27

标签: java flux spring-webflux reactor

我有两个通量,我想将它们合并以避免重复。我该怎么办?

这是我的通量:

Flux<Tweet> remoteTweets = Flux.just(
        new Tweet("tag1",new TweetID("text","name"),"userimage","country","place"),
        new Tweet("tag2",new TweetID("text","name"),"userimage","country","place")
);

Flux<Tweet> localTweets = Flux.just(
        new Tweet("tag1",new TweetID("text","name"),"userimage","country","place")
);

将这两个合并给我tag 1, tag1, tag2,但我想要tag1, tag2

顺序并不重要。

1 个答案:

答案 0 :(得分:3)

您可以这样做

     SpringApplication.run(BackendApplication.class, args);
  String[] strings = {"a","b","c"};
    Flux.merge(Flux.fromArray(strings),Flux.fromArray(strings))
            .distinct()
            .map(s -> {
                System.out.println(s);
                return s;
            }).subscribe();

但是在您的Tweet对象中,您需要重写equals和hashcode 首先要知道哈希码是否相等,然后是.equal函数

如果使用的是intellij,请尝试alt +插入并单击equal和hashCode