+ 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.

29th Dec 2021, 3:47 AM
Ruchika Sehgal
Ruchika Sehgal - avatar
10 Antworten
29th Dec 2021, 6:46 AM
CGM
CGM - avatar
+ 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
29th Dec 2021, 4:38 PM
CGM
CGM - avatar
0
Even with the help of iterator, can I swap two numbers in an ArrayLists/ LinkedList/ HashSet
29th Dec 2021, 10:12 AM
Ruchika Sehgal
Ruchika Sehgal - avatar
0
If yes, please explain
29th Dec 2021, 10:12 AM
Ruchika Sehgal
Ruchika Sehgal - avatar
29th Dec 2021, 10:13 AM
Ruchika Sehgal
Ruchika Sehgal - avatar
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..
29th Dec 2021, 4:22 PM
Jayakrishna 🇮🇳
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.
29th Dec 2021, 7:03 PM
Ruchika Sehgal
Ruchika Sehgal - avatar
0
Won't it be a pretty cumbersome task?
29th Dec 2021, 7:03 PM
Ruchika Sehgal
Ruchika Sehgal - avatar
0
Do make me understand if i understood something wrong 😅
29th Dec 2021, 7:04 PM
Ruchika Sehgal
Ruchika Sehgal - avatar
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/
29th Dec 2021, 8:05 PM
Jayakrishna 🇮🇳