Set<Instruction> () { Set<Instruction> joinInstructions = new HashSet<>(); StreamEx.of(myInstructions) .remove(Instruction::isLinear) .flatMap(inst -> IntStreamEx.of(inst.getSuccessorIndexes()).elements(myInstructions)) .into(joinInstructions); for (int index = 0; index < myInstructions.length; index++) { Instruction instruction = myInstructions[index]; if (instruction instanceof FinishElementInstruction && !((FinishElementInstruction)instruction).getVarsToFlush().isEmpty()) { // Good chances to squash something after some vars are flushed joinInstructions.add(myInstructions[index + 1]); } } return joinInstructions; }