字符串处理,map使用 key value迭代器使用,switch使用
字符串处理,map使用 key value迭代器使用,switch使用
package com.example.core.mydemo.java2; import java.util.*; public class SwitchTest { public static void main(String[] args) { //switch使用 /** * output: * convertCouponType1=A * convertCouponType2=B * convertCouponType3=C * convertCouponType4=B */ System.out.println("convertCouponType1=" + convertCouponType("1")); System.out.println("convertCouponType2=" + convertCouponType("2")); System.out.println("convertCouponType3=" + convertCouponType("3")); System.out.println("convertCouponType4=" + convertCouponType("4")); //字符串处理 String s1 = "上海+aaa+1"; String s2 = "北京+aaa+1"; String s3 = "深圳+ccc+3"; List<String> list = new ArrayList<String>(); list.add(s1); list.add(s2); list.add(s3); int idx0 = s1.indexOf("+"); /** * output: * idx0=2 * idx1=+aaa+1 * idx2=上海 */ System.out.println("idx0=" + idx0); System.out.println("idx1=" + s1.substring(idx0)); System.out.println("idx2=" + s1.substring(0,idx0)); /** * 解析的字符串,后面不变的是 key,前面城市的是value */ Map<String,String> map = new HashMap<>(); for (String ss : list) { int idx = ss.indexOf("+"); if(map.containsKey(ss.substring(idx))){ map.put(ss.substring(idx), map.get(ss.substring(idx)) + "/" + ss.substring(0, idx)); }else{ map.put(ss.substring(idx), ss.substring(0, idx)); } } /** * output: * map={+ccc+3=深圳, +aaa+1=上海/北京} */ System.out.println("map=" + map); //map判断是否存在key // map.put("aaa","上海"); // if(map.containsKey("aaa")) { // map.put("aaa", map.get("aaa")+"/"+"北京"); // } /** * 数据还原 */ List<String> handleList = new ArrayList<>(); Iterator iter = map.keySet().iterator(); while(iter.hasNext()){ String key = (String)iter.next(); //还原:将value 城市 + key 后面不变的字符 /** * output: * test=深圳+ccc+3 * test=上海/北京+aaa+1 */ System.out.println("test=" + map.get(key) + key); handleList.add(map.get(key) + key); } /** * handleList=[深圳+ccc+3, 上海/北京+aaa+1] */ System.out.println("handleList=" + handleList); } private static String convertCouponType(String couponTypeParam) { switch (couponTypeParam){ case "1": return "A"; case "2": return "B"; case "3": return "C"; } return "B"; } }
package com.example.core.mydemo.java; import java.util.*; /** * output: * map2={carPlatNum2=车B6991B2, CarPlatNum=车B6991B, carPlatNum=车B6991B} * entity1=车B6991B2 * entity1=车B6991B * entity1=车B6991B * entity2=车B6991B2 * entity2=车B6991B * entity2=车B6991B */ public class MapTest { public static void main(String[] args) { Map map = new HashMap(); map.put("carPlatNum","车B6991B"); map.put("CarPlatNum","车B6991B"); Map map2 = new HashMap(); map2.put("carPlatNum2","车B6991B2"); map2.put("CarPlatNum","车B6991B2"); // map.putAll(map2); // //output: map={CarPlatNum=车B6991B2, carPlatNum2=车B6991B2, carPlatNum=车B6991B} // System.out.println("map=" + map); map2.putAll(map); //output: map2={carPlatNum2=车B6991B2, CarPlatNum=车B6991B, carPlatNum=车B6991B} System.out.println("map2=" + map2); Set set = map2.keySet(); Iterator<String> iterator = set.iterator(); while(iterator != null && iterator.hasNext()) { String key = iterator.next(); System.out.println("entity1=" + map2.get(key)); } //根据值遍历 Collection set2 = map2.values(); Iterator<String> iterator2 = set2.iterator(); while(iterator2 != null && iterator2.hasNext()) { String entity = iterator2.next(); System.out.println("entity2=" + entity); } } }