iterate over a Hashset | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

iterate over a Hashset

Hello....!! could some one check the link below and explain this to me I do not understand why the input is / **popularLanguages.add("java") ; popularLanguages.add("c++") ; popularLanguages.add("c# ") ; popularLanguages.add("c") ; popularLanguages.add("kotlin") **/ and the output is c# python java java c++ instead of c++ as a simple array c c# Kotlin c python kotlim https://code.sololearn.com/csBVE3T2yGhK/?ref=app and how to get the same order in output as in imput when using a Set table

4th Feb 2020, 4:09 PM
jurise mboumene
jurise mboumene - avatar
3 Answers
+ 2
You can use LinkedHashSet to maintain elements in insertion order. You can use TreeSet to maintain the elements in sorted order.
4th Feb 2020, 4:35 PM
Avinesh
Avinesh - avatar
+ 2
i don't master my question well but the problem was about how to maintain insertion order when using a set and as you say on top i have use a LinkedHashSet and it work thans to all you
4th Feb 2020, 5:26 PM
jurise mboumene
jurise mboumene - avatar
0
String popularLanguage = popularLanguageIterator.next() ; // You can remove the above line and use the below to get the same result. System.out.println(popularLanguageIterator.next()); Also if you want to display it as an array or a list then you can do- System.out.println(popularLanguages.toString()); Edit: I don't think if I understood the question well but these may be a couple tricks you can use to make things easier.
4th Feb 2020, 4:22 PM
Avinesh
Avinesh - avatar