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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:43:09+00:00 2026-05-31T23:43:09+00:00

I have a piece of code like this: HashSet<Object> Set = new HashSet<Object>(); //add

  • 0

I have a piece of code like this:

HashSet<Object> Set = new HashSet<Object>();
//add elements to set 'Set'
Object o = null;
for (Iterator<Object> itr=Set.iterator();itr.hasNext();) {
    o = itr.next();
    //doing some processing on o
}

I assumed that Object o will point to the Object pointed to by itr. But it is not working as expected. The attributes pointed to by itr.next() is not being copied to o.
Can anybody suggest where I am going wrong? And also, is there some useful post on object assignment like

o1 = o2

and what happens at memory level in Java?

Below is my actual code:

What I am doing: I have created a set TSet of elements of type Types.AdjList and now I want to process each successive pair of elements of type Types.AdjList and have assigned iterator value during each iteration to two Types.AdjList variables T1 and T2. But T1 and T2 attributes are not matching what the iterator is having.

P.S. Types.AdjList is a HashMap

for (int i=0; i<numT; i++) {
        size = generator.nextInt(10)+1;
        T[i] = new Types().new AdjList(size);
}
HashSet<Types.AdjList> TSet = new HashSet<Types.AdjList>(); 
    for (int i=0; i<T.length; i++) {
        TSet.add(T[i]);
    }
Types.AdjList T1 = null, T2 = null;

for (Iterator<Types.AdjList> itr = TSet.iterator(); itr.hasNext();) {
        T1 = itr.next();
        if (T2 != null) {
            size1 = T1.adj.size();
            //size1 is returning 0, though T1.adj has some elements
            size2 = T2.adj.size();
            //do some processing on T1, T2 based on size1 and size2         
        }
        T2 = T1;
}

Any help will be appreciated.

Thanks,

Somnath

  • 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-31T23:43:10+00:00Added an answer on May 31, 2026 at 11:43 pm

    There is nothing wrong with your code, but it could use some cleaning up.

    If you apply these “best practice” coding standards:

    • Always declare the abstract type (ie Set not HashSet)
    • Use leading-lowercase names for variables (set not Set)
    • Use “foreach” syntax where possible (dispense with using iterators directly)
    • Declare variables so they have the smallest scope possible (o lived past its use)

    you get your code refactored to:

    for (Object o : new HashSet<Object>(someSet)) {
        // doing some processing on o
    }
    

    or even

    for (Object o : someSet) { // Not sure why you wanted to make a new Set
        // doing some processing on o
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of code like this: set<string>::iterator it1; set<string>::iterator it2; pair<set<string>::iterator,bool> ret;
I have a piece of code that looks like this: Dir.new(path).each do |entry| puts
I have a piece of code that looks like this: Algorithm a = null;
I have a piece of code that goes like this File file = null;
OK, I have this piece of code, var record = new Object(); //var aData
I have a piece of code like this: Class Program { static StreamReader sr
I have a piece of code like so... FileInputStream fi = new FileInputStream(filein); GZIPInputStream
I have a piece of code like this - <script> var x,m,cloneNodex; function addVtier()
I have a piece of code like this class Base { public: Base(bool _active)
I have a piece of javascript code like this right now. document.getElementById(pos).style.backgroundColor='#800080'; I want

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.