Skip to content

Instantly share code, notes, and snippets.

@RaviH
Created November 3, 2015 22:31
Show Gist options
  • Save RaviH/a70b15d2441bfbe3c8eb to your computer and use it in GitHub Desktop.
Save RaviH/a70b15d2441bfbe3c8eb to your computer and use it in GitHub Desktop.
CollectionUtils example for intersect using select and selectRejected.
package com.charter.aesd.account.core.query;
import com.charter.aesd.device.domain.Device;
import org.apache.commons.collections.CollectionUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by rhasija on 11/3/15.
*/
public class DeviceTest {
public static void main(String[] args) {
final List<String> macAddresses1 = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8");
final List<String> macAddresses2 = Arrays.asList("1", "2", "3", "44", "5", "6", "7", "8");
final List<Device> devices1 = createDevices(macAddresses1);
final List<Device> devices2 = createDevices(macAddresses2);
final List<Device> finalResult = new ArrayList<>();
// Get all devices from devices2 that are same as devices1
CollectionUtils.select(devices2, o -> {
Device device = (Device) o;
return devices1.stream().filter(device1 -> device1.getMacAddress().equals(device.getMacAddress())).findAny().isPresent();
}, finalResult);
CollectionUtils.selectRejected(devices2, o -> {
Device device = (Device) o;
return devices1.stream().filter(device1 -> device1.getMacAddress().equals(device.getMacAddress())).findAny().isPresent();
}, finalResult);
finalResult.forEach(device -> System.out.println(device.getMacAddress()));
}
static List<Device> createDevices(List<String> macAddresses) {
return macAddresses.stream().map(macAddress -> {
final Device device = new Device();
device.setMacAddress(macAddress);
return device;
}).collect(Collectors.<Device>toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment