我如何使用Java 8 Feature(Stream)迭代字符串并修改某些字段

问题描述 投票:0回答:1

我想对单个字符串执行多项操作。我需要获取一个String并使用定界符(“;”)提取不同的子字符串,然后再次使用定界符(“:”)提取不同的子字符串,如果字段匹配,则更新字段并最终将它们与这样的原始状态:firstName:Saroj; email:[email protected]; eaOldId; city:; center:com.test.Center :: 0/69会变成: firstName:xxxxxx;电子邮件:[email protected]; eaOldId; city:; center:com.test.Center :: 0/69问题的基本需求是我需要将某些必填字段[此处'firstName','email']修改为['xxxxxx','[email protected]']。我想加密敏感数据。

[我已经在主类中尝试了以下常规方法

String shortData = "firstName:Saroj;email:[email protected];eaOldId;city:;center:com.test.Center::0/69";
Map<String, List<String>> props = getProperties();
if(props.get("modelClasses").contains("com.test.Member")) {
   List<String> modelFields = props.get(getModelClass());
   List<String> shortDta = Stream.of(shortData.split(";")).map (elem -> new String(elem)).collect(Collectors.toList());
   StringBuilder sb = new StringBuilder();
   for(String data  : shortDta){
      String[] dta = data.split(":");
      if(dta.length>2){
          dta = data.split("=", 2);
      }
      if(dta.length == 1){
         sb.append(data).append(";");
         continue;
      }
      String key = dta[0];
      if(modelFields.stream().anyMatch(d -> d.equalsIgnoreCase(key))){
           String email = props.get("email").toString().replace("[", "").replace("]", "");
           String pattern = props.get("commonPattern").toString().replace("[", "").replace("]", "");
           sb.append(dta[0]).append(":").append(dta[0].equals("email") ? email : pattern).append(";");
      } else {
        sb.append(dta[0]).append(":;");
      }
   }
   if (sb.length() > 0) {
       sb.delete(sb.length() - 1, sb.length());
   }

以下是我从属性文件中获得的数据[此处手动转换为所需对象]

private Map<String, List<String>> getProperties(){
        Map<String, List<String>> props = new HashMap<>();
        List<String> str1 = new ArrayList<>();
        str1.add("firstName");
        str1.add("email");
        List<String> str2 = new ArrayList<>();
        str2.add("com.test.Member");
        str2.add("com.test.Contact");
        props.put("modelClasses", str2);
        props.put("com.test.Member", str1);
        props.put("com.test.Contact", str1);
        props.put("commonPattern", Arrays.asList("xxxxxxx"));
        props.put("email", Arrays.asList("[email protected]"));
        return props;
 }

如果您发现字符串中存在多个[],例如center:com.test.Center :: 0/69我也需要注意这一点。

我可以使用Java 8 Streams做同样的事情吗?非常感谢任何帮助。谢谢!!!

regex lambda java-8 stream java-stream
1个回答
1
投票

我可能在这里只将String#replaceAll用于正则表达式的单行选项:

String input = "firstName:Saroj;email:[email protected];eaOldId;city:;center:com.test.Center::0/69";
System.out.println(input);
input = input.replaceAll("(?<=\\bfirstName:)[^;]+", "xxxxxx");
input = input.replaceAll("(?<=\\bemail:)[^@]+@[^;]+(\\.[^;]+)", "xxxxx@xxx$1");
System.out.println(input);

此打印:

firstName:Saroj;email:[email protected];eaOldId;city:;center:com.test.Center::0/69
firstName:xxxxxx;email:[email protected];eaOldId;city:;center:com.test.Center::0/69

注意:我可能建议完全屏蔽电子邮件,即使用[email protected],以免甚至泄露有关用户真实电子邮件地址的any信息。通过显示诸如域和/或子域之类的内容,您可能会更容易发现此信息。

© www.soinside.com 2019 - 2024. All rights reserved.