package thyyxxk.webserver.utils; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.Data; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; /** * 物品领用的格式化 * */ public class TestClass { private static final Map> FUNC_MAP = new ConcurrentHashMap<>(); private static final String MAN = "man"; private static final String WOMAN = "woman"; private static final String NONE = ""; static { FUNC_MAP.put(MAN, person -> System.out.println(person + "是男人。")); FUNC_MAP.put(WOMAN, person -> System.out.println(person + "是女人。")); FUNC_MAP.put(NONE, person -> System.out.println(person + "没有性别。")); } @Data static class Person { private String gender; private String name; public Person(String gender, String name) { this.gender = null == gender ? "" : gender; this.name = name; } } public static void main(String[] args) { Person p = new Person(MAN, "张三"); Person p2 = new Person(WOMAN, "李四"); Person p3 = new Person(null, "王五"); FUNC_MAP.get(p.getGender()).accept(p.getName()); FUNC_MAP.get(p2.getGender()).accept(p2.getName()); FUNC_MAP.get(p3.getGender()).accept(p3.getName()); } // public static void main(String[] args) { // String ori = readFile("D:\\yyhc.txt"); // JSONArray arr = JSONArray.parseArray(ori); // Map map = new HashMap<>(); // for (int i = 0; i < arr.size(); i++) { // JSONObject itm = arr.getJSONObject(i); // if (!map.containsKey(itm.getString("code"))) { // JSONArray sizes = new JSONArray(); // JSONObject size = new JSONObject(); // size.put("val", itm.getString("size")); // sizes.add(size); // itm.replace("size", sizes); // map.put(itm.getString("code"), itm); // } else { // JSONObject size = new JSONObject(); // size.put("val", itm.getString("size")); // map.get(itm.getString("code")).getJSONArray("size").add(size); // } // } // // JSONArray dest = new JSONArray(); // for (Map.Entry entry : map.entrySet()) { // dest.add(entry.getValue()); // } // System.out.println(dest); // } // // public static String readFile(String path) { // StringBuilder result = new StringBuilder(); // try { // BufferedReader br = new BufferedReader(new FileReader(new File(path))); // String temp; // while ((temp = br.readLine()) != null) { // result.append(temp); // } // } catch (Exception e) { // e.printStackTrace(); // } // return result.toString(); // } }