Converting Strings to Enums in Java – 在Java中把字符串转换为枚举

最后修改: 2017年 3月 29日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this short tutorial, we’ll learn how to quickly convert a String into an enum in Java.

在这个简短的教程中,我们将学习如何在Java中快速将一个字符串转换成一个枚举。

2. Setup

2.设置

We’re dealing with core Java, so we don’t need to add any additional artifacts. We’ll also be working with the PizzaDeliveryStatusEnum from the enums guide article.

我们正在处理核心Java,所以我们不需要添加任何额外的工件。我们还将使用enums指南文章中的PizzaDeliveryStatusEnum

3. The Conversion

3.转换

Enums are similar to standard Java classes, and we can access their values using the dot notation. So to access the READY value of PizzaDeliveryStatusEnum, we would use:

Enums类似于标准的Java类,我们可以使用点符号访问它们的值。所以要访问PizzaDeliveryStatusEnumREADY值,我们将使用。

PizzaStatusEnum readyStatus = PizzaStatusEnum.READY;

This is fine, but what if we had the value of the status stored as a String, and wanted to convert it into a PizzaStatusEnum? The naive way of doing this would be to write a giant switch statement, returning the correct value of the enum for each of its possible values. But writing and maintaining such code is a nightmare, and we should avoid it at all costs.

这很好,但是如果我们把状态的值存储为String,并想把它转换成PizzaStatusEnum呢?天真的做法是写一个巨大的switch语句,为每个可能的值返回enum的正确值。但编写和维护这样的代码是一场噩梦,我们应该不惜一切代价避免它。

On the other hand, the enum type provides a valueOf() method that takes a String as an argument, and returns the corresponding enum object:

另一方面,enum类型提供了一个valueOf()方法,它接受一个String作为参数,并返回相应的enum对象:

PizzaStatusEnum readyStatus = PizzaStatusEnum.valueOf("READY");

We can check that this approach actually works through a unit test:

我们可以通过单元测试来检查这种方法是否真的有效。

@Test
public void whenConvertedIntoEnum_thenGetsConvertedCorrectly() {
 
    String pizzaEnumValue = "READY";
    PizzaStatusEnum pizzaStatusEnum
      = PizzaStatusEnum.valueOf(pizzaEnumValue);
    assertTrue(pizzaStatusEnum == PizzaStatusEnum.READY);
}

It’s important to remember that the valueOf() method does a case-sensitive match of the argument supplied to it, so passing a value that doesn’t match the case of any of the original enum‘s values will lead to an IllegalArgumentException:

重要的是要记住,valueOf()方法对提供给它的参数进行了区分大小写的匹配,所以传递一个与原始enum的任何值的大小写不匹配的值将导致IllegalArgumentException

@Test(expected = IllegalArgumentException.class)
public void whenConvertedIntoEnum_thenThrowsException() {
    
    String pizzaEnumValue = "rEAdY";
    PizzaStatusEnum pizzaStatusEnum
      = PizzaStatusEnum.valueOf(pizzaEnumValue);
}

Passing a value that’s not part of the original enum‘s values also leads to an IllegalArgumentException:

传递一个不属于原始enum的值也会导致IllegalArgumentException

@Test(expected = IllegalArgumentException.class)
public void whenConvertedIntoEnum_thenThrowsException() {
    String pizzaEnumValue = "invalid";
    PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue);
}

4. Conclusion

4.结论

In this brief article, we illustrated how to convert a String into an enum.

在这篇简短的文章中,我们说明了如何将一个String转换成enum

We highly recommend using the built-in valueOf() method of the enum type, instead of doing the conversion ourselves.

我们强烈建议使用enum类型的内置valueOf()方法,而不是自己做转换。

As always, the code for this article can be found over on GitHub.

像往常一样,本文的代码可以在GitHub上找到over