toList() vs collect(Collectors.toList())

I had some extra time this week so went through a bunch of Sonar findings. One was interesting – in Java 17 you can use .toList() instead of .collect(Collectors.toList()) on a stream.

[Yes, I know this was introduced in Java 16. I live in a world where only LTS releases matter]

Cool. I can fix a lot of these without thinking. It’s a search and replace on the project level after all. I then ran the JUnit regression tests and got failures. That was puzzling to me because I’ve been using .toList() in code I write for a good while without incident.

After looking into it, I found the problem. .toList() guarantees the returned List is immutable. However, Collectors.toList() makes no promises about immutability. The result might be immutable. Or you can change it freely. Surprise?

That’s according to the spec. On the JDK I’m using (and Jenkins is using), Collectors.toList() was returning an ArrayList. So people were treating the returned List as mutable and it was working. I added a bunch of “let’s make this explicitly mutable” and then I was able to commit.

Here’s an example that illustrates the diference

import java.util.*;
import java.util.stream.*;

public class PlayTest {

	public static void main(String[] args) {

		var list = List.of("a", "b", "c");
		var collectorReturned = collector(list);
		var toListReturned = toList(list);
		
		System.out.println(collectorReturned.getClass());  // ArrayList (but doesn't have to be)
		System.out.println(toListReturned.getClass());  // class java.util.ImmutableCollections$ListN
		
		collectorReturned.add("x");
		System.out.println(collectorReturned);  // [bb, cc, x]
		toListReturned.add("x");  // throws UnsupportedOperationException

	}

	private static List<String> toList(List<String> list) {
		return list.stream()
				.filter(s -> ! s.equals("a"))
				.map(s -> s + s)
				.toList();
	}

	private static List<String> collector(List<String> list) {
		return list.stream()
				.filter(s -> ! s.equals("a"))
				.map(s -> s + s)
				.collect(Collectors.toList());
				
	}

Collectors.toList() also makes no promises about serializablity or thread safety but I wasn’t expecting it to.

the problem with attending team/project meetings on vacation

I was recently discussing the impact of attending team/project meetings while on vacation with someone. Seemed like a good blog post.

Vacation is supposed to be about taking a mental and physical break. This means that tangential work things (taking a video course, obtaining a cert) etc don’t fall under the scope of this blog post. Those are career related but help you and not just your current employer. Similarly, work things that aren’t related to your current team/project also don’t fall within this scope. Whether one considers Toastmasters, employe networks, a town hall to be something they do on vacation, I feel like they fall in a different impact bucket that team/project meetings. (although some of the impacts overlap)

Personal impact – not recharging

It’s harder to recharge if working during your vacation. Plus vacation belongs to you. A Fast Company article includes the phrase “as simple as answering emails or as involved as taking meetings and creating deliverables”. So taking meetings (the subject of this blog post) is one of the more impactful things. The article lists the many personal downsides of working on vacation from burnout to being more creative.

I like that they use the word “disconnect.” It’s hard to disconnect while simultaneously joining a team/project meeting!

I know some people will say they don’t care about personal impact so my blog post is mostly about impact to others.

Team impact – coverage and opportunity

If a specific person doesn’t miss a meeting, you don’t know where coverage and skill gaps exist on the rest of the team. It’s easier to have the person who knows a topic the most answer or take notes or make a decision. Vacations are the most common way to address this. The person who is going to be on vacation during a meeting knows this in advance so has the opportunity to remind others what is important and what person X would bring up. It also lets people exercise the muscle of stepping up and not replying on person X. (Sick time accomplishes the same thing but isn’t often scheduled so adds stress to the person covering)

Team impact – feedback

When Person X returns, the others who attended the meeting, brief person X. This is an opportunity for feedback. Person X learns what happened. Everyone else gets more ideas of what they might have said. Even if a “decision” was made that was flat out wrong, it can be revisited now that person X has pointing out something critical that nobody else noted. (Obviously this doesn’t apply to contract negotiation.)

Team impact – learning to not experience everything

We can’t experience everything first hand. Vacation generally involves missing some meeting we wanted to attend. For example, I will be missing my team’s next retrospective and sprint review. I would like to be at those meetings. But I also trust my teammates to tell me what I need to know. So I know it’ll be ok to miss them.

Team impact – pressure to others

When one person “volunteers” to work on his/her time off, other’s on the team feel the pressure to do so. It doesn’t matter if that is the intent. It becomes a convention that (often newer or weaker) team members feel that working on vacation is now the floor of what is acceptable. And high impact things (team/project meetings and deliverables) are the worst because they are so visible. It’s not like anyone knows if someone skims (and doesn’t reply to) email.

Team impact – expectations

Similar to pressure to others, the pressure can exist on the other side. If it is seen that person x will attend team/project meetings on vacation, other meetings may be scheduled ignoring people’s schedules because of an expectation they will attend.

What about partial vacation days?

Where I work, we are allowed to take vacation in units of 2 hours. It’s interesting how this interacts with the “time away to recharge.” The answer is surprisingly well. I do take some longer vacations. But I also take a lot of 1-2 days or partial days. For example, I take 2-4 hours off to go to “Broadway in Bryant Park” in the summer. Or I’ll work 8:30-10:30am on a Friday morning before spending the rest of the day at the pool with my friends. The key is that I’m focused on what I’m doing and have 100% forgot about work on my vacation time. So even though it is short, my fun thing is my focus, not work. Taking 6 hours off and working 2 hours to attend a meeting is planning around the meeting which has the same problems listed in the above sections.

What about mandatory consecutive leave?

I haven’t worked in a place that has consecutive leave requirements. However, I am aware that some banks require a week or two in a row. My understanding is that it is about preventing shady things from happening. It’s hard to have an unofficial process in place if someone is out that long. I’m not a fan of mandatory consecutive leave because I take a lot of shorter vacation. But not being allowed to work certainly solves this problem!

What about management?

This article is about staff positions where there is a team. It’s different for managers, CEOs, owners, etc. What’s awkward is when these people attend meetings on vacation, they make others feel like others need to as well.