+ 1
hashSet insertion problem
I want to iterate through the hashSet to obtain one element at a time and maybe do some arithmetic Operations on it. Without using the for each loop, is it possible? Please elaborate.
10 Antworten
+ 1
Check this out, hope it helps
https://www.geeksforgeeks.org/hashset-iterator-method-in-java/
+ 1
As Jayakrishna🇮🇳 says, The hashsets do not guarantee that the order will always be the same, if so, it would not make sense to do swap, it would be the same to do remove() and add()
Here is an example of swap in a linkedlist:
https://code.sololearn.com/cvYWBXU59mnZ/?ref=app
0
Even with the help of iterator, can I swap two numbers in an ArrayLists/ LinkedList/ HashSet
0
If yes, please explain
0
Why do you need swaping of elements in java? Any example pls..
You can insert and delete items from where ever you want..
And main difference between those is "hashSet does not maintain its insertion order while ArrayList and LinkeList maintains it's inserted order. so depends on requirement, you have to choose the better one..
0
I want to know how to perform generic operations on the hashSet elements. Like if i had stored a hashSet statement and I want to divide each of that element's data by the next one, would it be possible? Because as far as i understand, to alter just a little value in hashSet in general, i would require to get() that element and store it in a temp, remove it from the hashSet, do the operation on the temp and then add it back.
0
Won't it be a pretty cumbersome task?
0
Do make me understand if i understood something wrong 😅
0
So you are trying to sort hashSet...?
But since it does not maintain order, you can sort or swap values..
Even if you do, you csnnot get order which you looking...
https://www.google.com/amp/s/www.geeksforgeeks.org/how-to-sort-hashset-in-java/amp/