From 4fa4b41faefb74c66bc524ebe179a2ef5078e412 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 15 Mar 2024 09:28:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xyz/zhouxy/plusone/commons/util/Enumeration.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java index b22081f..240356f 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java @@ -16,11 +16,13 @@ package xyz.zhouxy.plusone.commons.util; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.function.Function; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; @@ -86,15 +88,13 @@ public abstract class Enumeration> implements Comparabl @SafeVarargs @StaticFactoryMethod(ValueSet.class) public static > ValueSet of(T... values) { - Map temp = new HashMap<>(); - for (T value : values) { - temp.put(value.getId(), value); - } + Map temp = Arrays.stream(values) + .collect(Collectors.toMap(Enumeration::getId, Function.identity(), (a, b) -> b)); return new ValueSet<>(Collections.unmodifiableMap(temp)); } public T get(int id) { - Preconditions.checkArgument(this.valueMap.containsKey(id), "%s 对应的值不存在", id); + Preconditions.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id); return this.valueMap.get(id); }