{"id":8660,"date":"2012-09-25T21:41:03","date_gmt":"2012-09-25T16:11:03","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8660"},"modified":"2012-09-25T21:49:49","modified_gmt":"2012-09-25T16:19:49","slug":"changing-the-order-of-the-elements-of-list-in-random-manner","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/changing-the-order-of-the-elements-of-list-in-random-manner\/","title":{"rendered":"Changing the order of the elements of List in random manner"},"content":{"rendered":"<p>I found a very interesting method in java to change the order of elements in a list randomly, also called shuffling of elements in list.<br \/>\n<br \/>\nActually I got the requirement to show a random product details out of 5 products, each time user refreshes the page. I came out with the solution using <strong>Collections.shuffle(&lt;list&gt;)<\/strong><br \/>\n<br \/>\nFor understanding, lets assume, we have Product class. Each product has a name and its corresponding price.<\/p>\n<p>[java]<br \/>\nclass Product{<br \/>\n   String name<br \/>\n   Float price<br \/>\n }<\/p>\n<p>\/\/ Six different products<br \/>\nProduct product1 = new Product(name:&quot;TV&quot;,price:30000)<br \/>\nProduct product2 = new Product(name:&quot;Refrigerator&quot;,price:20000)<br \/>\nProduct product3 = new Product(name:&quot;Radio&quot;,price:2000)<br \/>\nProduct product4 = new Product(name:&quot;Washing Machine&quot;,price:20000)<br \/>\nProduct product5 = new Product(name:&quot;Fan&quot;,price:1000)<br \/>\nProduct product6 = new Product(name:&quot;AC&quot;,price:20000)<\/p>\n<p>\/\/List of above products<br \/>\nList products = [product1,product2,product3,product4,product5,product6]<\/p>\n<p>println products*.name<br \/>\n\/\/Output : [TV, Refrigerator, Radio, Washing Machine, Fan, AC]<\/p>\n<p>Collections.shuffle(products)<\/p>\n<p>println products*.name<br \/>\n\/\/ Output : [Washing Machine, Refrigerator, AC, Fan, Radio, TV]<br \/>\n[\/java]<\/p>\n<p>Hope it will help!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I found a very interesting method in java to change the order of elements in a list randomly, also called shuffling of elements in list. Actually I got the requirement to show a random product details out of 5 products, each time user refreshes the page. I came out with the solution using Collections.shuffle(&lt;list&gt;) For [&hellip;]<\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1],"tags":[1076,1075,1074],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8660"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8660"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8660\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}