LinkedHashSet alternative in python

741 Views Asked by At
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

1

There are 1 best solutions below

0
Andres Sacco On BEST ANSWER

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