2

I have a List of Objects called md. Each of this objects has an activityName, a startTime and an endTime(for the activity). I want to iterate over this list and for each activity, get the startTime and endTime.

Map<String,Long> m1 = new HashMap<String,Long>();
        m1 = md
                .stream()
                .map(s->s.activityName)
                .collect(HashMap<String,Long>::new,
                    (map,string)->{
                        String d1 = md.get(md.indexOf(string)).startTime;
                        String d2 = md.get(md.indexOf(string)).endTime;
                           .
                           .
                           .
                 },HashMap<String,Long>::putAll);

It gives me java.lang.ArrayIndexOutOfBoundsException: -1 when I try to get the index of string String d1 = md.get(md.indexOf(string)).startTime;

Is there any other way to simplify the code using Lambda expressions?

What if I have two activities with the same name (Drinking for ex).Will it only return the index of the first Drinking activity it finds?

9
  • 1
    It's unclear what the result should be. But since your list is a List<SomeObject>, calling indexOf(activityName) on in will always return -1. The String activityName is not equal to any of the SomeObject instances stored in the list. Your strategy s also extremely inefficient, since it needs to find the object in the list at each iteration, although the stream precisely iterates on these objects. Commented May 23, 2017 at 20:39
  • it is interesting, but if I want to print the activityName of the string , it displays it right. System.out.println(string), displays " Sleeping" , but still, it says that it`s index is -1. Commented May 23, 2017 at 20:40
  • of course it does. after the map operation your Stream is Stream<String> and you are printing this string... Commented May 23, 2017 at 20:43
  • I got it. But how can I fix my problem? What`s the right way to iterrate the List? Commented May 23, 2017 at 20:45
  • its not clear what you actually what to achieve - a more clear example would help. btw you can tag people in your comments via @, like @Eugene. Commented May 23, 2017 at 20:47

1 Answer 1

2

It seems that you are missing that fact that once you do:

md.stream().map(s -> s.activityName)

your Stream has become Stream<String>; while your md is still List<YourObject>

And in the map operation you are trying to find a String inside md, this obviously does not exist, thus a -1.

So you need a Map<String, Long> that is activitaName -> duration it takes(could be Date/Long)

 md.stream()
   .collect(Collectors.toMap(s -> s.activityName, x -> {
       Date start = // parse s.startTime
       Date end = // parse s.endTime
       return end.minus(start);
    }));

Now the parsing depends on the dates you use.

Sign up to request clarification or add additional context in comments.

2 Comments

Eugene, please see my comment in the question, you still need to manage collisions, because there can be more than one object in the list with the same activity name.
@FedericoPeraltaSchaffner thx... let's close this question then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.