1

我有一张地图,其中的键值和字符串一样长。地图的值是从数据库中获得的,格式如下。

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 作为其他实体的公共值

谁能告诉我该怎么做

谢谢

4

3 回答 3

0

这对你的要求有用吗?

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);
    }
}
于 2013-03-07T12:03:50.517 回答
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
于 2013-03-07T12:07:25.413 回答
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
于 2013-03-07T12:11:41.050 回答