Java 常用语句:修订间差异
跳到导航
跳到搜索
(→常见问题) |
|||
第45行: | 第45行: | ||
jval = new JSONObject(); | jval = new JSONObject(); | ||
jval.toJSONString(val) | jval.toJSONString(val) | ||
==== Object 转换 ==== | |||
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; | |||
} | |||
=== 条件 === | === 条件 === |
2024年3月28日 (四) 15:04的版本
转换
数据转为字符串
"" + 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) { 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);