2

The task was:

Modify bonuses for all workers whose name starts with a letter from the second half of the alphabet.

class Worker that extends Employee
{
    public Worker(String name, int yOfEmp, int salary, Manager boss){
        super(name, yOfEmp, salary,boss);
        boss.addEmployee(this);
    }

    public float getBonus(Employee emp){
        Random rand = new Random();
        float proc = rand.nextFloat();
        return emp.getSalary() * proc;
    }
    public float setBonus(Employee e, float proc){
        float newProc = proc;
        return e.getSalary() * proc;
    }

    @Override
    public String toString() {
        return super.toString();
    }
}

class RegWorker extends Worker
{
}

class Main
{
  public static void main(String[] str) {
    Manager bill = new Manager("Bill", 2010, 2549, null);
    Manager steven = new Manager("Steven", 2011, 2100, bill);
    Trainer jerry = new Trainer("Jerry", 2014, 800, steven);
    RegWorker john = new RegWorker("John", 2010, 1000, bill);
    RegWorker kate = new RegWorker("Kate", 2011, 1000, bill);
    RegWorker sam = new RegWorker("Sam", 2013, 1200, steven);

    final List<Worker> workers = Arrays.asList(jerry, john, kate, sam);

    System.out.println("Modify bonuses for all regular workers:");
    RegWorker reg = null;

    List<RegWorker> regWorkers = workers
                .stream()
                .filter(w -> w.getClass() == reg.getClass())
                .filter(w -> w.getName().charAt(0) < 'O')
                .map(w -> w.setBonus(w, w.getBonus(w)+3))
                .collect(toList());
    }

I've got this error

reason: inference variable T has incompatible bounds
equality constraints: RegWorker
lower bounds: Float

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • 1
    There's too much code and it is also quite weird: `reg.getClass()` is guaranteed to fail with `NullPointerException`. – Tunaki Oct 16 '15 at 20:02

1 Answers1

3

When you're doing map, you replace stream objects with mapper function return value which is return value of setBonus, which is float. So after the map step you have not Stream<RegWorker>, but Stream<Float> which should be collected into List<Float>.

It seems that you want to use peek instead of map. Also you will need to cast stream elements to RegWorker class as your original stream is Stream<Worker>. Finally note that your code has another problem which will appear in runtime: reg.getClass() will throw NullPointerException as reg is null. Probably you just need .filter(w -> w instanceof RegWorker):

List<RegWorker> regWorkers = workers
            .stream()
            .filter(w -> w instanceof RegWorker)
            .filter(w -> w.getName().charAt(0) < 'O')
            .map(w -> (RegWorker)w)
            .peek(w -> w.setBonus(w, w.getBonus(w)+3))
            .collect(toList());
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • And why not combine the two filters on the same line. – Jean-François Savard Oct 16 '15 at 20:05
  • 2
    @Jean-FrançoisSavard, that's the matter of taste. – Tagir Valeev Oct 16 '15 at 20:07
  • @TagirValeev Doesn't it add time complexity ? – Jean-François Savard Oct 16 '15 at 20:12
  • 4
    @Jean-FrançoisSavard, you'll never know until you measure. See [this question](http://stackoverflow.com/q/24054773/4856258). – Tagir Valeev Oct 16 '15 at 20:14
  • Thanks for the link, not sure why but I tought I had seem somewhere that it added time complexity and never verified myself. – Jean-François Savard Oct 17 '15 at 03:52
  • 2
    It’s more likely that moving `.filter(w -> w.getName().charAt(0) < 'O')` to the beginning of the chain improves performance as a character comparison is cheaper than `instanceof` and bringing the steps `.filter(w -> w instanceof RegWorker)` and `.map(w -> (RegWorker)w)` closer together raises the chance of them getting fused together by HotSpot. However, that’s all speculative and only about tendencies. It’s also very likely that the entire operation gets optimized as a single code block and then all differences are negligible. – Holger Oct 19 '15 at 10:20