Java 如何分离迭代器的元素?
我想随机生成时间和数字。。这里我用hashmap生成了一些记录。现在我可以生成数字,但我不能将它们分开。我必须将这些值分开,以便在数据库中设置这些值 这是我的密码Java 如何分离迭代器的元素?,java,arraylist,hashmap,Java,Arraylist,Hashmap,我想随机生成时间和数字。。这里我用hashmap生成了一些记录。现在我可以生成数字,但我不能将它们分开。我必须将这些值分开,以便在数据库中设置这些值 这是我的密码 public class DateTimePopulation { private Random rand = new Random(); private Date theDay; private String callDuration = null; private String endDay = null; SimpleDat
public class DateTimePopulation {
private Random rand = new Random();
private Date theDay;
private String callDuration = null;
private String endDay = null;
SimpleDateFormat mytimeFormat = new SimpleDateFormat("HH:mm:ss");
public static void main(String[] args) throws ParseException {
DateTimePopulation d = new DateTimePopulation();
for (int i = 1; i <= 3; i++) {
Map rec = d.getRecord();
for (int j = 0; j < rec.size(); j++) {
Collection c = rec.values();
Iterator itr = c.iterator();
int count=0;
while (itr.hasNext()) {
Object element=itr.next();
System.out.println(element);
}
System.out.println();
}
}
}
private Map getRecord() {
Map<String, Object> rec = new LinkedHashMap<String, Object>();
Date startDate;
try {
startDate = getRandStartDate();
rec.put("StartTime", startDate);
int start = 7200000, end = 0;
int duration = getRandDuration(start, end);
rec.put("Duration", duration);
Date endDate = getRandEndDate(startDate, duration);
rec.put("EndTime", endDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rec;
}
private Date getRandEndDate(Date startDate, int duration) {
Random r = new Random();
int ranSec = r.nextInt(duration - 0) + 0;
return new Date(startDate.getTime() + ranSec * 1000);
}
private int getRandDuration(int High, int Low) {
Random r = new Random();
return r.nextInt(High - Low) + Low;
}
private Date getRandStartDate() throws ParseException {
Date theDay = new SimpleDateFormat("yyyyMMdd hh:mm:ss")
.parse("20130101 00:00:00");
int High = 60 * 60 * 24 * 30 * 6;
Random r = new Random();
int Low = 10;
int R = r.nextInt(High - Low) + Low;
return new Date(theDay.getTime() + R * 1000);
}
}
尝试泛化此代码:
private Map getRecord() {
Map<String, Object> rec = new LinkedHashMap<String, Object>();
Date startDate;
try {
startDate = getRandStartDate();
rec.put("StartTime", startDate);
int start = 7200000, end = 0;
int duration = getRandDuration(start, end);
rec.put("Duration", duration);
Date endDate = getRandEndDate(startDate, duration);
rec.put("EndTime", endDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rec;
}
另见:
public class DateInterval {
private Date startDate;
private Date endDate;
private int duration;
public DateInterval(Date startDate, Date endDate, int duration) {
this.startDate = startDate;
this.endDate = endDate;
this.duration = duration;
}
public Date getStartDate() {
return startDate;
}
public Date getEndDate() {
return endDate;
}
public int getDuration() {
return duration;
}
}
我的意思是,你不需要地图来达到这个目的,使用你自己的实体。如果您需要在集合中保存所有实体,请使用Map with DB UUID作为键或列表。首先,您的设计从一开始就很奇怪-为什么要在实例上调用
getRecord()
,而它对您调用它的对象的字段没有任何作用
此外,当您在地图上迭代时,您实际上在同一地图上迭代了3次:
for (int j = 0; j < rec.size(); j++) {
Collection c = rec.values();
Iterator itr = c.iterator();
int count=0;
while (itr.hasNext()) {
Object element=itr.next();
System.out.println(element);
}
System.out.println();
}
基本上,我强烈建议您后退一步,重新审视您的整个设计。您可能会发现从头开始比更改现有代码更好。您所说的“分隔值”是什么意思?真的不清楚你想做什么。如果您试图将日期和时间拆分为其日期和时间组件,则这与迭代器无关。我希望将星期二1月8日11:01:57 IST 2013存储在stime中,将6074479存储在dur中,将星期五1月18日12:56:24 IST 2013存储在etime中。。。输出就像一组值,我必须将它们分开。。
public class DateInterval {
private Date startDate;
private Date endDate;
private int duration;
public DateInterval(Date startDate, Date endDate, int duration) {
this.startDate = startDate;
this.endDate = endDate;
this.duration = duration;
}
public Date getStartDate() {
return startDate;
}
public Date getEndDate() {
return endDate;
}
public int getDuration() {
return duration;
}
}
for (int j = 0; j < rec.size(); j++) {
Collection c = rec.values();
Iterator itr = c.iterator();
int count=0;
while (itr.hasNext()) {
Object element=itr.next();
System.out.println(element);
}
System.out.println();
}
System.out.println("Start: " + map.get("StartTime");
System.out.println("Duration: " + map.get("Duration");
System.out.println("End: " + map.get("EndTime");