publicclassClient { publicstaticvoidmain(String[] args) { //注意summer是小写 List<String> params = Arrays.asList("Spring", "summer"); for (String name : params) { //查找表面值与name相同的枚举项 Seasons= Season.valueOf(name); if (s != null) { // 有该枚举项时的处理 System.out.println(s); } else { // 没有该枚举项时的逻辑处理 System.out.println("无相关枚举项"); } } } }
enumSeason { Spring, Summer, Autumn, Winter; }
运行输出:
1 2 3 4 5
Spring Exception in thread "main" java.lang.IllegalArgumentException: No enum constant cn.summerchill.test.Season.summer at java.lang.Enum.valueOf(Unknown Source) at cn.summerchill.test.Season.valueOf(Client.java:1) at cn.summerchill.test.Client.main(Client.java:12)
/** * Returns a fixed-size list backed by the specified array. (Changes to * the returned list "write through" to the array.) This method acts * as bridge between array-based and collection-based APIs, in * combination with {@link Collection#toArray}. The returned list is * serializable and implements {@link RandomAccess}. * * <p>This method also provides a convenient way to create a fixed-size * list initialized to contain several elements: * <pre> * List<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); * </pre> * * @param a the array by which the list will be backed * @return a list view of the specified array */ @SafeVarargs publicstatic <T> List<T> asList(T... a) { returnnewArrayList<>(a); }
/** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, as this method is likely * to yield significantly better space and time performance by * caching frequently requested values. * * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */ publicstatic Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; returnnewInteger(i); }
/** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. The size of the cache * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option. * During VM initialization, java.lang.Integer.IntegerCache.high property * may be set and saved in the private system properties in the * sun.misc.VM class. */
static { // high value may be configured by property inth=127; StringintegerCacheHighPropValue= sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { inti= parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h;
在 Boxing Conversion 部分的Java语言规范(JLS)规定如下: 如果一个变量 p 的值属于:-128至127之间的整数(§3.10.1这个估计是版本号吧),true 和 false的布尔值 (§3.10.3),’u0000′ 至 ‘u007f’ 之间的字符(§3.10.4)中时,将 p 包装成 a 和 b 两个对象时,可以直接使用 a == b 判断 a 和 b 的值是否相等。
/** * Creates a new random number generator. This constructor sets * the seed of the random number generator to a value very likely * to be distinct from any other invocation of this constructor. */ publicRandom() { this(seedUniquifier() ^ System.nanoTime()); }
/** * Creates a new random number generator using a single {@code long} seed. * The seed is the initial value of the internal state of the pseudorandom * number generator which is maintained by method {@link #next}. * * <p>The invocation {@code new Random(seed)} is equivalent to: * <pre> {@code * Random rnd = new Random(); * rnd.setSeed(seed);}</pre> * * @param seed the initial seed * @see #setSeed(long) */ publicRandom(long seed) { if (getClass() == Random.class) this.seed = newAtomicLong(initialScramble(seed)); else { // subclass might have overriden setSeed this.seed = newAtomicLong(); setSeed(seed); } }
/** * Returns a {@code double} value with a positive sign, greater * than or equal to {@code 0.0} and less than {@code 1.0}. * Returned values are chosen pseudorandomly with (approximately) * uniform distribution from that range. * * <p>When this method is first called, it creates a single new * pseudorandom-number generator, exactly as if by the expression * * <blockquote>{@code new java.util.Random()}</blockquote> * * This new pseudorandom-number generator is used thereafter for * all calls to this method and is used nowhere else. * * <p>This method is properly synchronized to allow correct use by * more than one thread. However, if many threads need to generate * pseudorandom numbers at a great rate, it may reduce contention * for each thread to have its own pseudorandom-number generator. * * @return a pseudorandom {@code double} greater than or equal * to {@code 0.0} and less than {@code 1.0}. * @see Random#nextDouble() */ publicstaticdoublerandom() { return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble(); }