拆分字符串、对其进行平面映射并使用Java流创建对象?
我有两门课是这样的:拆分字符串、对其进行平面映射并使用Java流创建对象?,java,java-stream,flatmap,Java,Java Stream,Flatmap,我有两门课是这样的: public class Seat extends Moviefiable { private int number; private Hall hall; public Seat(int id, boolean active, int number, Hall hall) { super(id, active); this.number = number;
public class Seat extends Moviefiable {
private int number;
private Hall hall;
public Seat(int id, boolean active, int number, Hall hall) {
super(id, active);
this.number = number;
this.hall = hall;
}
public Seat() {this(-1, false, -1, null);}
public static Seat getById(int idSeat) throws SQLException {
Seat seat = SeatDAO.getById(idSeat);
return seat;
}
}
public class Ticket extends Moviefiable {
private Projection projection;
private Seat seat;
private LocalDateTime purchasingDate;
private User user;
public Ticket(int id, boolean active, Projection projection, Seat seat, LocalDateTime purchasingDate, User user) {
super(id, active);
this.projection = projection;
this.seat = seat;
this.purchasingDate = purchasingDate;
this.user = user;
}
public Ticket() {this(-1, false, null, null, null, null);}
String uri = request.getQueryString(); //uri looks like this: seats=2&seats=3
s -> s * 10
现在,我需要创建两个或多个票证
,具体取决于用户选择的票证数量。从我的JSP
页面,我将得到如下内容:
public class Seat extends Moviefiable {
private int number;
private Hall hall;
public Seat(int id, boolean active, int number, Hall hall) {
super(id, active);
this.number = number;
this.hall = hall;
}
public Seat() {this(-1, false, -1, null);}
public static Seat getById(int idSeat) throws SQLException {
Seat seat = SeatDAO.getById(idSeat);
return seat;
}
}
public class Ticket extends Moviefiable {
private Projection projection;
private Seat seat;
private LocalDateTime purchasingDate;
private User user;
public Ticket(int id, boolean active, Projection projection, Seat seat, LocalDateTime purchasingDate, User user) {
super(id, active);
this.projection = projection;
this.seat = seat;
this.purchasingDate = purchasingDate;
this.user = user;
}
public Ticket() {this(-1, false, null, null, null, null);}
String uri = request.getQueryString(); //uri looks like this: seats=2&seats=3
s -> s * 10
我想为两个Seat
对象创建两个Ticket
对象。在uri
字符串中,字符2和3是seats的主键
其思想是使用Java流来执行拆分和创建对象。这是我到目前为止所做的
ArrayList<Ticket> newTicketsForSeats = Stream.of(uri.split("&"))
.map(s -> s.split("seats=")[1])
//.flatMap(Arrays::stream)
.mapToInt(Integer::valueOf)
.mapToObj(s -> {
try {
return new Ticket(-1, true, projection, SeatDAO.getById(s),
LocalDateTime.now(), loggedInUser);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
})
.collect(Collectors.toCollection(ArrayList::new));
没关系。但当我运行上面的代码时,我得到:
java.lang.ArrayIndexOutOfBoundsException: 1
at servlets.ConfirmPurchaseServlet.lambda$0(ConfirmPurchaseServlet.java:62)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
projection
也是我获得的对象,这只是参考,它在servlet的其他地方<代码>日志用户我从会话中获得。我是Java流方面的新手,任何额外的解释都会非常好
我知道map()
需要IntUnaryOperator
lambda表达式,类似这样:
public class Seat extends Moviefiable {
private int number;
private Hall hall;
public Seat(int id, boolean active, int number, Hall hall) {
super(id, active);
this.number = number;
this.hall = hall;
}
public Seat() {this(-1, false, -1, null);}
public static Seat getById(int idSeat) throws SQLException {
Seat seat = SeatDAO.getById(idSeat);
return seat;
}
}
public class Ticket extends Moviefiable {
private Projection projection;
private Seat seat;
private LocalDateTime purchasingDate;
private User user;
public Ticket(int id, boolean active, Projection projection, Seat seat, LocalDateTime purchasingDate, User user) {
super(id, active);
this.projection = projection;
this.seat = seat;
this.purchasingDate = purchasingDate;
this.user = user;
}
public Ticket() {this(-1, false, null, null, null, null);}
String uri = request.getQueryString(); //uri looks like this: seats=2&seats=3
s -> s * 10
我不知道如何将int(座椅主键)映射到Seat
object
干杯。您的主要问题是试图将流输出分配给
数组列表Collectors.toList()
返回一个不能作为特定实现的列表
因此,要么这样做:
List<SomeClass> list = ...collect(Collectors.toList());
List List=…collect(collector.toList());
还是这个
ArrayList<SomeClass> list = ... collect(Collectors.toCollection(ArrayList::new));
ArrayList=。。。collect(Collectors.toCollection(ArrayList::new));
以下内容应该可以工作,但可能需要根据其他类进行一些调整
ArrayList<Ticket> newTicketsForSeats = Stream.of(uri.split("&"))
.map(s -> s.split("seats="))
.flatMap(Arrays::stream)
.mapToInt(Integer::valueOf)
.mapToObj(s -> new Ticket(-1, true, projection, SeatDAO.getById(s),
LocalDateTime.now(), loggedInUser))
.collect(Collectors.toCollection(ArrayList::new));
ArrayList newticketsforsets=Stream.of(uri.split(“&”))
.map(s->s.split(“seats=))
.flatMap(数组::流)
.mapToInt(整数::valueOf)
.mapToObj->新票证(-1,true,projection,SeatDAO.getById),
LocalDateTime.now(),loggedInUser))
.collect(收集器.toCollection(ArrayList::new));
这两种方法中的任何一种都可能有效:
List<Ticket> newTicketsForSeats = Stream.of(uri.split("&"))
.map(s -> s.replace("seats=", ""))
.mapToInt(Integer::valueOf)
.mapToObj(s -> new Ticket(-1, true, projection, SeatDAO.getById(s), LocalDateTime.now(), loggedInUser))
.collect(Collectors.toList());
我想您需要mapToObj
而不是map
…我将map
更改为mapToObj
,第二个mapToObj
报告错误:方法mapToObj((s)->{})对于类型stream是未定义的,也发布在这里:非常感谢您的重播。我编辑了我的原始问题。getById(int-idSeat)可能会抛出一个异常,所以Eclipse本身建议使用这个try-catch块,但是,不知怎么的,我得到了一些未检查的异常…谢谢Ron,在这里和Ranch论坛上的回复。互联网有时,看起来很奇怪,可能是一个小地方:)我编辑了我的答案。关于Ticket构造函数,我的类是这样的:Ticket(int-id,boolean-active,Projection-Projection,Seat-Seat,LocalDateTime-date-date,User-User)希望您能帮助我。顺便说一句,您肯定想要.mapToInt(Integer::parseInt)
而不是。mapToInt(Integer::valueOf)
,否则,您正在装箱到一个Integer
对象中,只需立即取消装箱到int
。如果您不关心装箱开销,请使用.map(Integer::valueOf)
…