Support
Quality
Security
License
Reuse
Coming Soon for all Libraries!
Currently covering the most popular Java, JavaScript and Python libraries. See a SAMPLE HERE.
kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
A set of tools that keep Java sweet.
Maven
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.9</version>
</dependency>
Gradle
compile 'cn.hutool:hutool-all:5.5.9'
编译安装
./hutool.sh install
How to transform this piece of code using Java Stream
@Override
public Map<String, String[]> getParameterMap() {
Map<String, String[]> parameters = super.getParameterMap();
LinkedHashMap<String, String[]> map = new LinkedHashMap<>();
if (parameters != null) {
map = parameters.entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey,
v -> Arrays.stream(v.getValue())
.filter(val -> !StrUtil.hasEmpty(val))
.map(HtmlUtil::filter).collect(Collectors.toList()).toArray(new String[0]),
(x, y) -> y, LinkedHashMap::new
));
}
return map;
}
-----------------------
public Map<String, String[]> getParameterMap() {
Map<String, String[]> parameters = super.getParameterMap();
if (parameters != null) {
for (String[] values : parameters.values()) {
for (int i = 0; i < values.length; i++) {
String value = values[i];
if (!StrUtil.hasEmpty(value)) {
values[i] = HtmlUtil.filter(value);
}
}
}
}
return parameters;
}
maven dose not download dependencies in pom.xml
<packaging>pom</packaging>
QUESTION
How to transform this piece of code using Java Stream
Asked 2021-Sep-20 at 11:22In order to defend against XSS attacks, I wrote a class that extends from HttpServletRequestWrapper
and overrides the getParameterValues
method, the code is shown below:
@Override
public Map<String, String[]> getParameterMap() {
Map<String, String[]> parameters = super.getParameterMap();
LinkedHashMap<String, String[]> map = new LinkedHashMap<>();
if (parameters != null) {
for (String key : parameters.keySet()) {
String[] values = parameters.get(key);
for (int i = 0; i < values.length; i++) {
String value = values[i];
if (!StrUtil.hasEmpty(value)) {
value = HtmlUtil.filter(value);
}
values[i] = value;
}
map.put(key, values);
}
}
return map;
}
I'm wondering if this piece of code could be transformed using Java Stream because I see a lot of if
condition judgement and for
loop.
Methods:StrUtil.hasEmpty
and HtmlUtil.filter
are come from here: Hutool
Any suggestions to improve the performance of this code are welcome.
ANSWER
Answered 2021-Sep-20 at 07:29 @Override
public Map<String, String[]> getParameterMap() {
Map<String, String[]> parameters = super.getParameterMap();
LinkedHashMap<String, String[]> map = new LinkedHashMap<>();
if (parameters != null) {
map = parameters.entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey,
v -> Arrays.stream(v.getValue())
.filter(val -> !StrUtil.hasEmpty(val))
.map(HtmlUtil::filter).collect(Collectors.toList()).toArray(new String[0]),
(x, y) -> y, LinkedHashMap::new
));
}
return map;
}
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit