gpt4 book ai didi

java - 组合类中的属性

转载 作者:行者123 更新时间:2023-12-02 08:45:31 25 4
gpt4 key购买 nike

我有以下自定义类

class rollingD {
private String settDate;
private String publishingPeriodCommencingTime;
private String fuelTypeGeneration;

public String getSettDate() {
return settDate;
}

public void setSettDate(String settDate) {
this.settDate = settDate;
}

public String getPublishingPeriodCommencingTime() {
return publishingPeriodCommencingTime;
}

public void setPublishingPeriodCommencingTime(String publishingPeriodCommencingTime) {
this.publishingPeriodCommencingTime = publishingPeriodCommencingTime ;
}

public String getFuelTypeGeneration() {
return fuelTypeGeneration;
}

public void setFuelTypeGeneration(String fuelTypeGeneration) {
this.fuelTypeGeneration = fuelTypeGeneration;
}

@Override
public String toString() {
return String.format("%s,%s,%s",
settDate,
publishingPeriodCommencingTime,
fuelTypeGeneration);



}}

这些项目添加到我的数组列表中,如下所示

ArrayList<rollingD> aList = new ArrayList<>();

属性

settDate
and
publishingPeriodCommencingTime

是独立的实体

01/04/2020
and
21:34:26

我想做的是在我的类中,我想在将属性添加到数组列表之前组合它们

所以我最终会得到

01/04/2020 21:34:26  

作为单个属性

因此,我的数组列表中只有 2 个属性,然后我可以对其进行排序等

有人可以提供帮助吗?

我尝试过之后流式传输 map 并合并流,但我意识到这不是一个理想的解决方案,因为我需要进一步使用数组列表,理想情况下使用日期时间戳和值作为我唯一的两个项目

这是解析API的代码

      while(parser.hasNext()) {
XMLEvent event = parser.nextEvent();

switch(event.getEventType()) {

case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();

if (qName.equalsIgnoreCase("settDate")) {
rD = new rollingD(null,null,null);
bMarks = true;


} else if (qName.equalsIgnoreCase("publishingPeriodCommencingTime")) {
bLastName = true;

} else if (qName.equalsIgnoreCase("fuelTypeGeneration")) {
bNickName = true;
aList.add(rD);

}

break;

case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
if(bMarks) {

// System.out.println("settDate: " + characters.getData());
rD.getTimeStamp(characters.getData(),null,null);
bMarks = false;

}
if(bLastName) {
// System.out.println("publishingPeriodCommencingTime: " + characters.getData());
rD.getTimeStamp(null,characters.getData(),null);

bLastName = false;
}
if(bNickName) {
// System.out.println("fuelTypeGeneration: " + characters.getData());
rD.getTimeStamp(null,null,characters.getData());
bNickName = false;
}



break;

case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();





//List<String> namesList = aList.stream()
//.map(x->x.getSettDate()+" "+ x.getPublishingPeriodCommencingTime()+","+ x.getFuelTypeGeneration())
//.collect(Collectors.toList());

//Collections.sort(namesList);
//
//for (String name : namesList) {
//System.out.println(name);
//}








if(endElement.getName().getLocalPart().equalsIgnoreCase("item")) {

System.out.println();
}
break;
}
}

最佳答案

按如下方式进行:

class RollingD {
private String settDate;
private String publishingPeriodCommencingTime;
private String fuelTypeGeneration;
private String timeStamp;

public RollingD(String settDate, String publishingPeriodCommencingTime, String fuelTypeGeneration) {
this.settDate = settDate;
this.publishingPeriodCommencingTime = publishingPeriodCommencingTime;
this.fuelTypeGeneration = fuelTypeGeneration;
timeStamp = settDate != null && publishingPeriodCommencingTime != null && !settDate.isEmpty()
&& !publishingPeriodCommencingTime.isEmpty() ? settDate + " " + publishingPeriodCommencingTime : "";
}

public String getTimeStamp() {
return settDate != null && publishingPeriodCommencingTime != null && !settDate.isEmpty()
&& !publishingPeriodCommencingTime.isEmpty() ? settDate + " " + publishingPeriodCommencingTime : "";

}

// ..Do not create any setter for timeStamp
// ..Other getters and setters

@Override
public String toString() {
return "RollingD [settDate=" + settDate + ", publishingPeriodCommencingTime=" + publishingPeriodCommencingTime
+ ", fuelTypeGeneration=" + fuelTypeGeneration + ", timeStamp=" + timeStamp + "]";
}
}

演示:

public class Main {
public static void main(String[] args) {
RollingD rd1 = new RollingD("", "", "G");
System.out.println(rd1);
RollingD rd2 = new RollingD("01/04/2020", "", "G");
System.out.println(rd2);
RollingD rd3 = new RollingD("", "21:34:26", "G");
System.out.println(rd3);
RollingD rd4 = new RollingD("01/04/2020", "21:34:26", "G");
System.out.println(rd4);
}
}

输出:

RollingD [settDate=, publishingPeriodCommencingTime=, fuelTypeGeneration=G, timeStamp=]
RollingD [settDate=01/04/2020, publishingPeriodCommencingTime=, fuelTypeGeneration=G, timeStamp=]
RollingD [settDate=, publishingPeriodCommencingTime=21:34:26, fuelTypeGeneration=G, timeStamp=]
RollingD [settDate=01/04/2020, publishingPeriodCommencingTime=21:34:26, fuelTypeGeneration=G, timeStamp=01/04/2020 21:34:26]

关于java - 组合类中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61110742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com