2013-03-07 98 views
0

我有一个映射,它的键值与long一样长且值为String。地图的值是从数据库中获得的,它的格式如下。要标记哈希映射中的值

1: BusinessPartner.name1,BusinessPartner.name2,BusinessPartner.name3,BusinessPartner.name4 
2: BusinessPartner.name1,BusinessPartner.name2,BusinessPartner.name3 
6: ADDRESS.addressline1,ADDRESS.addressline2,ADDRESS.addressline3 

其中1,2,6是关键。

我需要标记化为键1的字符串,结果应该是 Businesspartner,其他值应该是name1,name2,name3,name4。 我这样做是因为我需要将这些值放入另一个地图 Map(name1,name2,name3,name4)> 我可以拆分字符串,但如何将Businesspartner作为其他实体的公用值

谁能告诉我如何做到这一点

感谢

回答

0

将这项工作你riquierment?

public class Tokenize { 

    static Long keysFromDB[] = {1L, 2L, 6L}; 
    static String stringsFromDB[] = { 
     "BusinessPartner.name1,BusinessPartner.name2,BusinessPartner.name3,BusinessPartner.name4", 
     "BusinessPartner.name1,BusinessPartner.name2,BusinessPartner.name3", 
     "ADDRESS.addressline1,ADDRESS.addressline2,ADDRESS.addressline3"}; 

    @Test 
    public void tokenize() { 
     // use linked hashmap to preserve the order 
     Map<Long, Set<String>> tokenized = new LinkedHashMap<Long, Set<String>>(); 
     int c = 0; 
     for(Long key : keysFromDB) { 
      // use linked hashset to preserve the order 
      Set<String> record = new LinkedHashSet<String>(); 
      String splitedDBStrings[] = stringsFromDB[c++].split("\\.|,"); 
      System.out.println("List: " + Arrays.asList(splitedDBStrings)); 
      for(String s : splitedDBStrings) { 
       record.add(s); 
      } 
      System.out.println("Set: " + record); 
      tokenized.put(key, record); 
     } 

     System.out.println(tokenized); 
    } 
} 
+0

非常感谢你,它的工作。再次感谢 – user1984839 2013-03-07 12:48:23

+0

不客气! – A4L 2013-03-07 12:50:10

0

允许从头开始:

final Pattern pattern = Pattern.compile("[,\\s*]?([^.]+)\\.([^,]+)[,\\s*]?"); 
final Map<Long, String> myMap = getMapFromSomewhere();  

for(final Map.Entry<Long, String> entry : myMap.entrySet()) { 
    final String myString = entry.getValue(); 
    final Matcher matcher = pattern.matcher(myString);  
    final Map<String, List<String>> tokenised = new HashMap<String, List<String>>(); 
    while (matcher.find()) { 
    final String key = matcher.group(1); 
    List<String> names = tokenised.get(key); 
    if(names == null) { 
     names = new LinkedList<String>(); 
     tokenised.put(key, names) 
    } 
    names.add(matcher.group(2)); 
    } 
    //do stuff with map. 
} 

正则表达式分解如下:

  • [,\\s*]?任选匹配逗号后未知的(或零)长度
  • 的空白
  • ([^.]+)\\.匹配到下一站之前的所有内容,然后是“。”。
  • ([^,]+)采取一切高达下一个逗号中的匹配组
  • [,\\s*]?任选逗号后未知的(或零)长度

测试箱子空格相符:

public static void main(String[] args) { 

    final Pattern pattern = Pattern.compile("[,\\s*]?([^.]+)\\.([^,]+)[,\\s*]?"); 

    final String myString = "BusinessPartner.name1,BusinessPartner.name2,BusinessPartner.name3,BusinessPartner.name4"; 
    final Matcher matcher = pattern.matcher(myString); 

    while (matcher.find()) { 
     System.out.println(matcher.group(1)); 
     System.out.println(matcher.group(2)); 
    } 
} 

输出:

BusinessPartner 
name1 
BusinessPartner 
name2 
BusinessPartner 
name3 
BusinessPartner 
name4 
+0

我认为他希望'BusinessPartner'和'ADDRESS'(字符串女巫重复)成为结果的一部分 – A4L 2013-03-07 12:11:40

+0

编辑来做到这一点。 – 2013-03-07 12:23:21

+0

我想他只需要一次,他写道'结果应该是Businesspartner,其他值应该是name1,name2,name3,name4.'因此'BusinessPartner,name1,name2,name3,name4' – A4L 2013-03-07 12:35:38

0

运行此

public static void main(String[] args){ 
    Map<Long, String> dbmap = new HashMap<Long, String>(); 
    dbmap.put((long) 1, "BusinessPartner.name1,BusinessPartner.name2,BusinessPartner.name3,BusinessPartner.name4"); 
    dbmap.put((long) 2, "BusinessPartner.name1,BusinessPartner.name2,BusinessPartner.name3"); 
    dbmap.put((long) 6, "ADDRESS.addressline1,ADDRESS.addressline2,ADDRESS.addressline3"); 

    //Loop through the Map 
    Iterator<Entry<Long, String>> iterator = dbmap.entrySet().iterator(); 
    while(iterator.hasNext()){ 

     Map.Entry<Long, String> entry = (Map.Entry<Long, String>) iterator.next(); 

     //Split the string on comma ',' 
     //result entries should be 'BusinessPartner.name1', 'BusinessPartner.name2' etc 
     String[] commaSplit = entry.getValue().split(","); 

     //loop through each entry 
     for(int x=0; x<commaSplit.length; x++){ 

      //Split on Full Stop 
      //Result should be 'BusinessPartner', 'name2' 
      String[] dotSplit = commaSplit[x].split("\\."); 

      //print out common Value 
      System.out.println("Common Value is : " + dotSplit[0]); 

      //print out second value 
      System.out.println("Second Value is : " + dotSplit[1]); 

      System.out.println(); 
     } 
    } 
} 

输出是这样的

Common Value is : BusinessPartner 
Second Value is : name1 

Common Value is : BusinessPartner 
Second Value is : name2 

Common Value is : BusinessPartner 
Second Value is : name3 

Common Value is : BusinessPartner 
Second Value is : name4 

Common Value is : BusinessPartner 
Second Value is : name1 

Common Value is : BusinessPartner 
Second Value is : name2 

Common Value is : BusinessPartner 
Second Value is : name3 

Common Value is : ADDRESS 
Second Value is : addressline1 

Common Value is : ADDRESS 
Second Value is : addressline2 

Common Value is : ADDRESS 
Second Value is : addressline3 
+0

谢谢朋友们的回复 – user1984839 2013-03-08 07:25:12