Item 52: Use overloading judiciously

52. Overload should be used with caution

package tryAny.effectiveJava;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class OverloadTest {
    public static String classify(Set<?> s) {
        return "Set";
    }

    public static String classify(List<?> s) {
        return "List";
    }

    public static String classify(Collection<?> s) {
        return "Collection";
    }

    public static void main(String[] args) {
        Collection<?>[] collections = { new HashSet<String>(), new ArrayList<String>(),
                new HashMap<String, String>().values() };

        for (Collection<?> c : collections) {
            System.out.println(classify(c));
        }
        /**
         * Collection <br>
         * Collection <br>
         * Collection
         */
    }
}
package tryAny.effectiveJava;

import java.util.List;

public class OverrideTest {
    public static void main(String[] args) {
        List<Wine> l = List.of(new Wine(), new SparklingWine(), new Champagne());

        for (Wine w : l) {
            System.out.println(w.name());
            /**
             * wine<br>
             * sparkling wine<br>
             * champagne
             */
        }
    }
}

class Wine {
    String name() {
        return "wine";
    }
}

class SparklingWine extends Wine {
    @Override
    String name() {
        return "sparkling wine";
    }
}

class Champagne extends SparklingWine {
    @Override
    String name() {
        return "champagne";
    }
}
package tryAny.effectiveJava;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class SetList {
    public static void main(String[] args) {
        Set<Integer> s = new TreeSet<>();
        List<Integer> l = new ArrayList<>();

        for (int i = -3; i < 3; i++) {
            s.add(i);
            l.add(i);
        }

        for (int i = 0; i < 3; i++) {
            s.remove(i);
            l.remove(i);
        }

        System.out.println(s + "" + l);// [-3, -2, -1] [-2, 0, 2]
    }
}
package tryAny.effectiveJava;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Snippet {
    public static void main(String[] args) {
        new Thread(System.out::println).start();
        ExecutorService exec = Executors.newCachedThreadPool();
        exec.submit(System.out::println); //Compile error

    }
}
// Ensuring that 2 methods have identical behavior by forwarding
public boolean contentEquals(StringBuffer sb) {
    return contentEquals((CharSequence) sb);
}

Recommended Posts

Item 52: Use overloading judiciously
Item 53: Use varargs judiciously
Item 45: Use streams judiciously
Item 66: Use native methods judiciously
Item 67: Optimize judiciously
Item 26: Don't use raw types
Item 32: Combine generics and varargs judiciously
Item 59: Know and use the libraries
Item 40: Consistently use the Override annotation
Item 41: Use marker interfaces to define types
Item 36: Use EnumSet instead of bit fields
Item 71: Avoid unnecessary use of checked exceptions
Item 37: Use EnumMap instead of ordinal indexing
Item 72: Favor the use of standard exceptions
Item 48: Use caution when making streams parallel