• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reshuffling Elements in List  RSS feed

 
Tiklu Mukherjee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a scenario where i want to accept an input from user, the input could be any combination of characters/integers. Now we have to Shuffle this input and use this Shuffled input for any particular use(Storing in DB/pass it as token value,etc) please find the below sample code, which is the work done by me to Shuffle the input.

Till this point it is good enough to some extent , but i am looking for a different approach,i tried with Collection.shuffle() API and things go good in all way,
now i also want to Reshuffle back the Shuffled output from Collection.Shuffle() into original Input String.
To be precised,Collection.Shuffle() ,helps me to Shuffle elements in a LIST ,is there any JAVA API which can we used to Reshuffle it back(Here i dont want to use the Direct UserInput to retrieve the Original String).

Thanks for showing me the correct Way of Doing this,
Sincerely,
Abhijit
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid , we do not have any such Java method to get your original String back from Re-shuffled output.
I think only way is that you have to save original data in your code

~abhay
 
Tiklu Mukherjee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Abhay,
On confirming that we don't have any such API to Re-Shuffle.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or rather than using Java Collection shuffle() method, you can write your own implementation (method) for shuffling the List with your known pattern.

For example- you can decide to shuffle List elements based on any fixed & known pattern - Interchange elements present next to each other .

So if Original List is [A,B,C,D,E,F,G,H] then
and Shuffled List is [B,A,D,C,F,E,H,G]

Here , I have interchanged elements present at index 0 and 1. Then I have interchanged elements present at index 3 and 4. So on and so forth.

In this way, when we want to get original string back, we apply inverse of this Pattern. So, to get original string, again interchange adjacent elements.
So , you shall get[A,B,C,D,E,F,G,H] String back.

I have demonstrated a very simple Pattern to shuffle.
You can come up with your own Random Pattern.

Hope this will be helpful for you.
~ abhay

 
Tiklu Mukherjee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Abhay,

This do help with the scenario, i have also designed an alternative approach for this.

~Abhijit(Tiklu Mukherjee)
abhijitmukherjee655@gmail.com
 
Tiklu Mukherjee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Developed an Approach for shuffling the user Input and reshuffling it back
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!