Several small places

1. Overload is better to have inheritance hierarchy, do not write too "similar", minimize the use of heavy-duty, multiple-write, but to consider the reusability

public class Card {
    public static String classify(Set<?> set) {
        return "set";
    }
    public static String classify(List<?> list) {
        return "list";
    }
    public static String classify(Collection<?> collection) {
        return "unknown";
    }

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

        for (Collection<?> c : collections) {
            System.out.println(classify(c));
        }
    }
}

Here will compile, but are unknown, because the program does not know what the right, only to find a certain suitable, because sub-classes are the Collection.

Here we can look at a variant on ok

    public static String classify(Collection<?> collection) {
//        return "unknown";
        return collection instanceof Set ? "set" :
                collection instanceof List ? "list" : "unknown";
    }

 

2. Never return NULL, or to return a collection of empty array, this is really a very common mistake habits

- firstly, to increase the risk of untreated null

- Second, let's look strange logic, why null, null why the deal came

For the collection of performance we can in order to return an immutable collection. The following code is to do so, use the Collections.emptyList method. If you want to return a Set, you can use Collections.emptySet; To return to Map, use Collections.emptyMap. But remember, this is an optimization, it is rarely needed. If you think you need it, measure the performance before and after, make sure that it really helps

    public List<Card> getCards() {
        return nums == 0 ? Collections.emptyList()
                : new ArrayList<>(nums);
    }

Where in the case of an array of the same set. Never returns null, but returns an array of zero length. Generally, you should only return an array of the correct length, this length may be zero. If you think that assign zero-length arrays can hurt performance, it can return to repeat the same zero-length arrays, because all the zero-length arrays are immutable:

    private static final Card[] EMPTY_CARD_ARRAY = new Card[0];
    public Card[] getCards() {
        return Card.EMPTY_CARD_ARRAY;
    }

 

3.Optional point usage

public static <E extends Comparable<E>> Optional<E> max(Collection<E> c) {
        if (c.isEmpty()) {
            return Optional.empty();
        }
        E result = null;
        for (E e : c) {
            if (result == null || e.compareTo(result) > 0) {
                result = Objects.requireNonNull(e);
            }
        }
        return Optional.of(result);
    }
public static void main(String[] args) {
        List<String> words = new ArrayList<>();
        words.add("a");
        words.add("b");
        words.add("c");
        String maxWord = max1(words).orElse("no word");
        System.out.println(maxWord);
    }

Optional null at compile time to fail to compile, and one can use the following alternative above.

public static <E extends Comparable<E>> Optional<E> max1(Collection<E> c) {
        return c.stream().max(Comparator.naturalOrder());
    }

 

4. Iterable interfaces may be implemented, so that all classes can be iterative operations

5. For any calculations require precise answers, do not use float or double. If you want the system to handle decimal point, and does not mind the inconvenience and costs associated with the use of basic types, use BigDecimal. Another benefit of using BigDecimal is that it can be completely controlled rounding, when an operation of rounding is required, can be selected from eight rounding mode. If you run a legitimate business computing use rounding behavior, it will be very convenient. If performance is important, then you do not mind handling the decimal point, but the value is not too large, you can use int or long. If the value is not more than 9 decimals, int may be used; if not more than 18, can be used long. If the number may be more than 18, use BigDecimal. 

 

 

He joined the group of micro-channel K God today, so funny, ha ha ha, long time not so happy.

Guess you like

Origin www.cnblogs.com/CherryTab/p/11900717.html