我有一个地图,其键值为long,值为String。地图的值是从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。 我这样做是因为我需要将这些值放入另一个地图中 地图(NAME1,NAME2 NAME3,NAME4)GT; 我可以拆分字符串但是如何将Businesspartner作为其他实体的公共值
有人能告诉我怎么做吗
由于
答案 0 :(得分: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);
}
}
答案 1 :(得分: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
答案 2 :(得分: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