在作測試的時候,常常須要把一些信息輸出到控制檯,可是格式上比較亂,想弄成一個相似SQL客戶端的那個輸出格式,在參考了一些資料後本身寫了一個簡單的控制檯網格輸出的類,分享代碼供你們參考。java
使用方法:暫時支持了map和list兩種類型的數據展現,並無提供header功能。git
public static void main(String[] args) { List<String> ss0 = Arrays.asList("234", "432", "54"); List<String> ss3 = Arrays.asList("234", "432", "54", "54", "54"); List<String> ss1 = Arrays.asList("6546", "7675"); Map<String, String> sss = new HashMap<>(); sss.put(getNanoMark() + EMPTY, "fdsf"); sss.put(getNanoMark() + EMPTY, "fdsfdsaff"); sss.put(getNanoMark() + EMPTY, "fdsf"); sss.put(getNanoMark() + EMPTY, "fdsfafdsf"); sss.put(getNanoMark() + EMPTY, "fdsf"); sss.put(getMark() + EMPTY, "fdsf"); show(sss); List<List<String>> rows = Arrays.asList(ss1, ss3, ss0); show(rows); JSONObject json = new JSONObject(); json.put("3234", 32432); json.put("323dsa4", 32432); json.put("3fdsa234", 32432); json.put("323fdsf4", 32432); json.put("32d34", 32432); json.put("32fdsafdf34", 32432); show(json); }
效果展現: 類代碼以下(下一步優化其餘數據類型和header以及邊欄支持):apache
package com.fun.utils; import com.fun.frame.SourceCode; import net.sf.json.JSONObject; import org.apache.commons.lang3.StringUtils; import java.util.*; import java.util.stream.Collectors; public class ConsoleTable extends SourceCode { List<Integer> rowLength = new ArrayList<>(); public static void show(Map map) { new ConsoleTable(map); } public static void show(List<List<String>> rows) { new ConsoleTable(rows); } /** * 輸出map * * @param map */ private ConsoleTable(Map map) { Set set = map.keySet(); int asInt0 = set.stream().mapToInt(key -> key.toString().length()).max().getAsInt(); rowLength.add(asInt0 + 2); List<String> values = new ArrayList<>(); set.forEach(key -> values.add(map.get(key).toString())); int asInt1 = values.stream().mapToInt(value -> value.length()).max().getAsInt(); rowLength.add(asInt1 + 2); StringBuffer stringBuffer = new StringBuffer(LINE + getHeader()); map.forEach((k, v) -> { stringBuffer.append(getCel(0, k.toString())); stringBuffer.append(getCel(1, v.toString())); }); output(stringBuffer.append(LINE + getHeader()).toString()); } /** * 輸出list * * @param rows */ private ConsoleTable(List<List<String>> rows) { for (int i = 0; i < rows.size(); i++) { List<String> line = rows.get(i); for (int j = 0; j < line.size(); j++) { String s = line.get(j); if (rowLength.size() <= j) rowLength.add(0); if (rowLength.get(j) < s.length()) rowLength.set(j, s.length()); } } rowLength = rowLength.stream().map(n -> n + 2).collect(Collectors.toList()); StringBuffer stringBuffer = new StringBuffer(LINE + getHeader()); for (int i = 0; i < rows.size(); i++) { List<String> line = rows.get(i); for (int j = 0; j < rowLength.size(); j++) { stringBuffer.append(getCel(j, j < line.size() ? line.get(j) : EMPTY)); } } output(stringBuffer.append(LINE + getHeader()).toString()); } /** * 獲取每一格的string * * @param colum 列 * @param content 格內容 * @return */ public String getCel(int colum, String content) { Integer integer = rowLength.get(colum); int i = integer - content.length(); return (colum == 0 ? LINE + PART : PART) + getManyString(SPACE_1, i / 2) + content + getManyString(SPACE_1, i - i / 2) + (rowLength.size() - colum == 1 ? PART : EMPTY); } /** * 獲取頭尾行 * * @return */ private String getHeader() { List<String> collect = rowLength.stream().map(size -> getManyString("-", size)).collect(Collectors.toList()); return "+" + StringUtils.join(collect.toArray(), "+") + "+"; } }
這裏使用了一些stream的用法,雖然比較生疏,可是效果很不錯,stream語法很強大,使得編碼效率急速提高。在groovy語言使用java的stream各類方法時,仍是遇到了很多的坑,目前主要仍是符號兼容的問題比較多,因此儘可能仍是java stream的本身的語法比較好。json