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

  • Home
  • SEARCH
  • 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 8850393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:53:33+00:00 2026-06-14T12:53:33+00:00

I did not find a direct answer to this specific question, so… Assume class:

  • 0

I did not find a direct answer to this specific question, so… Assume class:

class MyClass {
    private final Set<String> tags;

    MyClass(Set<String> initialTags) {
        this.tags = ???(initialtags);
    }
}

I just want a copy without caring about exact implementation of the set, with minimal overhead, so basically a direct copy of whatever internal state initialTags has. I can’t use clone, since Set is interface. Is this possible, or do I have to use something like new HashSet<String>(tags);, needing to make a decision about the type?

Assumptions: Set item type is resticted to immutable. I can trust the caller to not pass a broken initialTags Set implementation, and I can trust it to have good enough performance for any internal use in this class.

I’m thinking of trying reflection and clone(), but this seems a bit dirty…

  • 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-14T12:53:34+00:00Added an answer on June 14, 2026 at 12:53 pm

    Guava provides a method for that:

      /**
       * Returns an immutable set containing the given elements, in order. Repeated
       * occurrences of an element (according to {@link Object#equals}) after the
       * first are ignored. This method iterates over {@code elements} at most once.
       *
       * <p>Note that if {@code s} is a {@code Set<String>}, then {@code
       * ImmutableSet.copyOf(s)} returns an {@code ImmutableSet<String>} containing
       * each of the strings in {@code s}, while {@code ImmutableSet.of(s)} returns
       * a {@code ImmutableSet<Set<String>>} containing one element (the given set
       * itself).
       *
       * <p>Despite the method name, this method attempts to avoid actually copying
       * the data when it is safe to do so. The exact circumstances under which a
       * copy will or will not be performed are undocumented and subject to change.
       *
       * @throws NullPointerException if any of {@code elements} is null
       */
      public static <E> ImmutableSet<E> copyOf(Iterable<? extends E> elements) {
        return (elements instanceof Collection)
            ? copyOf(Collections2.cast(elements))
            : copyOf(elements.iterator());
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I did not find an answer to this question. I have a VOIP application.
I did not find the answer so i ask the question... Sorry to bother.
I did search the forum and did not find a similar question. I'm looking
There are several variant questions on this on SO, but I did not find
To my surprise I did not find the solution how to set up an
Searching did not find a similar question, so: How can POST data used by
I did do some googling and searching on this site but did not find
I have been trying to find an answer to this question for months, but
I've came accross this concept several times and did not find a clear explanation.
I did not find any suitable questions answered yet, so I'd like to know

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.