如何分隔迭代器的元素

如何分隔迭代器的元素

我想随机生成时间和数字。在这里我用hashmap生成一些记录。现在,我可以生成数字了,但是我不能将它们分开。我必须分开这些值,以便可以在数据库中进行设置。

这是我的代码...

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);
}

}


这是输出。我正在展示2套。我必须分开时间,持续时间等

Tue Jan 08 11:01:57 IST 2013
6074479
Fri Jan 18 12:56:24 IST 2013

最佳答案

首先,您的设计从一开始就很奇怪-为什么在实例上调用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();
}


您的外部循环在这里毫无意义-毕竟您永远不会使用j。如果您确实想,我可能会遍历条目而不是值,然后可以打印出每个值都附带的键。

接下来,我建议您完全不要使用地图-使用开始时间,持续时间和结束时间的字段创建单独的类型。这将非常简单地解决问题。

接下来,如果您仍然想使用地图,请停止使用原始类型-这样会使您的生活更简单。

最后,如果您真的仍然想使用地图,那么您已经知道键,因此没有必要遍历它:

System.out.println("Start: " + map.get("StartTime");
System.out.println("Duration: " + map.get("Duration");
System.out.println("End: " + map.get("EndTime");


基本上,我强烈建议您退后一步,重新审视整个设计。您可能会发现,从头开始比更改现有代码更好。

关于java - 如何分隔迭代器的元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17359987/

10-11 22:43