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

livikmap99
  • 31
  • 2
  • Check this answer [Equivalent for LinkedHashSet for Python](https://stackoverflow.com/questions/653887/equivalent-for-linkedhashmap-in-python) – Andres Sacco Jan 07 '21 at 17:11
  • @AndresSacco can you explain this above code bcz i am new to java – livikmap99 Jan 07 '21 at 17:13
  • This looks like homework. Voting to close as it shows no attempt from author to solve it. – Karol Dowbecki Jan 07 '21 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. – Akash nitter Jan 07 '21 at 17:22

1 Answers1

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

Andres Sacco
  • 650
  • 3
  • 13