2012-11-06 68 views
-4

例如:如何映射到Java轉換爲分隔的格式

key1: 1,2,3,4 
key2: 5,6 

將轉換爲

key1|key2 
1|5 
2|6 
3 
4 

而且我知道,如果我們這樣做反覆,有很多不屬於有效的方法。我想知道是否有任何內置方法或任何可以直接轉換它們的方法。

HashMap<String, String> h1 = new HashMap<String, String>; 
h1.put("key1", "1,2,3,4"); 
h1.put("key2", "5,6"); 

需要打印出來作爲管道分隔格式:

key1|key2 
1|5 
2|6 
3 
4 
+0

你的問題不清楚 - 請澄清輸入是什麼。 (你的標題說地圖,但是'key1:1,2,3,4 key2:5,6'看起來不像一個)。 – assylias

+0

你剛剛擁有兩把鑰匙嗎? –

+2

這可能是一個'Map >'? –

回答

0

您可以使用一類像這樣的:

import java.util.*; 

class LegacyGlueifier 
{ 
    private LegacyGlueifier() 
    { 
    } 

    public static String generateLegacyDataset(Map<String, String> data) 
    { 
     final ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>(); 
     final int width = data.size(); 

     int i = 0; 
     for (Map.Entry<String, String> entry : data.entrySet()) 
     { 
      String[] values = entry.getValue().split(","); 
      changeDims(lists, width, values.length + 1); 

      for (int j = 0; j < values.length; ++j) setValue(lists, j + 1, i, values[j]); 
      setValue(lists, 0, i, entry.getKey()); 
      ++i; 
     } 

     return stringify(lists); 
    } 

    private static void changeDims(ArrayList<ArrayList<String>> lists, int width, int newHeight) 
    { 
     while (lists.size() < newHeight) lists.add(arrayListOfSize(width)); 
    } 

    private static ArrayList<String> arrayListOfSize(int w) 
    { 
     ArrayList<String> list = new ArrayList<String>(w); 
     while (list.size() < w) list.add(null); 
     return list; 
    } 

    private static void setValue(ArrayList<ArrayList<String>> lists, int row, int col, String val) 
    { 
     ArrayList<String> temp = lists.get(row); 
     temp.set(col, val); 
     // System.out.println("SET: " + row + " " + col + ": " + val); 
    } 

    private static String swapNullWithEmpty(String s) 
    { 
     if (s == null) return ""; 
     return s; 
    } 

    private static String stringify(ArrayList<ArrayList<String>> lists) 
    { 
     StringBuilder sb = new StringBuilder(); 
     for (ArrayList<String> sublist : lists) 
     { 
      if (sublist.size() != 0) sb.append(swapNullWithEmpty(sublist.get(0))); 
      for (int i = 1; i < sublist.size(); ++i) 
       sb.append("|").append(swapNullWithEmpty(sublist.get(i))); 
      sb.append("\n"); 
     } 

     return sb.toString(); 
    } 
} 

調用是String dataset = LegacyGlueifier.generateLegacyDataset(myMap)

I ran it through a basic test case to see if it worked但我還是測試更加徹底,因爲你會使用它。

其時間複雜度介於原始數據集中逗號分隔字段總數的線性之間,與輸出數據集中的字段總數(包括空白字段)呈線性關係。

0

這將做轉型。代碼看起來很複雜,但總體複雜性仍然是O(n):無論地圖的大小如何,每個鍵和值都會觸及固定次數。

public static void main(final String[] args) { 
    Map<String, String> map = getMap(); 
    Map<String, String[]> map2 = new TreeMap<>(); 

    // (1) Read the map into an intermediate map and 
    // get the number of rows needed 
    int maxSize = 0; 
    for (Map.Entry<String, String> entry : map.entrySet()) { 
     String[] array = entry.getValue().split(","); 
     maxSize = array.length > maxSize ? array.length : maxSize; 
     map2.put(entry.getKey(), array); 
    } 

    // (2) prepare the table structure 
    List<List<String>> table = new ArrayList<>(); 
    for (int i = 0; i < (maxSize + 1); i++) { 
     table.add(new ArrayList<String>()); 
    } 

    // (3) read the values into the table structure 
    for (Map.Entry<String, String[]> entry : map2.entrySet()) { 
     table.get(0).add(entry.getKey()); 
     for (int i = 0; i < maxSize; i++) { 
      if (i < entry.getValue().length) { 
       table.get(i + 1).add(entry.getValue()[i]); 
      } else { 
       table.get(i + 1).add(""); 
      } 
     } 
    } 

    // (4) dump the table 
    for (List<String> row : table) { 
     StringBuilder rowBuilder = new StringBuilder(); 
     boolean isFirst = true; 
     for (String value : row) { 
      if (isFirst) { 
       isFirst = false; 
      } else { 
       rowBuilder.append('|'); 
      } 
      rowBuilder.append(value); 
     } 
     System.out.println(rowBuilder.toString()); 
    } 

} 

private static Map<String, String> getMap() { 
    Map<String, String> map = new TreeMap<>(); 
    map.put("key1", "1,2,3,4"); 
    map.put("key2", "5,6"); 
    map.put("key3", "7,8,9"); 
    return map; 
} 

此示例的結果是:

key1|key2|key3 
1|5|7 
2|6|8 
3||9 
4|| 

(第一應答的基礎上,錯誤猜測)

假設,5和6是 1和2,那麼這是一個很好的解決方案:

public static void dumpMap(Map<String, String> map) { 
    for (Map.Entry<String, String> entry:map.entrySet()) { 
     System.out.printf("%s|%s%n", entry.getKey(), nullSafe(entry.getValue())); 
    } 
} 

private static String nullSafe(String value) { 
    return value == null ? "" : value; 
} 

這是O(n),我們不能做得更有效率,因爲我們必須訪問每個鍵/值對來打印一次。

(除非我們可以使用並行計算;))

+0

不,地圖像'{「key1」:「1,2,3,4」,「key2」:「5,6」}' 。這並不是小事,你必須並行地遍歷所有的值(解析後),並根據map條目的數量創建一個printf模板。 –

+0

Marko,我在Victor向我們展示了地圖的樣子之前創建了答案 –

+0

我在看到之前評論過:)我將它基於最初存在的電子表格樣式顯示。 –

相關問題