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 3458754
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:01:22+00:00 2026-05-18T10:01:22+00:00

I have a curious question, I hope someone can help me. I am using

  • 0

I have a curious question, I hope someone can help me.
I am using Java.

I receive a Set from a method. This TypeA is exactly what I want, but I would like to add some more functionalities to it, in order to increase code cleanness.

In C# I could write Extension Methods, but I don’t think Java has something like that, so I would like to create a new type, TypeB, that contains a TypeA variable and performs the extra function that I want (something like an Wrapper/Adapter). but the problem is:

I can’t do

Set<TypeB> mySet = myMethod();

or

Set<TypeB> mySet = (Set<TypeB>)myMethod();

What would be perfect would be something like that

Set<TypeB> mySet = new Set<TypeB>(myMethod());

But Set is a Java type, so I can’t add a constructor to it.

How could I do so I can parse it in a clean way?
If I add a method like

Set<TypeB> mySet = parse(myMethod());

I would have to iterate through all items in myMethod, which would not be very performatic.

Is there any better solution than creating this method?

Thanks,
Oscar

  • 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-05-18T10:01:22+00:00Added an answer on May 18, 2026 at 10:01 am

    What if you added functionality not by inheriting from TypeA, but by using functions (static methods):

    So instead of:

    public class TypeB {
        TypeA self;
        public void Frobnicate(int foo) {...}
    }
    

    Write:

    public class TypeAUtils {
        public static void Frobnicate(TypeA self, int foo) {...}
    }
    

    C# extension methods are just syntactic sugar for this technique anyway.

    This is one of my favorite design patterns. I call it “functions”, and it is highly underrated. 😛

    If you can’t do it this way (and thus you wouldn’t be able to with extension methods either), then you must be adding state to TypeA, in which case you have to allocate more memory for each object in your set, which means you can’t avoid iterating.

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

Sidebar

Related Questions

My question..not even sure if this is possible but curious from others... I have
I have a curious issue here In my ejbCreate() method from where i insert
Not sure if this question is related to software development, but I hope someone
I have a curious problem, I need to analyze a Java heap dump (from
I have a Javascript question that I am curious about. When using: var test
I have had this question niggling at the curious bit of my mind for
I have this curious question. I was looking at XML files and so I
This is a continuation question from a previous question I have asked I now
Curious question. I'm using Microsoft's Sandcastle to generate documentation from the XML comments in
I have a language-agnostic question about an algorithm. This comes from a (probably simple)

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.