1、合并数组
int [] arry1 = {1,2,3,4,5}; int [] arry2 = {6,7,8,9,0}; int [] arry3 = new int[arry1.length+arry2.length]; System.arraycopy(arry1, 0, arry3, 0, arry1.length); System.arraycopy(arry2, 0, arry3, arry1.length, arry2.length); System.out.println(Arrays.toString(arry3)); ------------------------------------------------------------------------------ [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
2、对象序列化为json,json反序列化为对象
try { D d = new D(); d.setA("a"); ObjectMapper om = new ObjectMapper(); ObjectWriter ow = om.writerWithView(d.getClass()); System.out.println(ow.writeValueAsString(d)); ObjectReader or = om.reader(D.class); D s = or.readValue(ow.writeValueAsString(d)); System.out.println(s.getA()); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ---------------------------------------------------------------------------------- {"a":"a","t":{"name":"zhangSan"},"paramTest":"param"} a
3、json转换为map(bean转换为map键值对)
try { D d = new D(); d.setA("a"); d.setT(new T("zhangSan")); d.setParamTest("param"); ObjectMapper om = new ObjectMapper(); ObjectWriter ow = om.writerWithView(d.getClass()); String result = ow.writeValueAsString(d); System.out.println(result); JSONObject jasonObject = JSONObject.fromObject(result); HashMap<String, Object> data = new HashMap<String, Object>(); Iterator it = jasonObject.keys(); // 遍历jsonObject数据,添加到Map对象 while (it.hasNext()) { String key = String.valueOf(it.next()); Object value = jasonObject.get(key); data.put(key, value); } System.out.println(data.toString()); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ------------------------------------------------------------------------------------- {"a":"a","t":{"name":"zhangSan"},"paramTest":"param"} {t={"name":"zhangSan"}, a=a, paramTest=param}
4、进制转换
System.out.println(Integer.toBinaryString(8));//十进制转二进制 System.out.println(Integer.toHexString(8));//十进制转十六进制 System.out.println(Integer.toOctalString(8));//十进制转八进制 System.out.println(Integer.valueOf("1000", 2));//二进制转十进制 -------------------------------------------------------------------------------------- 1000 8 10 8
5、list自定义排序
List<CollectionTest> list = new ArrayList<CollectionTest>(); list.add(new CollectionTest(1,DateUtils.addHours(new Date(), 2))); list.add(new CollectionTest(2,DateUtils.addHours(new Date(), 1))); list.add(new CollectionTest(3,DateUtils.addHours(new Date(), 4))); list.add(new CollectionTest(4,DateUtils.addHours(new Date(), 3))); //这里是倒序 Collections.sort(list, new Comparator<CollectionTest>() { @Override public int compare(CollectionTest arg0, CollectionTest arg1) { if (arg0.getOrder1().getTime() < arg1.getOrder1().getTime()) { return 1; } else if (arg0.getOrder1().getTime() > arg1.getOrder1().getTime()) { return -1; } else { return 0; } } }); for (CollectionTest collectionTest : list) { System.out.println(collectionTest.getOrder()); System.out.println(collectionTest.getOrder1()); } ----------------------------------------------------------------------------------------- 3 Fri Jun 26 21:19:05 CST 2015 4 Fri Jun 26 20:19:05 CST 2015 1 Fri Jun 26 19:19:05 CST 2015 2 Fri Jun 26 18:19:05 CST 2015
6、正则表达式基本使用
Pattern p=Pattern.compile(".*.*/.*.*/service/.*.*/.*Service.*"); System.out.println(p.matcher("com/paic/cicp/test/biz/service/impl/UserServiceImpl").matches()); System.out.println(p.matcher("com/paic/cicp/test/biz/service/impl/UserService.i").matches()); String s = "123"; System.out.println(s.matches("\\d{3}")); --------------------------------------------------------------------------------------- true true true
7、分隔,代替split
StringTokenizer tokenizer = new StringTokenizer("I|身份证", "|"); System.out.println(tokenizer.nextToken()); System.out.println(tokenizer.nextToken()); ---------------------------------------------------------------------------------------- I 身份证
8、浅复制、深复制
//浅复制(只复制基本类型,不支持自定义复杂类型) BeanUtils.cloneBean(object); //深复制(串行化,支持自定义复杂类型,要求自定义类型实现Serializable) public static Object deepClone(Object src) throws Throwable { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(src); oos.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); Object o = ois.readObject(); ois.close(); return o; } //深复制(通过序列换、反序列化实现,支持自定义复杂类型,要求自定义类型实现Serializable) public static <T> T deepClone1(T t){ T r = null; try { ObjectMapper om = new ObjectMapper(); ObjectWriter ow = om.writerWithView(t.getClass()); String serResult = ow.writeValueAsString(t); System.out.println(serResult); ObjectReader or = om.reader(t.getClass()); r = or.readValue(serResult); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return r; }
9、
10、
11、
12、
13、
14、
15、
16、
17、
18、
19、
20、