Abstract built-in classes

Report a typo

In which cases can you replace an anonymous class with a lambda expression? All interfaces are from the standard java class library.

a)

Thread thread = new Thread(new Runnable() {
    @Override public void run() { 
        System.out.println("Hello!"); 
    } 
});

b)

Comparator<String> comparator = new Comparator<String>() { 
    @Override public int compare(String s1, String s2) {
        return s1.compareTo(s2); 
    }
};

c)

Iterator<Integer> iterator = new Iterator<Integer>() {
    @Override 
    public boolean hasNext() { 
        return false; 
    }

    @Override 
    public Integer next() { 
        return 0; 
    } 
};

d)

// see Swing 
JButton button = new JButton("Click me"); 
button.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
        System.out.println("Button clicked"); 
    } 
});

To solve this problem, it is recommended to try it in code or think well.

Select one or more options from the list
___

Create a free account to access the full topic