Java 常用语句:修订间差异
跳到导航
跳到搜索
第47行: | 第47行: | ||
==== Object 转换 ==== | ==== Object 转换 ==== | ||
public static boolean isnull(Object obj1) { | <small>public static boolean isnull(Object obj1) { | ||
if (obj1 instanceof String) { return ((String) obj1).isEmpty(); } | |||
else if (obj1 instanceof List) { return ((List) obj1).isEmpty(); } | |||
else if (obj1 instanceof Map) { return ((Map) obj1).isEmpty(); } | |||
else if (obj1 instanceof Properties) { return ((Properties) obj1).isEmpty(); } | |||
else { return false; } | |||
}</small> | |||
<small>public static boolean isnull(Object obj1) { | |||
String type1 = obj1.getClass().toString(); | String type1 = obj1.getClass().toString(); | ||
type1 = type1.substring(type1.lastIndexOf(".") + 1); | type1 = type1.substring(type1.lastIndexOf(".") + 1); | ||
switch (type1) { | switch (type1) { | ||
case "String": | case "String" : return ((String) obj1).isEmpty(); | ||
case "ArrayList" : return ((ArrayList<?>) obj1).isEmpty(); | |||
case "ArrayList": | case "HashMap" : return ((HashMap<?, ?>)obj1).isEmpty(); | ||
case "Properties1": return ((Properties) obj1).isEmpty(); | |||
case "HashMap": | |||
case "Properties1": | |||
} | } | ||
return false; | return false; | ||
} | }</small> | ||
=== 条件 === | === 条件 === |
2024年3月28日 (四) 15:25的版本
转换
数据转为字符串
"" + i // 产生两个String对象 String.valueOf(i) // 产生一个对象
字符串转换成整数
String s = "10"; Integer.parseInt(s)
List & Set
List<String> list1 = new ArrayList<>(set1) Set<String> set1 = new HashSet<>(list1)
json 转换
pom.xml
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.9</version> </dependency> import com.google.gson.Gson; Gson gson = new Gson(); String json1 = gson.toJson(val); HashMap<String, ArrayList<String>> val1 = new HashMap<>(); val1 = gson.fromJson(json1, HashMap.class); - and - HashMap<Integer, ArrayList<String>> val; val = new HashMap<Integer, ArrayList<String>>(); -.OR.- <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> import org.json.simple.JSONObject; JSONObject jval; jval = new JSONObject(); jval.toJSONString(val)
Object 转换
public static boolean isnull(Object obj1) { if (obj1 instanceof String) { return ((String) obj1).isEmpty(); } else if (obj1 instanceof List) { return ((List) obj1).isEmpty(); } else if (obj1 instanceof Map) { return ((Map) obj1).isEmpty(); } else if (obj1 instanceof Properties) { return ((Properties) obj1).isEmpty(); } else { return false; } }
public static boolean isnull(Object obj1) { String type1 = obj1.getClass().toString(); type1 = type1.substring(type1.lastIndexOf(".") + 1); switch (type1) { case "String" : return ((String) obj1).isEmpty(); case "ArrayList" : return ((ArrayList<?>) obj1).isEmpty(); case "HashMap" : return ((HashMap<?, ?>)obj1).isEmpty(); case "Properties1": return ((Properties) obj1).isEmpty(); } return false; }
条件
判断一个字符串为空
null == s || "".equals(s)
比较两个字符串,true=相等
s.equals(s2)
找出字符串位置,-1=不含
s.indexOf(s2)
获取变量类型
.getClass().toString()
循环
list
ArrayList<String> tmp; for (String i : tmp) { System.out.println(tmp.get(i)); }
map
HashMap<String, String> tmp; tmp.forEach((key, value) -> { System.out.println(String.format("key: %s, val: %s", key, value)); });
properties
Properties tmp = new Properties(); for (String key : tmp.stringPropertyNames()) { System.out.println(key + "=" + tmp.getProperty(key)); }
获取信息
当前执行的类名、方法名
- File : Thread.currentThread().getStackTrace()[2].getFileName()
- Class : Thread.currentThread().getStackTrace()[2].getClassName()
- Method: Thread.currentThread().getStackTrace()[2].getMethodName()
- Line : Thread.currentThread().getStackTrace()[2].getLineNumber()
String log1 = "test logger"; Logger logger = Logger.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); logger.info(log1); --> 2024-03-20 09:25:42,222 INFO [test] test logger
常见问题
同一个对象多次 add
添加对象时,添加的是它的引用。所以多次 add,发现多条记录都是最后对象的值。
每次 new 对象再 add,如:
HashMap<Integer, ArrayList<String>> val = new HashMap<>(); ArrayList<String> l1 = new ArrayList<>(); for (i=0;...) l1 = new ArrayList<>(); ... val.put(i, l1);