简体   繁体   中英

How to Get common Items from a stream of objects in Java8

I have 2 streams of Coll objects and i want to find the common objects on the basis of one the instance variable say i here. I need to do this using Java 8 streams. Further I need to update the j variable by say a multiplier of 1000 for the common elements.

class Coll
{
Integer i;
Integer j;

public Coll(Integer i, Integer j) {
    this.i = i;
    this.j = j;
}

public Integer getI() {
    return i;
}

public void setI(Integer i) {
    this.i = i;
}

public Integer getJ() {
    return j;
}

public void setJ(Integer j) {
    this.j = j;
}

}

I am wring something like :

 public static void main(String args[])
{
    Stream<Coll> stream1 = Stream.of(new Coll(1,10),new Coll(2,20),new Coll(3,30) );
    Stream<Coll> stream2 = Stream.of(new Coll(2,20),new Coll(3,30),new Coll(4,40) );

    Stream<Coll> common = stream1
            .filter(stream2
                    .map(x->x.getI())
                    .collect(Collectors.toList())
                    ::equals(stream2
                                .map(x->x.getI()))
                                .collect(Collectors.toList()));
    common.forEach( x-> x.setJ(x.getJ()*1000));
    common.forEach(x -> System.out.println(x));

}

Am doing something wrong around equals method!! I guess Java8 doesn't support methods with parameters like equals does!!

I am getting a compilation error: expected a ')' or ';' around equals method

Move the logic to collect all the i of Stream2 outside. Then filter all Coll in stream1 if it's i is present in the other list.

List<Integer> secondCollStreamI = stream2
            .map(Coll::getI)
            .collect(Collectors.toList());
Stream<Coll> common = stream1
            .filter(coll -> secondCollStreamI.contains(coll.getI()));


common.forEach( x-> x.setJ(x.getJ()*1000));
common.forEach(x -> System.out.println(x));

The last statement will result in an IllegalStateException ( stream has already been operated upon or closed ) since you cannot reuse the stream. You need to somewhere collect it to a List<Coll> ... Something like...

List<Coll> common = stream1
            .filter(coll -> secondCollStreamI.contains(coll.getI()))
            .collect(Collectors.toList());

common.forEach(x -> x.setJ(x.getJ() * 1000));
common.forEach(System.out::println);

Or, if you want to do everything on the fly without collecting it

stream1
        .filter(coll -> secondCollStreamI.contains(coll.getI()))
        .forEach(x->  {
            x.setJ(x.getJ()*1000);
            System.out.println(x);
        });

You can do it like so,

Map<Integer, Coll> colsByI = listTwo.stream()
    .collect(Collectors.toMap(Coll::getI, Function.identity()));
List<Coll> commonElements = listOne.stream()
    .filter(c -> Objects.nonNull(colsByI.get(c.getI())) && c.getI().equals(colsByI.get(c.getI()).getI()))
    .map(c -> new Coll(c.getI(), c.getJ() * 1000))
    .collect(Collectors.toList());

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