0
Iterator i1=h1.iterator();
while(i1.hasNext()){
int p=(int)(i1.next());
}

Please explain this code in python programming language term, here h1 is linkedhashset Thanks in advance

4
  • Check this answer Equivalent for LinkedHashSet for Python Commented Jan 7, 2021 at 17:11
  • @AndresSacco can you explain this above code bcz i am new to java Commented Jan 7, 2021 at 17:13
  • This looks like homework. Voting to close as it shows no attempt from author to solve it. Commented Jan 7, 2021 at 17:18
  • 2
    @KarolDowbecki I think you are not getting him. He already mentioned that he is new to java and want the explanation of the above code in python . So how he will show you that he attempt to understand the above code or not ? also the LinkedHashSet is difficult part for a beginner so there is nothing wrong in asking this question. Commented Jan 7, 2021 at 17:22

1 Answer 1

1

You have different ways to iterate a "LinkedHashSet", for example using: "forEach" or "iterate".

An iterator is an object that can be used to iterate all the elements in a collection. The Iterator has two important methods (hasNext, next).

I put an explanation about each line of your code

Iterator i1 = h1.iterator(); //obtain an iterator of the collection
while(i1.hasNext()) { // Continue iterating because has a next element 
  int p=(int)(i1.next()); //return the next element and parse it to "int"
}

Also with an Iterator you can add or remove elements.

Additional resources:

Java Iterator - Geeks for Geeks

Java official Documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.