如何先分组,然后使用Java流应用过滤?
示例:考虑此类Employee:我希望按部门分组,其中包含薪水大于2000的员工列表.
public class Employee {
private String department;
private Integer salary;
private String name;
//getter and setter
public Employee(String department, Integer salary, String name) {
this.department = department;
this.salary = salary;
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我如何做到这一点
List<Employee> list = new ArrayList<>();
list.add(new Employee("A", 5000, "A1"));
list.add(new Employee("B", 1000, "B1"));
list.add(new Employee("C", 6000, "C1"));
list.add(new Employee("C", 7000, "C2"));
Map<String, List<Employee>> collect = list.stream()
.filter(e -> e.getSalary() > 2000)
.collect(Collectors.groupingBy(Employee::getDepartment));
Run Code Online (Sandbox Code Playgroud)
产量
{A=[Employee [department=A, salary=5000, name=A1]],
C=[Employee [department=C, …Run Code Online (Sandbox Code Playgroud) 我想为我的Web应用程序实现跨站点请求防伪,这是基于struts 1.x框架.我知道struts 2框架为此提供了令牌拦截器,我可以使用过滤器实现类似的功能.
我很少有人认为1)我怎么能用简单的方式生成独特的令牌呢?(我可以为此目的使用Action类令牌,用于避免重复的表单提交)
使用struts 1.x框架令牌机制进行CSRF预防是否存在任何问题
考虑以下WorkExperience课程:
public class WorkExperience {
private int year;
private List<Skills> skill;
public WorkExperience(int year, List<Skills> skill) {
this.year = year;
this.skill = skill;
}
//getter setter
}
public class Skills {
private String skills;
public Skills(String skills) {
this.skills = skills;
}
@Override
public String toString() {
return "Skills [skills=" + skills + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
假设我希望按年份按照我的技能进行分组,这就是我们groupBy今年的表现:
public static void main(String[] args) {
List<Skills> skillSet1 = new ArrayList<>();
skillSet1.add(new Skills("Skill-1"));
skillSet1.add(new Skills("Skill-2"));
skillSet1.add(new Skills("Skill-3"));
List<Skills> skillSet2 …Run Code Online (Sandbox Code Playgroud) 如何使用 Spring Bootwebclient发布application/x-www-form-urlencoded
内容类型为“application/x-www-form-urlencoded”的示例 curl 请求的请求
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=XXXX' \
--data-urlencode 'password=XXXX'
Run Code Online (Sandbox Code Playgroud)
如何使用 webclient 发送相同的请求?
我有下面的字符串
String str="select * from m_menus;
select * from m_roles";
Run Code Online (Sandbox Code Playgroud)
我希望上面的字符串在一行中像
String str="select * from m_menus;select * from m_roles";
Run Code Online (Sandbox Code Playgroud)
我试过了
str1=str.replace("[\r\n]+", " ");
Run Code Online (Sandbox Code Playgroud)
并且
str1=str.replace("\n"," ");
Run Code Online (Sandbox Code Playgroud)
两者都不起作用.
我需要从中删除所有空/空值List<Optional<String>>.
例:
List<Optional<String>> list = new ArrayList<>();
list.add(Optional.empty());
list.add(Optional.of("Str1"));
list.add(Optional.of("Str2"));
list.add(Optional.of("Str3"));
list.add(Optional.of("Str4"));
list.add(Optional.of("Str5"));
list.add(Optional.empty());
list.add(Optional.ofNullable(null));
Run Code Online (Sandbox Code Playgroud)
目前,我正在使用以下方法之一:
方式1:
List<String> collect = list.stream()
.filter(Optional::isPresent)
.map(obj ->obj.get())
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
方式2:
List<Optional<String>> emptlist = new ArrayList<>();
emptlist.add(Optional.empty());
list.removeAll(emptlist);
Run Code Online (Sandbox Code Playgroud)
还有其他更好的方法吗?
我想比较2个LocalDateTime对象而不考虑纳秒.这就是我目前正在做的事情.这有什么更好的方法吗?
LocalDateTime object1 = LocalDateTime.of(2014, 3, 30, 12, 30, 23, 12000);
LocalDateTime object2 = LocalDateTime.of(2014, 3, 30, 12, 30, 23, 12004);
System.out.println(object1.isEqual(object2)); // false
LocalDateTime objec1tWithoutNano = object1.minusNanos(object1.getNano());
LocalDateTime objec2tWithoutNano = object2.minusNanos(object2.getNano());
System.out.println(objec1tWithoutNano.isEqual(objec2tWithoutNano)); // true
Run Code Online (Sandbox Code Playgroud) 如何使用Reduce操作在对象的两个字段上执行求和.
例如
class Pojo
{
public Pojo(int a, int b) {
super();
this.a = a;
this.b = b;
}
int a ;
int b;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
}
Pojo object1 = new Pojo(1, 1);
Pojo object2 = new Pojo(2, 2);
Pojo object3 = new Pojo(3, 3);
Pojo object4 = new Pojo(4, …Run Code Online (Sandbox Code Playgroud) 是否有任何可用的命令可以在当前的jshell会话中打印所有新创建的方法?类似/list但仅适用于方法的东西
我们可以向 提交两种类型的任务forkJoinPool。一个是RecursiveAction,另一个是RecursiveTask。
它们之间有什么区别?
java ×9
java-8 ×5
java-9 ×4
java-stream ×4
collectors ×1
cross-domain ×1
forkjoinpool ×1
java-11 ×1
jshell ×1
spring-boot ×1
struts ×1
struts-1 ×1