通过在java中放入列表来设置和获取数据循环
我有一个for循环,在这个循环中,我可以将数据发送到不同类中的其他方法 UserManagerImpl.java通过在java中放入列表来设置和获取数据循环,java,Java,我有一个for循环,在这个循环中,我可以将数据发送到不同类中的其他方法 UserManagerImpl.java for (User user: userList) { surveyModel survey = new SurveyModel(); survey.setSurveyData(Id, comment, serveyScore, grade); } 在另一个类中,我设置和get来创建数据列表,然
for (User user: userList) {
surveyModel survey = new SurveyModel();
survey.setSurveyData(Id, comment, serveyScore, grade);
}
在另一个类中,我设置和get来创建数据列表,然后希望通过get方法获取它
surveyModel.java
public class SurveySentimentModel {
public static String delimiter = "|||";
private List scores = new ArrayList();
private List negativeData = new ArrayList();
private List PositiveData = new ArrayList();
public void setSurveyData(String Id, String comment, double Score, String grade) {
//want to add score to scores list
//if grade positive add to positive list or else negative after delimiting
}
public double getTotalScore() {
//calculate the sum of scores
return totalScore;
}
public String getTotalSentimentgrade() {
if (totalScore> 0) {
return "Positive";
}
return "Negative";
}
public List getSurveyData() {
//Want to merge list - first negative and then positive
return new ArrayList();
}
}
private String grade;
private Number Score;
private String Id;
private String comment;
public SurveyModel(String Id, String comment, double Score, String grade) {
this.grade= grade;
this.Score= Score;
this.comment = comment;
this.Id = Id;
}
public SurveyModel() {
// TODO Auto-generated constructor stub
}
SurveyModel.java
public class SurveySentimentModel {
public static String delimiter = "|||";
private List scores = new ArrayList();
private List negativeData = new ArrayList();
private List PositiveData = new ArrayList();
public void setSurveyData(String Id, String comment, double Score, String grade) {
//want to add score to scores list
//if grade positive add to positive list or else negative after delimiting
}
public double getTotalScore() {
//calculate the sum of scores
return totalScore;
}
public String getTotalSentimentgrade() {
if (totalScore> 0) {
return "Positive";
}
return "Negative";
}
public List getSurveyData() {
//Want to merge list - first negative and then positive
return new ArrayList();
}
}
private String grade;
private Number Score;
private String Id;
private String comment;
public SurveyModel(String Id, String comment, double Score, String grade) {
this.grade= grade;
this.Score= Score;
this.comment = comment;
this.Id = Id;
}
public SurveyModel() {
// TODO Auto-generated constructor stub
}
//接球手和接球手
在这里我想
1.) Add score to scores list
2.) want to add graddes to list by negative first with delimiter then positive.
3.) want to get total score.
我怎样才能达到我的要求。我是java新手,请在这方面帮助我。这里有一个建议:
这是模型类:
public class SurveySentimentModel {
public static String delimiter = "|||";
private List<SurveyModel> scores = new ArrayList();
private List<SurveyModel> negativeData = new ArrayList();
private List<SurveyModel> positiveData = new ArrayList();
public void setSurveyData(String Id, String comment, double score, String grade) {
SurveyModel survey = new SurveyModel(id, comment, score, grade );
scores.add(survey)
if(score >= 0){
positiveData.add(survey);
}else{
negativeData.add(survey);
}
}
public double getTotalScore() {
double sum = 0;
for(SurveyModel s: scores){
sum += s.getScore();
}
return sum;
}
public List getSurveyData() {
List<SurveyModel> joined = new ArrayList(negativeData);
joined.addAll(positiveData)
return joined;
}
}
请告诉我们你试过什么。@Bill F给定的布局思想,输入查询已完成。但我不知道如何通过将其放入列表中来实现。以下三件事我没有按照您的建议得到:1.我无法使用scores.stream.mapToDoubleSurveyModel::getScore.sum;它显示了这一行中的错误我正在使用java 7。2.我无法在getsurveyData中获取作为返回的列表,因为addallpositiveData,这迫使它将返回类型更改为boolean。3.我想第一次在positiveData列表之前添加分隔符。stream是java 8+我将其更改为与java 7兼容的循环。“我不明白您在positiveData之前使用分隔符是什么意思。@Raphael。我又添加了一个arraylist,我正在合并三个列表,在getsurveyData中,通过这个列表MergedSurveyData=new arraylist;mergedSurveyDataList.addAllnegativeData;mergedSurveyDataList.addAllnuetralData;mergedSurveyDataList.addAllpositiveData;但每次列表大小都会增加0倍。如何仅获取唯一的列表元素