Believe everything the book as no book value of the acquired enumeration code optimization

1 Background

According to the actual development of a property to obtain enumerated enumeration values ​​are very common.

Defined as an enumeration:

@Getter
public enum CoinEnum {
    PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
 
    CoinEnum(int value) {
        this.value = value;
    }
 
    private final int value;
}

The actual development often need to get the enumeration object based on the value of value.

So how to do it?

 

 

2 encoding

2.1 wording is very low

The work will have to see by way of switch-case or if-else achieve access to enumerate according to a property.

Such an approach similar to the following:

public static CoinEnum getEnum(int value) {
    switch (value) {
        case 1:
            return PENNY;
        case 5:
            return NICKEL;
        case 10:
            return DIME;
        case 25:
            return QUARTER;
        default:
            return null;
    }
}

The biggest problem is that if such an approach enumeration constant to add, delete, modify, so the need for modifications to the corresponding function, the coupling is very high.

Does not comply with the principle of opening and closing (on-off principle: open to expansion, but closed for modification).

Also, if the enumeration constants are more easily mapping errors, late difficult to maintain.

 

2.2 Improved

We can use a static function values ​​to obtain enumerated array enumeration class match, write a version of the code improvements:

public static CoinEnum getEnum(int value) {
    for (CoinEnum coinEnum : CoinEnum.values()) {
        if (coinEnum.value == value) {
            return coinEnum;
        }
    }
    return null;
}

 

Followed by the enumeration constants need to modify this improvement, the function does not need to change, obviously a lot better than before.

The wording is also very common in the workplace.

So what is there room for improvement?

The wording although very good, but each time the object must traverse the enumeration to obtain enumerated array once, the time complexity is O (n).

Reduce the time complexity of how to do that? A common idea is to space for time .

 

2.3 Optimization again

It can be used to map cache, the map value when used directly.

It can be optimized:

@Getter
public enum CoinEnum {
    PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
 
    CoinEnum(int value) {
        this.value = value;
    }
 
    private final int value;
 
    public int value() {
        return value;
    }
 
    private static final Map<Integer, CoinEnum> cache = new HashMap<>();
 
    static {
        for (CoinEnum coinEnum : CoinEnum.values()) {
            cache.put(coinEnum.getValue(), coinEnum);
        }
    }
 
    public static CoinEnum getEnum(int value) {
        return cache.getOrDefault(value, null);
    }
}

By optimizing the above, the use of the time complexity is O (1), performance has improved.

The actual development of the students can adopt such an approach is not too much.

The main reason is much similar to the online article , which is a lot of love to Baidu to solve the problems of the important reasons to solve the problem of student progress is not thinking small .

 

2.4 life-long learning

By optimizing the above two, the code of the coupling is reduced, performance increased.

So, can a perfect call it a day?

NO...

2.3 gives the code are still some problems:

  • Each enum class will need to write code like this, it is cumbersome.

  • Many enumeration class provides the introduction of the tool, if only enumeration constant, will trigger the execution of static code block.

There is no more elegant options?

I hope you can think about, given their own solutions.

See specific solutions right learning "Ali Baba Java Development Manual" Detailed column of Section 10 enumeration class

 

3 summary

Since the choice of programming this road, I hope you before asking questions, before Baidu, we must first have their own thinking.

When the school community to recruit and recruit, by many large companies face very attractive, but not very helpful for most people. Because many face was only subject no answer, a lot of people will search for answers to recite, but the nature of many so-called standard answer did not reveal the problem, it is not complete, nor is the best answer.

Believe everything the book as no book, I hope you are reading, watching blog, see columns to process more attention to the issue of thinking, learning methods, but remember specific knowledge. As the saying goes, "Give a man a fish, as delegate to fish", I hope this can inspire more friends realize the importance of thinking and methods.

 

----------

If you think this article has helped you, welcome thumbs up, comment, forwarding (attribution), attention, your support is the greatest driving force of my work.

Published 379 original articles · won praise 862 · Views 1.32 million +

Guess you like

Origin blog.csdn.net/w605283073/article/details/103519048