public static final String RE_HTML_MARK = "(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)"; private boolean isMaliciousRequest(Map<String, String[]> parameterMap) { return Pattern.matches(RE_HTML_MARK, parameterMap.); } 如何正确续写这段代码,使其遍历parameterMap,判断是否符合RE_HTML_MARK正则表达式?
时间: 2024-03-26 20:40:46 浏览: 244
您可以使用以下代码将参数遍历并检查是否符合正则表达式:
```
for (String[] values : parameterMap.values()) {
for (String value : values) {
if (Pattern.matches(RE_HTML_MARK, value)) {
return true;
}
}
}
return false;
```
这将针对参数映射中的每个值执行循环,并使用 `Pattern.matches` 方法检查该值是否符合 `RE_HTML_MARK` 正则表达式。如果找到匹配项,则返回 `true`,否则返回 `false`。
相关问题
2).自定义异常:任意输入一学生的成绩,用自定义异常处理所输入的成绩是否 小于0或大于100,并在程序中进行异常处理,若没异常直接输出该学生的成绩。 要求把下面空缺处补全,完成题目要求。 Input: import java.util.Scanner; class Myexption extends Exception { private static final long serialVersionUID = 1L; public String myMessm() { return ("成绩不能小于0或大于100"); } } class Stu_mark { public void show() { int m; Scanner rdata = new Scanner(System.in); System.out.print("请输入一个学生的成绩:"); m = rdata.nextInt(); try { if(m<0||m>100) { } System.out.print("学生成绩为:"+m); } catch (Myexption e) { System.out.println(e.myMessm()); } } } public class Ja04 { public static void main(String[] args) { Stu_mark s1 = new Stu_mark();s1.show(); } }
import java.util.Scanner;
class MyException extends Exception {
private static final long serialVersionUID = 1L;
public String myMessm() {
return ("成绩不能小于0或大于100");
}
}
class Stu_mark {
public void show() {
int m;
Scanner rdata = new Scanner(System.in);
System.out.print("请输入一个学生的成绩:");
m = rdata.nextInt();
try {
if (m < 0 || m > 100) {
throw new MyException();
}
System.out.print("学生成绩为:" + m);
} catch (MyException e) {
System.out.println(e.myMessm());
}
}
}
public class Ja04 {
public static void main(String[] args) {
Stu_mark s1 = new Stu_mark();
s1.show();
}
}
public void setProfit(BigDecimal profit, String tradeId, String businessCode,String noteCode) { TradeReportContextData contextData = (TradeReportContextData) reportContextRegister.getContextData(); List<TradeDataMarket> dataMarketList = contextData.getDatemarket(); if(AtsObjectUtils.isEmpty(dataMarketList)){ dataMarketList=new ArrayList<>(); contextData.setDatemarket(dataMarketList); } boolean isNeedNewObject = true; for (TradeDataMarket dataMarket : dataMarketList) { if (dataMarket.getTradeid().equals(tradeId)) { dataMarket.setHedgingcostprofit(profit); isNeedNewObject = false; } } if (isNeedNewObject) { TradeDataMarket dataMarket = new TradeDataMarket(); dataMarket.setHedgingcostprofit(profit) .setTradeid(tradeId) .setDatabatchno(AtsDateUtils.formatDate(AtsDateUtils.newDate(),AtsDateUtils.DATE_FMT)) .setBusinesscode(businessCode) .setNotecode(noteCode); dataMarketList.add(dataMarket); } } 优化这段代码
以下是对该段代码的优化建议:
1. 建议将常量提取到类的静态常量中,如:private static final String DATE_FMT = "yyyyMMdd"。
2. 建议将方法的参数设置为对象,而不是多个参数,可以提高代码的可读性和可维护性。
3. 建议使用Java8的Stream API来简化代码,如: dataMarketList.stream().filter(dataMarket -> dataMarket.getTradeid().equals(tradeId)).findFirst().ifPresent(dataMarket -> dataMarket.setHedgingcostprofit(profit));
4. 建议通过使用Optional来减少null值检查的代码。
以下是优化后的代码:
```
public void setProfit(TradeDataMarket dataMarket, BigDecimal profit) {
TradeReportContextData contextData = reportContextRegister.getContextData();
List<TradeDataMarket> dataMarketList = contextData.getDatemarket();
if (CollectionUtils.isEmpty(dataMarketList)) {
dataMarketList = new ArrayList<>();
contextData.setDatemarket(dataMarketList);
}
dataMarketList.stream().filter(dm -> dm.getTradeid().equals(dataMarket.getTradeid())).findFirst()
.ifPresentOrElse(dm -> dm.setHedgingcostprofit(profit), () -> {
dataMarketList.add(new TradeDataMarket().setHedgingcostprofit(profit)
.setTradeid(dataMarket.getTradeid())
.setDatabatchno(AtsDateUtils.formatDate(AtsDateUtils.newDate(), DATE_FMT))
.setBusinesscode(dataMarket.getBusinesscode())
.setNotecode(dataMarket.getNotecode()));
});
}
```
阅读全文