【问题标题】:To tokenize the values in a hashmap标记哈希图中的值
【发布时间】:2013-02-22 14:22:13
【问题描述】:

我有一张地图,它的键值和字符串一样长。 map的值是从db中获取的,格式如下。

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。 我这样做是因为我需要将这些值放入另一个地图中 地图(名称1,名称2,名称3,名称4)> 我可以拆分字符串,但如何将 Businesspartner 作为其他实体的公共值

谁能告诉我怎么做

谢谢

【问题讨论】:

    标签: java hashmap stringtokenizer


    【解决方案1】:

    这对您的要求有用吗?

    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);
        }
    }
    

    【讨论】:

      【解决方案2】:

      让我们从头开始:

      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
      

      【讨论】:

      • 我认为他希望 BusinessPartnerADDRESS(字符串女巫正在重复)成为结果的一部分
      • 我想他只想要一次,他写the result should be Businesspartner and the other values should be name1,name2,name3,name4.因此BusinessPartner,name1,name2,name3,name4
      【解决方案3】:

      运行这个

      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
      

      【讨论】:

      • 谢谢朋友的回复
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 2019-10-04
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多