一、在Java正則表達式中點號(.)能夠表明一個中文字符;html
二、Collections.sort()使用-返回負數值的對象排在前面,正值在後面:java
public static void main(String[] args){ List<Integer> list = new ArrayList<Integer>(); list.add(2); list.add(1); list.add(0); list.add(3); Collections.sort(list, new Comparator<Integer>(){ @Override public int compare(Integer o1, Integer o2) { if(o1.intValue() >o2.intValue()){ return 1; } if(o1.intValue() < o2.intValue()){ return -1; } return 0; } }); System.out.println(list); } 結果:[0, 1, 2, 3]
三、如何將System.out.println()打印輸出到指定的文件中,以下:正則表達式
public static void main(String[] args) { try{ PrintStream ps = new PrintStream("D:/log.txt"); System.setOut(ps); System.out.println("print log to file"); }catch(FileNotFoundException e){ e.printStackTrace(); } new Thread(new Runnable() { public void run() { System.out.println("I am thread log"); } }).start(); }
說明:System類的out屬性是全局靜態變量,使得整個系統都會寫入該文件,即便在其它線程裏的打印;api
public final static PrintStream out = nullPrintStream();數據結構
四、關於Map的排序ide
(1)、對Map的鍵進行排序的方法, 能夠使用TreeMap的數據結構,傳遞一個比較器給其構造函數:函數
TreeMap(Comparator<? super K> comparator)
構造一個新的、空的樹映射,該映射根據給定比較器進行排序。spa
(2)、對Map的值進行排序,先將其轉成List對象後再排序,以下: 命令行
Map<String,Integer> countMap=new HashMap<String,Integer>(); for(String s:readLine){ String[] split = s.split("modelName"); String r = "modelName"+split[1]; Integer integer = countMap.get(r); countMap.put(r, (integer==null?1:integer+1)); } List<Entry<String, Integer>> entrySet = new ArrayList<Map.Entry<String,Integer>>(countMap.entrySet()); Collections.sort(entrySet,new Comparator<Entry<String, Integer>>() { public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { if(o1.getValue() > o2.getValue()){ return -1; } if(o1.getValue()<o2.getValue()){ return 1; } return 0; } });
五、如何使用命令行編譯執行java文件,依賴包的使用:線程
(1)編譯
(2)執行:
六、如何執行帶有包路徑的類:
(1)Java文件:
package com.jd.jcc;
public class Test{
public static void main(String[] args){
System.out.println("Test class ");
}
}
(2)執行指令(注意-d後面那個點,表明放在當前路徑):
七、快速實現對象深拷貝
public static Object deepClone(Object obj) throws IOException, ClassNotFoundException{ /* 寫入當前對象的二進制流 */ ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(obj); /* 讀出二進制流產生的新對象 */ ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); return ois.readObject(); }