在一个类中合并属性。

huangapple go评论74阅读模式
英文:

Combining attributes in a class

问题

以下是翻译好的内容:

我有以下自定义类

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
publishingPeriodCommencingTime

是单独的实体

01/04/2020
21:34:26

我想要做的是在我的类内部将属性组合在一起然后再添加到数组列表中

所以我最终会得到

01/04/2020 21:34:26  

作为单个属性

因此我在数组列表中只会有2个属性然后我可以对它们进行排序等操作

有人可以提供帮助吗

我尝试过之后流式映射然后在流合并后但我意识到这不是一个理想的解决方案因为我需要在后面进一步处理数组列表并且理想情况下只有datetimestamp和值是我的两个项目

这是解析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;
} 
}  

请注意,上述内容是您提供的原始代码的翻译版本,保留了代码的结构和格式。如果您有任何问题或需要进一步的帮助,请随时提问。

英文:

I have the following custom class

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(&quot;%s,%s,%s&quot;,
settDate,
publishingPeriodCommencingTime,
fuelTypeGeneration);
}} 

The items add to my array list as below

ArrayList&lt;rollingD&gt; aList = new ArrayList&lt;&gt;();

The attributes

settDate
and 
publishingPeriodCommencingTime

are separate entities

01/04/2020
and 
21:34:26

What i would like to do is within my class i want to combine my attributes before adding them to the array list

so i would end up with

01/04/2020 21:34:26  

as a single attribute

I would therefore only have 2 attributes in my arraylist which i can then sort etc

Can someone provide assistance please.

I have tried streaming a map afterwards and combining the streams after but i realised that is not an ideal solution as i need to then work with the arraylist further down the line and ideally with datetimestamp and the value as my only two items

This is the code for parsing the 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(&quot;settDate&quot;)) {
rD = new rollingD(null,null,null);
bMarks = true;
} else if (qName.equalsIgnoreCase(&quot;publishingPeriodCommencingTime&quot;)) {
bLastName = true;
} else if (qName.equalsIgnoreCase(&quot;fuelTypeGeneration&quot;)) {
bNickName = true;
aList.add(rD);
}
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
if(bMarks) {
//  System.out.println(&quot;settDate: &quot; + characters.getData());
rD.getTimeStamp(characters.getData(),null,null);
bMarks = false;
}
if(bLastName) {
//  System.out.println(&quot;publishingPeriodCommencingTime: &quot; + characters.getData());
rD.getTimeStamp(null,characters.getData(),null);
bLastName = false;
}
if(bNickName) {
// System.out.println(&quot;fuelTypeGeneration: &quot; + characters.getData());
rD.getTimeStamp(null,null,characters.getData());
bNickName = false;
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
//List&lt;String&gt; namesList = aList.stream()
//.map(x-&gt;x.getSettDate()+&quot; &quot;+ x.getPublishingPeriodCommencingTime()+&quot;,&quot;+ x.getFuelTypeGeneration())
//.collect(Collectors.toList());
//Collections.sort(namesList);
//
//for (String name : namesList) {
//System.out.println(name);
//}
if(endElement.getName().getLocalPart().equalsIgnoreCase(&quot;item&quot;)) {
System.out.println();
}
break;
} 
}  

答案1

得分: 1

请按以下方式进行操作

    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 : "";

        }

        // ..不要为timeStamp创建任何setter方法
        // ..其他的getter和setter方法

        @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]
英文:

Do it as follows:

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 &amp;&amp; publishingPeriodCommencingTime != null &amp;&amp; !settDate.isEmpty()
&amp;&amp; !publishingPeriodCommencingTime.isEmpty() ? settDate + &quot; &quot; + publishingPeriodCommencingTime : &quot;&quot;;
}
public String getTimeStamp() {
return settDate != null &amp;&amp; publishingPeriodCommencingTime != null &amp;&amp; !settDate.isEmpty()
&amp;&amp; !publishingPeriodCommencingTime.isEmpty() ? settDate + &quot; &quot; + publishingPeriodCommencingTime : &quot;&quot;;
}
// ..Do not create any setter for timeStamp
// ..Other getters and setters
@Override
public String toString() {
return &quot;RollingD [settDate=&quot; + settDate + &quot;, publishingPeriodCommencingTime=&quot; + publishingPeriodCommencingTime
+ &quot;, fuelTypeGeneration=&quot; + fuelTypeGeneration + &quot;, timeStamp=&quot; + timeStamp + &quot;]&quot;;
}
}

Demo:

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

Output:

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]

答案2

得分: 0

以下是翻译好的内容:

你的方法存在一些误解。
不需要在基本对象内部(或添加到之前)进行任何参数的组合。
只需像您对待列表数据结构中的对象一样进行添加。
您的问题可能是:如何进一步处理列表,基于存储对象的属性。(例如:根据名称、日期小时等排序)
在这里,我强烈建议查阅列表方法的功能。(使用比较器等)

英文:

The are some miss-concepts on your approach.
<br>Do not need to do any combinations of parameters inside base object(or before adding to).
<br>Just added as you did the objects on List Data Structure
<br>Your issues is maybe: How to process further the list, based on stored-objects
properties.(eg: sorting based on names, date_hours,etc)
<br>Here I strongly advise to search on List methods capabilities.(using a comparator, etc)

huangapple
  • 本文由 发表于 2020年4月9日 06:02:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61110742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定