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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:49:56+00:00 2026-06-09T05:49:56+00:00

In Java, I have: Set<Integer> set = new HashSet<Integer>(); callVoidMethod(set); … public static void

  • 0

In Java, I have:

Set<Integer> set = new HashSet<Integer>();
callVoidMethod(set);
...
public static void callVoidMethod(Set<Integer> set) {

    Set<Integer> superset = new HashSet<Integer>(set);
    ...
    // I just added this loop to show that I'm adding quite a lot
    // well, it depends on conditions, sometimes I add nothing,
    // but it is unpredictable and do not know if add something
    for (int i = 0; i < 1000; i++) {
         ...
         if (conditionSatisfied) superset.add(someValue);
         ...
    }

}

The code above is simplified, the idea is to pass the set by reference into a void method and create a full copy of a set such that we will be able to add some new elements to the copy (superset here) and do not touch the set as we need it untouched when we exit the void method.

My code works with lots of data processing and if there is no faster way to make a copy, then I would like to optimize the HashSet itself, for instance I do not need Integers as keys, but better primitive ints. Would be a good idea to implement an int[] array of keys in the MyHashSet?

If so is possible, I would be interested in using the same idea for improving this:

Map<Integer, ArrayList<Item>> map = new HashMap<Integer, ArrayList<Item>>();

EDIT: I need only speed-performance-optimization. I do not need beautiful-maintainable code and memory.

  • 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-09T05:49:57+00:00Added an answer on June 9, 2026 at 5:49 am

    What do you do with these objects later? If you’re just doing lookups or something like that, it might be faster to keep them separate and check both, rather than making a full copy. So,

    public static void callVoidMethod(Set<Integer> set) {
    
        Set<Integer> superset = new HashSet<Integer>();
        ...
        if (conditionSatisfied) superset.add(someValue);
    
        ...
        if(set.contains(value) || superset.contains(value))
            doSomething();
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have a class with a constructor like this: public FilterList(Set<Integer> labels) {
I have this: import java.util.ArrayList; public class MyClass { ArrayList list = new ArrayList();
I have a java.util.Set<City> cities and I need to add cities to this set
I have the following code snippet: class Cert { public static void main(String[] args)
So basically I have this constructor for the class League : import java.util.*; public
I have set my java min/max heap size to be the same as outlined
I have set the eclipse java formatter to wrap lines that exceed 120 characters
I have set up a remote Mercurial (Hg) repository that holds a large Java
I have java script code to set some of the properties of ajax controls.
I have a set of Java 5 source files with old-style Doclet tags, comments

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.