简体   繁体   中英

Java Guava filter two collection with different type

I have two collection:

ArrayList<B> currentB = new ArrayList<B>();
{
    currentB.add(new B(new A("1")));
    currentB.add(new B(new A("2")));
    currentB.add(new B(new A("7")));
    currentB.add(new B(new A("3")));
    currentB.add(new B(new A("4")));
}
ArrayList<A> newA = new ArrayList<A>();
{
    newA.add(new A("1"));
    newA.add(new A("5"));
    newA.add(new A("2"));
    newA.add(new A("6"));
    newA.add(new A("7"));
    newA.add(new A("8"));

}

Collections has following types:

class A {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        A nodeRef = (A) o;

        return !(id != null ? !id.equals(nodeRef.id) : nodeRef.id != null);

    }

    public A() {
    }

    public A(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "NodeRef{" +
                "id='" + id + '\'' +
                '}';
    }
}

class B {
    private A a;

    public A a() {
        return a;
    }

    public A getA() {
        return a;
    }

    public void setA(A a) {
        this.a = a;
    }

    @Override
    public String toString() {
        return "B{" +
                "a=" + a +
                '}';
    }

    public B(A a) {
        this.a = a;
    }

    public B() {
    }
}

I want to create (the best result is modify newA and currentB) two list:

  • The first one should have objects, which not represented in another
    one (in example case - a(3), a(4));
  • The second one the same logic but vice versa: (a(5), a(6), a(8)).

I can do it with guava, but it need to create 3 collection:

Function<B, A> BtoA = new Function<B, A>() {
    public A apply(final B b) {
        return b.getA();
    }
};
Collection<A> currentA = Collections2.transform(currentB, BtoA);

java.util.Collection<A> idToDelete = Collections2.filter(currentA, Predicates.not(Predicates.in(newA)));
java.util.Collection<A> idToAdd = Collections2.filter(newA, Predicates.not(Predicates.in(currentA)));

System.out.println("Old B" + idToDelete);
System.out.println("New A" + idToAdd);

Is there a way to get rid of Collection.transform or even the best way?

It will look quite nice with java 8 streams API:

java.util.Collection<String> idToDelete =
        currentB.stream() //get the stream
        .filter(b -> !newA.contains(b.getA())) // filter those b, whose A is in newA
        .map(b -> b.getA().id) // map transform to get just an Id (you can use just getA() here)
        .collect(Collectors.toList()); // finally transform back to list

java.util.Collection<String> idToAdd =
        newA.stream() // again get stream
        .filter(
                // this is a little bit fancy...
                // only leave those A, for which currentB doesn't contain element, that has getA() equals to that A
                a -> currentB.stream().noneMatch(
                        b -> b.getA().equals(a)
                )
        )
        .map(a -> a.id) // again get id
        .collect(Collectors.toList()); // transform to list

[EDIT:]

If you look into guava's source code, you can see, that transform just wraps your existing one with a transform function. So guava basically works just as java 8 streams. So you can actually use transform as you did. If absolutely don't wanna do it, this is complete example with guava:

Function<B, A> BtoA = new Function<B, A>() {
    public A apply(final B b) {
        return b.getA();
    }
};
Function<A, String> aToId = new Function<A, String>() {
    public String apply(final A a) {
        return a.getId();
    }
};

java.util.Collection<B> bToDelete = Collections2.filter(currentB, Predicates.compose(Predicates.not(Predicates.in(newA)), BtoA));

//without transform, looks ugly
java.util.Collection<A> aToAdd = Collections2.filter(newA, new Predicate<A>() {
    @Override
    public boolean apply(final A a) {
        return !Iterables.any(currentB, new Predicate<B>() {
            @Override
            public boolean apply(B b) {
                return b.getA().equals(a);
            }
        });
    }
});
// this is essentially the same, you can safely use transform
//java.util.Collection<A> aToAdd = Collections2.filter(newA, Predicates.not(Predicates.in(Collections2.transform(currentB, BtoA))));

java.util.Collection<String> idToDelete = Collections2.transform(bToDelete, Functions.compose(aToId, BtoA));
java.util.Collection<String> idToAdd = Collections2.transform(aToAdd, aToId);

System.out.println("Old B: " + idToDelete);
System.out.println("New A: " + idToAdd);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM