如何分离迭代器的元素?

时间:2013-06-28 08:01:02

标签: java arraylist hashmap

我想随机生成时间和数字..这里我使用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   

2 个答案:

答案 0 :(得分:1)

首先,你的设计开始时很奇怪 - 为什么你在一个实例上调用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");

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

答案 1 :(得分:0)

尝试生成此代码:

 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 DateInterval getRecord() {
    DateInterval interval = null;
    try {
        Date startDate = getRandStartDate();
        int start = 7200000, end = 0;
        int duration = getRandDuration(start, end);
        Date endDate = getRandEndDate(startDate, duration);

        interval = new DateInterval(startDate, endDate, duration);


    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return interval;
}

另见:

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,请使用您自己的实体。如果您需要在Collection中保存所有实体,请使用Map with DB UUID作为键或List。

相关问题