简体   繁体   English

如何分隔迭代器的元素?

[英]How to separate the elements of an iterator?

I want to randomly generate times and numbers.. Here I used hashmap generate some records. 我想随机生成时间和数字。在这里我用hashmap生成一些记录。 Now I can generate the numbers but I cant separate them. 现在,我可以生成数字了,但是我不能将它们分开。 I have to separate the values so that I can set those in database.. 我必须分开这些值,以便可以在数据库中进行设置。

Here is my code... 这是我的代码...

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

}

Here is the output. 这是输出。 I am showing 2 set of it. 我正在展示2套。 I have to separate the time, duration etc. 我必须分开时间,持续时间等

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

Firstly, your design is strange to start with - why are you calling getRecord() on an instance, when it doesn't do anything with the fields of the object you're calling it on? 首先,您的设计从一开始就很奇怪-为什么在实例上调用getRecord()要调用它的对象的字段不做任何事情?

Additionally, when you're iterating over a map, you're actually iterating over the same map 3 times: 此外,当您遍历地图时,实际上是遍历同一地图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();
}

Your outer loop is pointless here - you're never using j , after all. 您的外部循环在这里毫无意义-毕竟您永远不会使用j I would probably iterate over the entries rather than the values, if you really want to - then you can print out the key which goes with each value. 如果您确实想,我可能会遍历条目而不是值,然后可以打印出每个值都附带的键。

Next, I would encourage you not to use a map for this at all - create a separate type with fields for start time, duration and end time. 接下来,我建议您完全不要使用地图-使用开始时间,持续时间和结束时间的字段创建单独的类型。 That will fix things very simply. 这将非常简单地解决问题。

Next, if you still want to use a map, stop using the raw types - it'll make your life simpler. 接下来,如果您仍然想使用地图,请停止使用原始类型-这样会使您的生活更简单。

Finally, if you really still want to use a map, you already know the keys, so there's no point in iterating over it: 最后,如果您真的仍然想使用地图,那么您已经知道键,因此没有必要遍历它:

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

Basically, I strongly suggest that you take a step back and revisit your whole design. 基本上,我强烈建议您退后一步,重新审视整个设计。 You may well find it's better to start from scratch than to change your existing code. 您可能会发现,从头开始比更改现有代码更好。

Try to generify this code: 尝试生成以下代码:

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

Something like that: 像这样:

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

See also: 也可以看看:

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

I mean, you don't need Map for this purpose, use the your own entity. 我的意思是,您不需要Map即可使用您自己的实体。 If you need to hold all your entities at Collection, use Map with DB UUIDs as a keys or List. 如果需要将所有实体都保存在Collection中,请使用带有DB UUID的Map作为键或List。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM