Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:31:07+00:00 2026-06-11T15:31:07+00:00

Objective Create a class to be used as an immutable list of String objects.

  • 0

Objective

Create a class to be used as an immutable list of String objects.

Approach

I have decided to leverage Google Guava‘s ImmutableList<E> collection rather than wrapping a simple List<E> with Collections.unmodifiableList(List<? extends T> list) because I understand this avoids unnecessary concurrency checks on the backing List<E>, which is unaware of being wrapped (source: ImmutableCollectionsExplained).

Requirements

  • Class with be a "value-holder" to be used across threads
  • No code should be allowed to change the inner values after creation

Nice-to-haves

  • The class should implement Iterable<E> for iteration over the values in the order of creation
  • There should be only one class for a given set of String s.

Attempts

Here some attempts at it, although more combinations are possible. Forgive the humourous rendition.

Attempt #1 (including usage example)

import java.util.List;
import com.google.common.collect.ImmutableList;
class BritnetSpearsSpellings implements Iterable<String> {
  public static BritnetSpearsSpellings of(String... spellings) {
    BritnetSpearsSpellings britneySpears = new BritnetSpearsSpellings();
    britneySpears.spellings = ImmutableList.copyOf(spellings);
    return britneySpears;
  }
  private List<String> spellings;
  private BritnetSpearsSpellings() {
  }
  public List<String> getSpellings() {
    return spellings;
  }
}
@Override
public Iterator<String> iterator() {
  return spellings.iterator();
}
public class Usage {
  public static void main(String[] args) {
    for (String sepllin : BritnetSpearsSpellings.of("Brittany Spears", "Brittney Spears", "Britany Spears"))
      System.out.printf("You spel Britni like so: %s%n", sepllin);
    }
  }
}

Attempt #2

class BritnetSpearsSpellings implements Iterable<String> {
  public static BritnetSpearsSpellings of(String... spellings) {
    BritnetSpearsSpellings britneySpears = new BritnetSpearsSpellings();
    britneySpears.spellings = ImmutableList.copyOf(spellings);
    return britneySpears;
  }
  private ImmutableList<String> spellings;
  private BritnetSpearsSpellings() {
  }
  public ImmutableList<String> getSpellings() {
    return spellings;
  }
  @Override
  public Iterator<String> iterator() {
    return spellings.iterator();
  }
}

Attempt #3

class BritnetSpearsSpellings implements Iterable<String> {
  public static BritnetSpearsSpellings of(String... spellings) {
    BritnetSpearsSpellings britneySpears = new BritnetSpearsSpellings(ImmutableList.copyOf(spellings));
    return britneySpears;
  }
  private final ImmutableList<String> spellings;
  private BritnetSpearsSpellings(ImmutableList<String> spellings) {
    this.spellings = spellings;
  }
  public ImmutableList<String> getSpellings() {
    return spellings;
  }
  @Override
  public Iterator<String> iterator() {
    return spellings.iterator();
  }
}

Summary of differences

  • 1 keeps List<E> in the public interfaces, documenting immutability in the JavaDoc.

  • 2 stores and exposes everything as Google Guava‘s ImmutableList<E>

  • 3 keeps the internal reference as final at the expense of creating a specialised constructor, possibly making the static factory method initialisation look silly in the absence of other initialisation options (which are actually there in the real class)

Question

Please help me choose among one of these implementations, with your reasoning behind the choice.

I think the main drawback of approach #2 is that clients need to have cognizance/visibility of the specialised Google Guava type and probably they shouldn’t?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:31:09+00:00Added an answer on June 11, 2026 at 3:31 pm

    Clearly, #3 is the best choice in my opinion. final enforces and documents the immutability of (that field of) the class, not only the fact that the List itself is immutable.

    Whether you expose a List with some javadoc or an actual ImmutableList in the getter is another thing. I’ve seen opinions that returning an ImmutableList documents clearly the intention, but still, you’re then tying yourself to an implementation instead of an interface for something that’s low level and could need to change in the future (even if immutability is “generally” good). So it’s more a global design choice for your application than for just one use case. If you do use ImmutableList, I don’t think that’s a real problem for the clients, the name is explicit and it can easily be seen through your IDE that it’s an implementation of List, and they can access the javadoc if they want more information. And who knows, they might like it and start using it also, with all the other goodies Guava provides 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I create a singleton class in Objective C?
I have a peculiar problem. I created a normal Objective C class called SampleTable.
Someone on IRC told me I could create objective c objects directly from plist
The objective is to create indicators for a factor/string variable in a data frame.
In Objective-C I can create categories to extend a class. I can also add
How do I create a static variable in my Objective-C class? I'm familiar with
Suppose I’m making an Objective-C class that represents a fraction, and want to create
How to create & access static string in iPhone (objective c)? I declare static
I am new to iPhone development and Objective C. I have decided I would
I'm relatively new to the world of Objective-C and have a class that I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.