CINXE.COM

Java 8 - Collect and Shuffle List or Stream

<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><link href="//cdn.snigelweb.com" rel="preconnect"/><title>Java 8 - Collect and Shuffle List or Stream</title><meta property="og:site_name" content="Stack Abuse"/><meta property="og:title" content="Java 8 - Collect and Shuffle List or Stream"/><meta property="og:description" content="In this short Byte - learn how to collect and shuffle a stream or list in Java 8."/><meta property="og:url" content="https://stackabuse.com/bytes/java-8-collect-and-shuffle-list-or-stream/"/><meta name="description" content="In this short Byte - learn how to collect and shuffle a stream or list in Java 8."/><meta name="twitter:card" content="summary"/><meta name="twitter:title" content="Java 8 - Collect and Shuffle List or Stream"/><meta name="twitter:description" content="In this short Byte - learn how to collect and shuffle a stream or list in Java 8."/><meta name="twitter:url" content="https://stackabuse.com/bytes/java-8-collect-and-shuffle-list-or-stream/"/><meta name="twitter:site" content="@StackAbuse"/><meta name="twitter:creator" content="@StackAbuse"/><meta name="twitter:label1" content="Written by"/><meta name="twitter:data1" content="David Landup"/><script type="application/ld+json">{ "@context": "https://schema.org", "@type": "Article", "publisher": { "@type": "Organization", "name": "Stack Abuse", "logo": { "@type": "ImageObject", "url": "https://stackabuse.com/assets/images/favicon.svg" } }, "headline": "Java 8 - Collect and Shuffle List or Stream", "url": "https://stackabuse.com/bytes/java-8-collect-and-shuffle-list-or-stream/", "image": { "@type": "ImageObject", "url": null }, "description": "In this short Byte - learn how to collect and shuffle a stream or list in Java 8.", "mainEntityOfPage": { "@type": "WebPage", "@id": "https://stackabuse.com/" }, "dateCreated": "2023-01-18T14:28:44Z", "datePublished": "2023-01-06T03:30:00Z", "dateModified": "2023-01-18T14:32:19Z", "author": { "@type": "Person", "name": "David Landup", "url": "https://stackabuse.com/author/david/", "sameAs": [ "https://www.linkedin.com/in/david-landup-859455144/" ], "image": { "@type": "ImageObject", "url": "https://s3.stackabuse.com/media/users/865cd7d217ea11c9d9555c4f666e2d73.jpg" } } }</script><link rel="canonical" href="https://stackabuse.com/bytes/java-8-collect-and-shuffle-list-or-stream/"/><script data-cfasync="false" type="text/javascript"> window.snigelPubConf = { "adengine": { "activeAdUnits": ["top_leaderboard","sidebar1","sidebar3"] } } </script><script data-cfasync="false" async="" src="https://cdn.snigelweb.com/adengine/stackabuse.com/loader.js" type="text/javascript"></script><script async="" src="https://btloader.com/tag?o=5658536637890560&amp;upapi=true"></script><script src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" type="text/javascript" defer=""></script><meta name="next-head-count" content="23"/><link href="//fonts.googleapis.com" rel="preconnect"/><link href="//fonts.gstatic.com" rel="preconnect" crossorigin="true"/><link href="//cdnjs.cloudflare.com" rel="preconnect"/><link href="//s3.stackabuse.com" rel="preconnect"/><link href="//googletagmanager.com" rel="dns-prefetch"/><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="HandheldFriendly" content="True"/><link rel="shortcut icon" sizes="any" href="/assets/images/favicon.ico"/><link rel="shortcut icon" type="image/svg+xml" href="/assets/images/favicon.svg"/><link rel="preload" as="style" href="//fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&amp;display=swap"/><link rel="preload" as="style" href="//fonts.googleapis.com/css?family=Nunito:400,400i,700,700i&amp;display=swap"/><meta name="referrer" content="no-referrer-when-downgrade"/><link rel="alternate" type="application/rss+xml" title="Stack Abuse" href="https://stackabuse.com/rss/"/><link rel="preload" href="/_next/static/css/eaee8aa5ba78cb00.css" as="style"/><link rel="stylesheet" href="/_next/static/css/eaee8aa5ba78cb00.css" data-n-g=""/><noscript data-n-css=""></noscript><script defer="" nomodule="" src="/_next/static/chunks/polyfills-c67a75d1b6f99dc8.js"></script><script src="/_next/static/chunks/webpack-5adf41f2348cdbab.js" defer=""></script><script src="/_next/static/chunks/framework-1dabaad0401a053e.js" defer=""></script><script src="/_next/static/chunks/main-7cc54cdcfbd9bea6.js" defer=""></script><script src="/_next/static/chunks/pages/_app-85dd70c54f224ed0.js" defer=""></script><script src="/_next/static/chunks/c78d26b1-fe043233b220ebfb.js" defer=""></script><script src="/_next/static/chunks/816-dfcc5ef3cff7cadb.js" defer=""></script><script src="/_next/static/chunks/3646-f084444085f451b1.js" defer=""></script><script src="/_next/static/chunks/7329-ea47eb245049fa3f.js" defer=""></script><script src="/_next/static/chunks/2221-aa293f4e0cf5d5c8.js" defer=""></script><script src="/_next/static/chunks/2325-32d98f7a450411f4.js" defer=""></script><script src="/_next/static/chunks/6359-49f1ffe3cce0d438.js" defer=""></script><script src="/_next/static/chunks/8069-a016d67cff28dd39.js" defer=""></script><script src="/_next/static/chunks/2555-26ee58aa5fb641d3.js" defer=""></script><script src="/_next/static/chunks/pages/bytes/%5Bslug%5D-6075ed32f11a883f.js" defer=""></script><script src="/_next/static/1740324887682/_buildManifest.js" defer=""></script><script src="/_next/static/1740324887682/_ssgManifest.js" defer=""></script></head><body><div id="__next"><div><nav class="bg-white pb-2 border-b shadow"><div class="mx-8 px-2 sm:px-4 lg:px-8"><div class="flex justify-between h-16"><div class="flex items-center px-2 lg:px-0"><a href="/"><div class="flex-shrink-0 flex items-center"><svg class="h-6 w-full" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 494.36 63"><defs><style>.cls-1{fill:#363636}.cls-2{fill:#f29c80}.cls-3{fill:#b34a28}.cls-4{fill:#f16335}</style></defs><title>SA Logotype</title><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path class="cls-1" d="M104.94,27.65q-3.15-1.44-4.65-2.16c-1-.48-2.17-1.12-3.49-1.91A10.35,10.35,0,0,1,94,21.35a3.58,3.58,0,0,1-.82-2.27,4.79,4.79,0,0,1,1.74-3.89,7.6,7.6,0,0,1,5-1.44,16,16,0,0,1,5.61,1.15,27.13,27.13,0,0,1,4.1,1.84c.73.46,1.3.83,1.71,1.12l5.2-10.8-2-1.44A28.63,28.63,0,0,0,108,2.84a29.37,29.37,0,0,0-9-1.4A18.5,18.5,0,0,0,86.2,6.52a16.81,16.81,0,0,0-5.61,13.06,14.64,14.64,0,0,0,2.81,8.61,21.59,21.59,0,0,0,7.93,6.66c.82.43,2.09,1.07,3.8,1.91s2.94,1.46,3.69,1.87a27.75,27.75,0,0,1,2.6,1.65,6.9,6.9,0,0,1,2,2.09,4.57,4.57,0,0,1,.58,2.27q0,5.84-8.14,5.83a14.52,14.52,0,0,1-6.46-1.87,35.41,35.41,0,0,1-5.1-3c-.95-.74-1.71-1.35-2.25-1.83L75.67,55.15l.89.79a19.23,19.23,0,0,0,2.8,2c1.28.77,2.71,1.55,4.31,2.34a26.52,26.52,0,0,0,6,2,33.14,33.14,0,0,0,7.25.79,29.54,29.54,0,0,0,8-1,21.94,21.94,0,0,0,6.74-3.21,14.53,14.53,0,0,0,4.79-6,21.36,21.36,0,0,0,1.74-8.93,13.77,13.77,0,0,0-3.86-9.54A28,28,0,0,0,104.94,27.65Z"></path><path class="cls-1" d="M143.66,50.69a5.8,5.8,0,0,1-1.17.57,3.84,3.84,0,0,1-1.43.29q-2.75,0-2.74-5.11V34.7h7.39V25h-7.39V11.23H126.21V25h-5.06V34.7h5.06V48.89a22.21,22.21,0,0,0,.55,5,16,16,0,0,0,1.81,4.47,9,9,0,0,0,3.73,3.38A12.76,12.76,0,0,0,138.18,63a17.23,17.23,0,0,0,8.9-2.3l-3-10.3Z"></path><path class="cls-1" d="M177.44,28.51A15.41,15.41,0,0,0,166,23.9a15.82,15.82,0,0,0-12.51,5.69,20.47,20.47,0,0,0-4.93,13.9,20.33,20.33,0,0,0,4.93,13.86A15.85,15.85,0,0,0,166,63a14.47,14.47,0,0,0,11.49-5.26h.14v4.18h12V25H177.44Zm-2,21.17a7.68,7.68,0,0,1-5.85,2.45,7.79,7.79,0,0,1-6-2.41,8.74,8.74,0,0,1-2.26-6.23,8.69,8.69,0,0,1,2.36-6.27,8.15,8.15,0,0,1,11.63,0,8.69,8.69,0,0,1,2.36,6.27A8.74,8.74,0,0,1,175.43,49.68Z"></path><path class="cls-1" d="M216.43,34.34a9.4,9.4,0,0,1,5.88,1.73l4.11-8.71-1.16-.86a15,15,0,0,0-4.18-1.73,22.57,22.57,0,0,0-6.43-.87,18.72,18.72,0,0,0-13.57,5.8,18.81,18.81,0,0,0-5.85,13.86,18.52,18.52,0,0,0,5.85,13.72A18.83,18.83,0,0,0,214.65,63a22,22,0,0,0,6.54-.94,20,20,0,0,0,3.89-1.47c.57-.36,1-.66,1.34-.9l-4.11-8.86-.61.43a9.12,9.12,0,0,1-2.16.87,10.87,10.87,0,0,1-3.11.43,7.75,7.75,0,0,1-5.88-2.66,9.22,9.22,0,0,1-2.46-6.48A9.1,9.1,0,0,1,210.55,37,7.72,7.72,0,0,1,216.43,34.34Z"></path><polygon class="cls-1" points="271.43 24.98 255.69 24.98 243.45 37.87 243.45 0 231.34 0 231.34 61.92 243.45 61.92 243.45 47.16 257.47 61.92 273.07 61.92 254.74 42.62 271.43 24.98"></polygon><polygon class="cls-1" points="296.96 2.52 274.18 61.92 288 61.92 302.84 20.88 302.98 20.88 317.82 61.92 331.63 61.92 308.86 2.52 296.96 2.52"></polygon><path class="cls-1" d="M357.42,23.9a15.41,15.41,0,0,0-11.49,4.61V0H333.82V61.92h12V57.74h.14A14.47,14.47,0,0,0,357.42,63a15.88,15.88,0,0,0,12.52-5.65,20.38,20.38,0,0,0,4.92-13.86,20.51,20.51,0,0,0-4.92-13.9A15.86,15.86,0,0,0,357.42,23.9Zm2.33,25.82a7.8,7.8,0,0,1-5.95,2.41A7.65,7.65,0,0,1,348,49.68a8.69,8.69,0,0,1-2.29-6.19A8.64,8.64,0,0,1,348,37.22a7.7,7.7,0,0,1,5.78-2.44,7.78,7.78,0,0,1,5.84,2.44A8.64,8.64,0,0,1,362,43.49,8.78,8.78,0,0,1,359.75,49.72Z"></path><path class="cls-1" d="M405.78,42.55c0,3.65-.53,6.24-1.57,7.78s-2.83,2.3-5.34,2.3-4.29-.77-5.33-2.3S392,46.2,392,42.55V25h-12.1V45.65q0,9.21,4.54,13.28T398.87,63q9.92,0,14.47-4.07t4.55-13.28V25H405.78Z"></path><path class="cls-1" d="M449.11,42.19A20.21,20.21,0,0,0,445.79,40a23.31,23.31,0,0,0-3.69-1.44,8.7,8.7,0,0,1-1-.29c-.44-.14-.74-.25-.93-.32l-.82-.32-.78-.33-.65-.32a2.25,2.25,0,0,1-.58-.4c-.12-.12-.24-.26-.38-.43a1.25,1.25,0,0,1-.27-.54,2.25,2.25,0,0,1-.07-.58,1.82,1.82,0,0,1,.85-1.76,6.53,6.53,0,0,1,2.91-.47,16,16,0,0,1,7.8,2.31l4-8.36-1.43-.72a22.21,22.21,0,0,0-4.38-1.44,24.9,24.9,0,0,0-5.95-.72q-7,0-11.18,3.14a10.32,10.32,0,0,0-4.14,8.74,9.07,9.07,0,0,0,1.91,6,18.12,18.12,0,0,0,5.2,4.06,28.82,28.82,0,0,0,3.59,1.59A14.8,14.8,0,0,1,439,49a2.2,2.2,0,0,1,1,1.9c0,1.78-1.26,2.67-3.76,2.67a9.22,9.22,0,0,1-4-1.19A23.33,23.33,0,0,1,429,50.51c-.6-.46-1.08-.85-1.44-1.19L422.61,58l1.5,1.3A17.63,17.63,0,0,0,429,61.78,21,21,0,0,0,436.15,63q16.62,0,16.62-12.6a11.23,11.23,0,0,0-1-4.75A9.24,9.24,0,0,0,449.11,42.19Z"></path><path class="cls-1" d="M494.36,43.49q0-9.43-5.24-14.51T475.55,23.9a18.46,18.46,0,0,0-13.62,5.76,19,19,0,0,0-5.67,13.83,18.9,18.9,0,0,0,5.67,13.79A18.54,18.54,0,0,0,475.55,63q12.72,0,18.19-9.65L485,48.74a13.63,13.63,0,0,1-3.9,3.28,10.84,10.84,0,0,1-4.65.83,8.3,8.3,0,0,1-5.2-1.87q-2.4-1.87-2.4-5.76h25.45A14.91,14.91,0,0,0,494.36,43.49Zm-25.17-5.33a6,6,0,0,1,1.91-4.32,6.77,6.77,0,0,1,4.79-1.66,6.51,6.51,0,0,1,4.48,1.55,7,7,0,0,1,2.15,4.43Z"></path><polygon class="cls-2" points="66.05 19.08 73.15 1.3 57.25 1.3 50.16 19.08 66.05 19.08"></polygon><polygon class="cls-3" points="40.21 43.99 32.99 62.06 48.88 62.06 56.1 43.99 40.21 43.99"></polygon><polygon class="cls-4" points="57.69 40.01 64.46 23.05 48.57 23.05 41.8 40.01 57.69 40.01"></polygon><polygon class="cls-2" points="33.06 19.08 40.16 1.3 24.27 1.3 17.17 19.08 33.06 19.08"></polygon><polygon class="cls-3" points="7.22 43.99 0 62.06 15.89 62.06 23.11 43.99 7.22 43.99"></polygon><polygon class="cls-4" points="24.7 40.01 31.47 23.05 15.58 23.05 8.81 40.01 24.7 40.01"></polygon></g></g></svg></div></a><div class="hidden lg:block lg:ml-6"><div class="hidden lg:ml-6 lg:flex"><a href="/tools/" class="mr-8 inline-flex items-center px-1 pt-1 border-b-2 border-blue-400 text-sm font-medium leading-10 text-gray-500 hover:text-gray-700 focus:outline-none focus:text-gray-700 transition duration-150 ease-in-out">Tools</a><div class="px-1 pt-1 border-b-2 border-green-400 focus:border-gray-300 transition duration-150 ease-in-out"><div class="relative" data-headlessui-state=""><button class="text-gray-500 group rounded-md inline-flex items-center focus:outline-none text-sm font-medium leading-10 text-gray-500 hover:text-gray-700 hover:border-gray-300 focus:outline-none focus:text-gray-700" type="button" aria-expanded="false" data-headlessui-state=""><span>Learn</span><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true" class="text-gray-400 ml-2 h-5 w-5 group-hover:text-gray-500"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg></button></div><div hidden="" style="position:fixed;top:1px;left:1px;width:1px;height:0;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0;display:none"></div></div><a href="/about/" class="ml-8 inline-flex items-center px-1 pt-1 border-b-2 border-red-400 text-sm font-medium leading-10 text-gray-500 hover:text-gray-700 focus:outline-none focus:text-gray-700 transition duration-150 ease-in-out">About</a></div></div></div><div class="hidden mt-4 md:flex md:flex-1 md:items-center md:justify-center md:px-2 lg:ml-6 lg:justify-end"><div class="relative w-1/2 text-gray-400 transition duration-200 focus-within:text-gray-600"><div class="absolute inset-y-0 left-0 flex items-center pointer-events-none" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true" class="h-5 w-5 ml-2"><path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd"></path></svg></div><input id="search-field" class="block w-full h-full pl-10 pr-3 py-3 text-sm border-gray-300 text-gray-900 placeholder-gray-500 rounded transition duration-200 focus:outline-none focus:ring-0 focus:border-gray-400" placeholder="Search" type="text"/></div><div class="hidden ml-6 md:flex items-center"><button class="whitespace-nowrap text-base font-medium text-gray-500 hover:text-gray-900">Sign in</button><button class="ml-6 whitespace-nowrap inline-flex items-center justify-center px-4 py-2 border border-transparent rounded-md shadow-sm text-base font-medium text-white bg-orange-500 hover:bg-orange-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-orange-500">Sign up</button></div></div><div class="flex items-center lg:hidden"><button class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 focus:text-gray-500 transition duration-150 ease-in-out" aria-label="Main menu" aria-expanded="false"><svg class="block h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path></svg><svg class="hidden h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg></button></div></div></div><div class="hidden lg:hidden"><div class="pt-2 pb-3"><a href="/tag/python" class="mt-1 block pl-3 pr-4 py-2 border-l-4 border-transparent text-base font-medium text-gray-600 hover:text-gray-800 hover:bg-gray-50 hover:border-gray-300 focus:outline-none focus:text-gray-800 focus:bg-gray-50 focus:border-gray-300 transition duration-150 ease-in-out">Python</a><a href="/tag/javascript" class="mt-1 block pl-3 pr-4 py-2 border-l-4 border-transparent text-base font-medium text-gray-600 hover:text-gray-800 hover:bg-gray-50 hover:border-gray-300 focus:outline-none focus:text-gray-800 focus:bg-gray-50 focus:border-gray-300 transition duration-150 ease-in-out">JavaScript</a><a href="/tag/java" class="mt-1 block pl-3 pr-4 py-2 border-l-4 border-transparent text-base font-medium text-gray-600 hover:text-gray-800 hover:bg-gray-50 hover:border-gray-300 focus:outline-none focus:text-gray-800 focus:bg-gray-50 focus:border-gray-300 transition duration-150 ease-in-out">Java</a></div></div></nav><main><div class="flex items-stretch min-h-screen"><div class="w-full pb-6"><div class="bg-gradient-to-tl from-gray-100 to-gray-300"><div class="grid grid-cols-24 gap-4 pt-8 text-gray-800"><div class="col-span-3"></div><div class="col-span-20"><div class="hidden sm:flex items-center text-sm leading-5 font-medium"><a href="/" class="text-gray-500 hover:text-gray-700 transition duration-150 ease-in-out">Home</a><svg class="flex-shrink-0 mx-2 h-5 w-5 text-gray-600" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"></path></svg><span class="text-gray-500">Bytes</span></div><div class="py-2"><h1 class="text-3xl font-bold leading-7 sm:text-4xl sm:leading-9">Java 8 - Collect and Shuffle List or Stream</h1><div class="flex items-center mt-4"><div><img class="inline-block h-8 w-8 rounded-full ring-2 ring-gray-200" src="//s3.stackabuse.com/media/users/865cd7d217ea11c9d9555c4f666e2d73.jpg" alt=""/></div><div class="ml-4"><a href="/author/david/" class="text-gray-500 hover:text-gray-700">David Landup</a></div><div></div></div></div></div></div><div style="color:#ffffff"><svg class="mt-4 transform rotate-180" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 65"><path fill="currentColor" fill-opacity="1" d="M0,64L60,53.3C120,43,240,21,360,10.7C480,0,600,0,720,10.7C840,21,960,43,1080,48C1200,53,1320,43,1380,37.3L1440,32L1440,0L1380,0C1320,0,1200,0,1080,0C960,0,840,0,720,0C600,0,480,0,360,0C240,0,120,0,60,0L0,0Z"></path></svg></div></div><div class="mt-8 grid grid-cols-1 md:grid-cols-24 mx-2 md:mx-0 md:ml-8 md:gap-8"><div class="hidden md:block lg:col-span-3"></div><div class="col-span-13"><div><div class=" my-4 flex justify-center" style="min-height:90px"><div id="adngin-top_leaderboard-0"></div></div><div class="mt-8 content"><noscript><style>.lazyload-placeholder { display: none; }</style></noscript><p>In this short Byte - we&#x27;ll take a look at how you can collect and shuffle a list in Java, using the Functional API, and paired with the <code>collectingAndThen()</code> collector.</p> <div class="alert alert-reference"> <div class="flex"> <div class="flex-shrink-0 mr-3"> <img src="/assets/images/icon-link-solid.svg" class="icon" aria-hidden="true"/> </div> <div class="w-full"> <p><strong>Advice:</strong> If you&#x27;d like to read more about <code>collectingAndThen()</code> - read our in-depth <a target="_blank" href="https://stackabuse.com/guide-to-java-8-collectors-collectingandthen/"><em>&quot;Guide to Java 8 Collectors: collectingAndThen()&quot;!</em></a></p> </div> </div> </div> <h3 id="collectandshufflealistinjava">Collect and Shuffle a List in Java</h3> <p>When collecting a stream back into a list (streamed from a Java <code>Collection</code>) - you may decide to shuffle it. Using Java 8&#x27;s Functional API - working with streams is efficient and simple.</p> <p>An intuitive method to make use of here is <code>collectingAndThen()</code> which allows you to <code>collect()</code> a <code>Stream</code> and then run an anonymous function on the result:</p> <pre><code class="hljs"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Shuffle</span> </span>{ <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> Stream&lt;?&gt; stream; <span class="hljs-keyword">public</span> List&lt;?&gt; getList() { <span class="hljs-keyword">return</span> stream.collect( collectingAndThen( toList(), l -&gt; { Collections.shuffle(l); <span class="hljs-keyword">return</span> l; } ) ); } } </code></pre> <p>Here, we&#x27;ve collected the stream into a <code>List</code>, which is a <em>terminal operation</em> (ending the stream). Then, that list is streamed again, utilizing the central <code>Collections.shuffle()</code> method, which accepts any valid <code>Collection</code> and shuffles it in-place.</p> <p>You can test the method and assert the correct output with:</p> <pre><code class="hljs"><span class="hljs-meta">@Test</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">shouldShuffleList</span><span class="hljs-params">()</span> </span>{ Shuffle shuffle = <span class="hljs-keyword">new</span> Shuffle(Stream.of(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>)); List&lt;?&gt; list = shuffle.getList(); assertNotEquals(<span class="hljs-string">&quot;[1, 2, 3]&quot;</span>, list.toString()); } </code></pre> <h2 id="conclusion">Conclusion</h2> <p>In this short Byte - we took a look at how you can collect and shuffle a list or stream in Java 8+.</p> </div><div class="md:flex md:items-center md:justify-between mt-8 mb-4"><div class=""><a class="inline-flex items-center mr-2 px-2.5 py-0.5 rounded-md text-sm font-medium bg-gray-200 text-gray-700 hover:text-gray-900" href="/tag/java/"><span class="text-gray-400">#</span> <!-- -->java</a><a class="inline-flex items-center mr-2 px-2.5 py-0.5 rounded-md text-sm font-medium bg-gray-200 text-gray-700 hover:text-gray-900" href="/tag/streams/"><span class="text-gray-400">#</span> <!-- -->streams</a></div><div class="inline-flex text-gray-500 text-sm"><div>Last Updated: <!-- -->January 18th, 2023</div></div></div><div class="w-full lg:w-1/3 border-gray-200" style="border-top-width:6px"></div><div class="md:flex md:items-center md:justify-between py-2"><div class="flex items-center text-sm text-gray-500"><span class="mr-2">Was this helpful?</span><div class="flex justify-center sm:justify-start"><span class="cursor-pointer"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true" class="w-5 h-5"><path stroke-linecap="round" stroke-linejoin="round" d="M11.049 2.927c.3-.921 1.603-.921 1.902 0l1.519 4.674a1 1 0 00.95.69h4.915c.969 0 1.371 1.24.588 1.81l-3.976 2.888a1 1 0 00-.363 1.118l1.518 4.674c.3.922-.755 1.688-1.538 1.118l-3.976-2.888a1 1 0 00-1.176 0l-3.976 2.888c-.783.57-1.838-.197-1.538-1.118l1.518-4.674a1 1 0 00-.363-1.118l-3.976-2.888c-.784-.57-.38-1.81.588-1.81h4.914a1 1 0 00.951-.69l1.519-4.674z"></path></svg></span><span class="cursor-pointer"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true" class="w-5 h-5"><path stroke-linecap="round" stroke-linejoin="round" d="M11.049 2.927c.3-.921 1.603-.921 1.902 0l1.519 4.674a1 1 0 00.95.69h4.915c.969 0 1.371 1.24.588 1.81l-3.976 2.888a1 1 0 00-.363 1.118l1.518 4.674c.3.922-.755 1.688-1.538 1.118l-3.976-2.888a1 1 0 00-1.176 0l-3.976 2.888c-.783.57-1.838-.197-1.538-1.118l1.518-4.674a1 1 0 00-.363-1.118l-3.976-2.888c-.784-.57-.38-1.81.588-1.81h4.914a1 1 0 00.951-.69l1.519-4.674z"></path></svg></span><span class="cursor-pointer"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true" class="w-5 h-5"><path stroke-linecap="round" stroke-linejoin="round" d="M11.049 2.927c.3-.921 1.603-.921 1.902 0l1.519 4.674a1 1 0 00.95.69h4.915c.969 0 1.371 1.24.588 1.81l-3.976 2.888a1 1 0 00-.363 1.118l1.518 4.674c.3.922-.755 1.688-1.538 1.118l-3.976-2.888a1 1 0 00-1.176 0l-3.976 2.888c-.783.57-1.838-.197-1.538-1.118l1.518-4.674a1 1 0 00-.363-1.118l-3.976-2.888c-.784-.57-.38-1.81.588-1.81h4.914a1 1 0 00.951-.69l1.519-4.674z"></path></svg></span><span class="cursor-pointer"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true" class="w-5 h-5"><path stroke-linecap="round" stroke-linejoin="round" d="M11.049 2.927c.3-.921 1.603-.921 1.902 0l1.519 4.674a1 1 0 00.95.69h4.915c.969 0 1.371 1.24.588 1.81l-3.976 2.888a1 1 0 00-.363 1.118l1.518 4.674c.3.922-.755 1.688-1.538 1.118l-3.976-2.888a1 1 0 00-1.176 0l-3.976 2.888c-.783.57-1.838-.197-1.538-1.118l1.518-4.674a1 1 0 00-.363-1.118l-3.976-2.888c-.784-.57-.38-1.81.588-1.81h4.914a1 1 0 00.951-.69l1.519-4.674z"></path></svg></span><span class="cursor-pointer"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true" class="w-5 h-5"><path stroke-linecap="round" stroke-linejoin="round" d="M11.049 2.927c.3-.921 1.603-.921 1.902 0l1.519 4.674a1 1 0 00.95.69h4.915c.969 0 1.371 1.24.588 1.81l-3.976 2.888a1 1 0 00-.363 1.118l1.518 4.674c.3.922-.755 1.688-1.538 1.118l-3.976-2.888a1 1 0 00-1.176 0l-3.976 2.888c-.783.57-1.838-.197-1.538-1.118l1.518-4.674a1 1 0 00-.363-1.118l-3.976-2.888c-.784-.57-.38-1.81.588-1.81h4.914a1 1 0 00.951-.69l1.519-4.674z"></path></svg></span></div></div><div class="inline-flex"><a href="https://twitter.com/share?text=Check out Java 8 - Collect and Shuffle List or Stream!&amp;amp;url=https://stackabuse.com/bytes/java-8-collect-and-shuffle-list-or-stream/&amp;amp;via=StackAbuse"><svg class="h-6 w-6 text-gray-500 hover:text-twitter" fill="currentColor" viewBox="0 0 24 24"><path d="M8.29 20.251c7.547 0 11.675-6.253 11.675-11.675 0-.178 0-.355-.012-.53A8.348 8.348 0 0022 5.92a8.19 8.19 0 01-2.357.646 4.118 4.118 0 001.804-2.27 8.224 8.224 0 01-2.605.996 4.107 4.107 0 00-6.993 3.743 11.65 11.65 0 01-8.457-4.287 4.106 4.106 0 001.27 5.477A4.072 4.072 0 012.8 9.713v.052a4.105 4.105 0 003.292 4.022 4.095 4.095 0 01-1.853.07 4.108 4.108 0 003.834 2.85A8.233 8.233 0 012 18.407a11.616 11.616 0 006.29 1.84"></path></svg></a><a href="https://www.facebook.com/sharer/sharer.php?u=https://stackabuse.com/bytes/java-8-collect-and-shuffle-list-or-stream/"><svg class="h-6 w-6 ml-2 text-gray-500 hover:text-facebook" fill="currentColor" viewBox="0 0 24 24"><path fill-rule="evenodd" d="M22 12c0-5.523-4.477-10-10-10S2 6.477 2 12c0 4.991 3.657 9.128 8.438 9.878v-6.987h-2.54V12h2.54V9.797c0-2.506 1.492-3.89 3.777-3.89 1.094 0 2.238.195 2.238.195v2.46h-1.26c-1.243 0-1.63.771-1.63 1.562V12h2.773l-.443 2.89h-2.33v6.988C18.343 21.128 22 16.991 22 12z" clip-rule="evenodd"></path></svg></a><a href="/cdn-cgi/l/email-protection#7e410d0b1c141b1d0a43341f081f5e465e535e3d1112121b1d0a5e1f101a5e2d160b1818121b5e32170d0a5e110c5e2d0a0c1b1f135e08171f5e2d0a1f1d153f1c0b0d1b501d1113581f130e451f130e451c111a07433d161b1d155e110b0a5e0a161b5e1f0c0a171d121b5e160a0a0e0d4451510d0a1f1d151f1c0b0d1b501d1113511c070a1b0d51141f081f5346531d1112121b1d0a531f101a530d160b1818121b5312170d0a53110c530d0a0c1b1f13515e11105e2d0a1f1d153f1c0b0d1b501d11135f"><svg class="h-6 w-6 ml-2 text-gray-500 hover:text-gray-900" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 12a4 4 0 10-8 0 4 4 0 008 0zm0 0v1.5a2.5 2.5 0 005 0V12a9 9 0 10-9 9m4.5-1.206a8.959 8.959 0 01-4.5 1.207"></path></svg></a></div></div></div><div><div class="flex content-start flex-wrap"><div class="mt-12"><div class="flex items-center"><div class="flex-shrink-0"><a href="/author/david/"><div class="lazyload-wrapper "><div style="height:5rem" class="lazyload-placeholder"></div></div></a></div><div class="ml-3"><a href="/author/david/" class="font-medium text-lg leading-6"><span class="text-gray-900">David Landup</span></a><span class="ml-2 text-sm leading-5 text-gray-500 italic">Author</span><div class="flex content-start flex-wrap"><a target="_blank" rel="noopener noreferrer" href="https://www.linkedin.com/in/david-landup-859455144/" class="m-1"><svg class="text-gray-700 h-4 w-4" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM4.332 8.027a6.012 6.012 0 011.912-2.706C6.512 5.73 6.974 6 7.5 6A1.5 1.5 0 019 7.5V8a2 2 0 004 0 2 2 0 011.523-1.943A5.977 5.977 0 0116 10c0 .34-.028.675-.083 1H15a2 2 0 00-2 2v2.197A5.973 5.973 0 0110 16v-2a2 2 0 00-2-2 2 2 0 01-2-2 2 2 0 00-1.668-1.973z" clip-rule="evenodd"></path></svg></a></div><div class="mt-4"><div><p>Entrepreneur, Software and Machine Learning Engineer, with a deep fascination towards the application of Computation and Deep Learning in Life Sciences (Bioinformatics, Drug Discovery, Genomics), Neuroscience (Computational Neuroscience), robotics and BCIs.</p> <p>Great passion for accessible education and promotion of reason, science, humanism, and progress.</p> </div></div></div></div></div></div><div class="flex content-start flex-wrap"><div class="mt-6 flex items-center w-1/3"><div class="flex-shrink-0"><a href="/author/hiram-k/"><div class="lazyload-wrapper "><div style="height:3.5rem" class="lazyload-placeholder"></div></div></a></div><div class="ml-3"><a href="/author/hiram-k/" class="text-sm leading-5 font-medium"><span class="text-gray-900">Hiram Kamau</span></a><div class="flex text-sm leading-5 text-gray-500 italic">Author</div></div></div></div></div></div><div class="col-span-7 pl-8"><div class="pb-4"><div class=" my-4 flex justify-center" style="min-height:600px"><div id="adngin-sidebar1-0"></div></div></div><div class="py-4"><a href="https://pingbot.dev/?ref=stackabuse-1" class="hover:no-underline course-link" target="_blank" rel="sponsored noopener"><div class="flex flex-col border rounded-lg shadow overflow-hidden"><div class="aspect-w-16 aspect-h-9 rounded-t-lg border-2 border-gray-700"><div class="h-full w-full overflow-hidden"><div class="relative"><img class="rounded-lg object-cover" src="https://s3.pingbot.dev/images/monitor-overview.png" alt=""/></div></div></div><div class="flex-1 bg-white p-4 flex flex-col justify-between"><div class="flex-1"><div class="w-full text-base leading-5 flex justify-between"><span class="text-primary">Free</span></div><h3 class="mt-2 text-xl leading-7 font-semibold text-gray-900 hover:text-gray-900 hover:no-underline">Monitor with Ping Bot</h3><div class="inline-flex items-center mr-2 px-2.5 py-0.5 rounded-md text-xs font-medium bg-gray-200 text-gray-700 hover:text-gray-900"><span class="text-gray-400">#</span> <!-- -->monitoring</div><div class="inline-flex items-center mr-2 px-2.5 py-0.5 rounded-md text-xs font-medium bg-gray-200 text-gray-700 hover:text-gray-900"><span class="text-gray-400">#</span> <!-- -->uptime</div><div class="inline-flex items-center mr-2 px-2.5 py-0.5 rounded-md text-xs font-medium bg-gray-200 text-gray-700 hover:text-gray-900"><span class="text-gray-400">#</span> <!-- -->observability</div><p class="mt-4 text-sm leading-6 text-gray-500 break-words">Reliable monitoring for your app, databases, infrastructure, and the vendors they rely on. Ping Bot is a powerful uptime and performance monitoring tool that helps notify you and resolve issues before they affect your customers.</p></div><div class="mt-6 flex justify-between text-sm text-gray-600"><div class="flex flex-nowrap items-center gap-x-2 gap-y-2"><img src="https://s3.pingbot.dev/icons/system/openai.png" alt="OpenAI" title="OpenAI" class="h-6 w-6 rounded-full ring-2 ring-white"/><img src="https://s3.pingbot.dev/icons/system/vercel.png" alt="Vercel" title="Vercel" class="h-6 w-6 rounded-full ring-2 ring-white"/><img src="https://s3.pingbot.dev/icons/system/github.svg" alt="GitHub" title="GitHub" class="h-6 w-6 rounded-full ring-2 ring-white"/><img src="https://s3.pingbot.dev/icons/system/cloudflare.png" alt="MongoDB" title="MongoDB" class="h-6 w-6 rounded-full ring-2 ring-white"/><img src="https://s3.pingbot.dev/icons/system/postgres.svg" alt="PostgreSQL" title="PostgreSQL" class="h-6 w-6 rounded-full ring-2 ring-white"/></div><div class=""><div class="inline-flex items-center px-2 py-2 border border-gray-300 shadow-sm font-bold text-sm font-medium rounded text-gray-700 bg-white hover:bg-gray-100 hover:text-gray-700 focus:outline-none hover:no-underline">Learn more<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true" class="ml-1 w-5 h-5"><path stroke-linecap="round" stroke-linejoin="round" d="M13 7l5 5m0 0l-5 5m5-5H6"></path></svg></div></div></div></div></div></a></div><div class="py-4 sticky top-0"><div class=" my-4 flex justify-center" style="min-height:600px"><div id="adngin-sidebar3-0"></div></div></div></div></div></div></div></main><div class="bg-white border-t border-gray-300"><div class="max-w-screen-xl mx-auto pt-12 pb-6 px-4 sm:px-6 md:flex md:items-center md:justify-between lg:px-8"><div class="flex justify-center md:order-2"><a href="https://twitter.com/stackabuse" target="_blank" rel="noreferrer noopener" class="text-gray-500 hover:text-twitter"><span class="sr-only">Twitter</span><svg class="h-6 w-6" fill="currentColor" viewBox="0 0 24 24"><path d="M8.29 20.251c7.547 0 11.675-6.253 11.675-11.675 0-.178 0-.355-.012-.53A8.348 8.348 0 0022 5.92a8.19 8.19 0 01-2.357.646 4.118 4.118 0 001.804-2.27 8.224 8.224 0 01-2.605.996 4.107 4.107 0 00-6.993 3.743 11.65 11.65 0 01-8.457-4.287 4.106 4.106 0 001.27 5.477A4.072 4.072 0 012.8 9.713v.052a4.105 4.105 0 003.292 4.022 4.095 4.095 0 01-1.853.07 4.108 4.108 0 003.834 2.85A8.233 8.233 0 012 18.407a11.616 11.616 0 006.29 1.84"></path></svg></a><a href="https://github.com/stackabuse" target="_blank" rel="noreferrer noopener" class="ml-6 text-gray-500 hover:text-github"><span class="sr-only">GitHub</span><svg class="h-6 w-6" fill="currentColor" viewBox="0 0 24 24"><path fill-rule="evenodd" d="M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.202 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.943.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0022 12.017C22 6.484 17.522 2 12 2z" clip-rule="evenodd"></path></svg></a><a href="https://www.facebook.com/stackabuse" target="_blank" rel="noreferrer noopener" class="ml-6 text-gray-500 hover:text-facebook"><span class="sr-only">Facebook</span><svg class="h-6 w-6" fill="currentColor" viewBox="0 0 24 24"><path fill-rule="evenodd" d="M22 12c0-5.523-4.477-10-10-10S2 6.477 2 12c0 4.991 3.657 9.128 8.438 9.878v-6.987h-2.54V12h2.54V9.797c0-2.506 1.492-3.89 3.777-3.89 1.094 0 2.238.195 2.238.195v2.46h-1.26c-1.243 0-1.63.771-1.63 1.562V12h2.773l-.443 2.89h-2.33v6.988C18.343 21.128 22 16.991 22 12z" clip-rule="evenodd"></path></svg></a></div><div class="mt-8 md:mt-0 md:order-1"><p class="text-center text-base leading-6 text-gray-500">漏 2013-<!-- -->2025<!-- --> Stack Abuse. All rights reserved.</p><div class="text-sm"><a href="/about" class="pr-4 text-gray-500 border-r-2 border-grey-400 hover:no-underline hover:text-gray-600">About</a><a href="/disclosure" class="px-4 text-gray-500 border-r-2 border-grey-400 hover:no-underline hover:text-gray-600">Disclosure</a><a href="/privacy-policy" class="px-4 text-gray-500 border-r-2 border-grey-400 hover:no-underline hover:text-gray-600">Privacy</a><a href="/terms-of-service" class="pl-4 text-gray-500 hover:no-underline hover:text-gray-600">Terms</a><div id="ccpa" style="display:none;cursor:pointer">Do not share my Personal Information.</div></div></div></div></div></div></div><script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script><script id="__NEXT_DATA__" type="application/json">{"props":{"pageProps":{"content":{"id":1736,"title":"Java 8 - Collect and Shuffle List or Stream","slug":"java-8-collect-and-shuffle-list-or-stream","body_html":"\u003cp\u003eIn this short Byte - we'll take a look at how you can collect and shuffle a list in Java, using the Functional API, and paired with the \u003ccode\u003ecollectingAndThen()\u003c/code\u003e collector.\u003c/p\u003e\n\n \u003cdiv class=\"alert alert-reference\"\u003e\n \u003cdiv class=\"flex\"\u003e\n \n \u003cdiv class=\"flex-shrink-0 mr-3\"\u003e\n \u003cimg src=\"/assets/images/icon-link-solid.svg\" class=\"icon\" aria-hidden=\"true\" /\u003e\n \u003c/div\u003e\n \n \u003cdiv class=\"w-full\"\u003e\n \u003cp\u003e\u003cstrong\u003eAdvice:\u003c/strong\u003e If you'd like to read more about \u003ccode\u003ecollectingAndThen()\u003c/code\u003e - read our in-depth \u003ca target=\"_blank\" href=\"https://stackabuse.com/guide-to-java-8-collectors-collectingandthen/\"\u003e\u003cem\u003e\u0026quot;Guide to Java 8 Collectors: collectingAndThen()\u0026quot;!\u003c/em\u003e\u003c/a\u003e\u003c/p\u003e\n\n \u003c/div\u003e\n \u003c/div\u003e\n \u003c/div\u003e\n \u003ch3 id=\"collectandshufflealistinjava\"\u003eCollect and Shuffle a List in Java\u003c/h3\u003e\n\u003cp\u003eWhen collecting a stream back into a list (streamed from a Java \u003ccode\u003eCollection\u003c/code\u003e) - you may decide to shuffle it. Using Java 8's Functional API - working with streams is efficient and simple.\u003c/p\u003e\n\u003cp\u003eAn intuitive method to make use of here is \u003ccode\u003ecollectingAndThen()\u003c/code\u003e which allows you to \u003ccode\u003ecollect()\u003c/code\u003e a \u003ccode\u003eStream\u003c/code\u003e and then run an anonymous function on the result:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003e\u003cspan class=\"hljs-keyword\"\u003epublic\u003c/span\u003e \u003cspan class=\"hljs-class\"\u003e\u003cspan class=\"hljs-keyword\"\u003eclass\u003c/span\u003e \u003cspan class=\"hljs-title\"\u003eShuffle\u003c/span\u003e \u003c/span\u003e{\n \u003cspan class=\"hljs-keyword\"\u003eprivate\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003efinal\u003c/span\u003e Stream\u0026lt;?\u0026gt; stream;\n \n \u003cspan class=\"hljs-keyword\"\u003epublic\u003c/span\u003e List\u0026lt;?\u0026gt; getList() {\n \u003cspan class=\"hljs-keyword\"\u003ereturn\u003c/span\u003e stream.collect(\n collectingAndThen(\n toList(),\n l -\u0026gt; {\n Collections.shuffle(l);\n \u003cspan class=\"hljs-keyword\"\u003ereturn\u003c/span\u003e l;\n }\n )\n );\n } \n}\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003eHere, we've collected the stream into a \u003ccode\u003eList\u003c/code\u003e, which is a \u003cem\u003eterminal operation\u003c/em\u003e (ending the stream). Then, that list is streamed again, utilizing the central \u003ccode\u003eCollections.shuffle()\u003c/code\u003e method, which accepts any valid \u003ccode\u003eCollection\u003c/code\u003e and shuffles it in-place.\u003c/p\u003e\n\u003cp\u003eYou can test the method and assert the correct output with:\u003c/p\u003e\n\u003cpre\u003e\u003ccode class=\"hljs\"\u003e\u003cspan class=\"hljs-meta\"\u003e@Test\u003c/span\u003e\n\u003cspan class=\"hljs-function\"\u003e\u003cspan class=\"hljs-keyword\"\u003epublic\u003c/span\u003e \u003cspan class=\"hljs-keyword\"\u003evoid\u003c/span\u003e \u003cspan class=\"hljs-title\"\u003eshouldShuffleList\u003c/span\u003e\u003cspan class=\"hljs-params\"\u003e()\u003c/span\u003e \u003c/span\u003e{\n Shuffle shuffle = \u003cspan class=\"hljs-keyword\"\u003enew\u003c/span\u003e Shuffle(Stream.of(\u003cspan class=\"hljs-number\"\u003e1\u003c/span\u003e,\u003cspan class=\"hljs-number\"\u003e2\u003c/span\u003e,\u003cspan class=\"hljs-number\"\u003e3\u003c/span\u003e));\n List\u0026lt;?\u0026gt; list = shuffle.getList();\n \n assertNotEquals(\u003cspan class=\"hljs-string\"\u003e\u0026quot;[1, 2, 3]\u0026quot;\u003c/span\u003e, list.toString());\n}\n\u003c/code\u003e\u003c/pre\u003e\n\u003ch2 id=\"conclusion\"\u003eConclusion\u003c/h2\u003e\n\u003cp\u003eIn this short Byte - we took a look at how you can collect and shuffle a list or stream in Java 8+.\u003c/p\u003e\n","parent_id":null,"type":"byte","visibility":"public","img_feature":null,"is_featured":false,"locale":"en","custom_excerpt":null,"inject_header":null,"inject_footer":null,"canonical_url":null,"comment_id":null,"og_image":null,"og_title":null,"og_description":null,"twitter_image":null,"twitter_title":null,"twitter_description":null,"meta_title":null,"meta_description":"In this short Byte - learn how to collect and shuffle a stream or list in Java 8.","read_time_min":4,"published_at":1672975800000,"created_at":1674052124556,"updated_at":1674052339137,"contributors":[{"id":16,"name":"David Landup","slug":"david","role_id":2,"img_profile":"//s3.stackabuse.com/media/users/865cd7d217ea11c9d9555c4f666e2d73.jpg","img_cover":null,"bio_html":"\u003cp\u003eEntrepreneur, Software and Machine Learning Engineer, with a deep fascination towards the application of Computation and Deep Learning in Life Sciences (Bioinformatics, Drug Discovery, Genomics), Neuroscience (Computational Neuroscience), robotics and BCIs.\u003c/p\u003e\n\u003cp\u003eGreat passion for accessible education and promotion of reason, science, humanism, and progress.\u003c/p\u003e\n","website":"https://www.linkedin.com/in/david-landup-859455144/","location":"Serbia","facebook":"","twitter":"","github":null,"created_at":1534532687000,"updated_at":1692629560355,"role":"editor","_pivot_content_id":1736,"_pivot_user_id":16,"_pivot_role":"author","_pivot_sort_order":0},{"id":55,"name":"Hiram Kamau","slug":"hiram-k","role_id":4,"img_profile":"//s3.stackabuse.com/media/users/f38ed4ea2cc8a6b544f2db97e1e403f3.jpeg","img_cover":null,"bio_html":"\u003cp\u003eIn addition to catching code errors and going through debugging hell, I also obsess over whether writing in an active voice is truly better than doing it in passive.\u003c/p\u003e\n","website":"https://www.upwork.com/freelancers/~0157b88d591b73eb50","location":"Nairobi, Kenya","facebook":null,"twitter":"@hkamau_xight","github":"IdelsTak","created_at":1577973227000,"updated_at":1697580361272,"role":"author","_pivot_content_id":1736,"_pivot_user_id":55,"_pivot_role":"author","_pivot_sort_order":1}],"tags":[{"id":13,"name":"java","slug":"java","img_cover":null,"description_html":"","og_image":null,"og_title":null,"og_description":null,"twitter_image":null,"twitter_title":null,"twitter_description":null,"meta_title":null,"meta_description":null,"inject_header":null,"inject_footer":null,"canonical_url":null,"color":null,"created_at":1431358735000,"updated_at":1431358735000,"_pivot_content_id":1736,"_pivot_tag_id":13,"_pivot_sort_order":0},{"id":150,"name":"streams","slug":"streams","img_cover":null,"description_html":"","og_image":null,"og_title":null,"og_description":null,"twitter_image":null,"twitter_title":null,"twitter_description":null,"meta_title":null,"meta_description":null,"inject_header":null,"inject_footer":null,"canonical_url":null,"color":null,"created_at":1572451232000,"updated_at":1572451232000,"_pivot_content_id":1736,"_pivot_tag_id":150,"_pivot_sort_order":1}]},"relatedContent":[],"relatedCollections":[],"error":null},"__N_SSP":true},"page":"/bytes/[slug]","query":{"slug":"java-8-collect-and-shuffle-list-or-stream"},"buildId":"1740324887682","isFallback":false,"isExperimentalCompile":false,"gssp":true,"scriptLoader":[]}</script></body></html>

Pages: 1 2 3 4 5 6 7 8 9 10