import java.nio.file.*; import java.io.*; import java.util.*; import java.util.stream.*; public class Examples { public static void main(String[] args) throws IOException { int[] numbers = {18, 9, 7, 42, 3, 8, 15, 32, 208, 315, 8, 12, 8}; // standard array traversal System.out.print("numbers:"); for (int i = 0; i < numbers.length; i++) { System.out.print(" " + numbers[i]); } System.out.println(); // for each loop System.out.print("numbers:"); for (Integer val : numbers) { System.out.print(" " + val); } System.out.println(); // Java 8 approach involves working with streams // streams are of the form: source of data -> modifiers -> terminator // functions are specified with a lambda (anonymous function) System.out.print("numbers:"); Arrays.stream(numbers) .sorted() .distinct() .forEach(n -> System.out.print(" " + n)); System.out.println(); Files.list(Paths.get(System.getProperty("user.dir"))) .forEach(n -> sort(n)); } public static void sort(Path p){ String parent = p.getParent().toString(); String[] folders = {"jpg", "mp3", "txt"}; for (int i = 0; i < folders.length; i++) { if (p.toString().endsWith("." + folders[i])) { Path folderPath = Paths.get(parent + "/" + folders[i]); try { Files.move(p, folderPath.resolve(p.getFileName()), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { System.out.println(e); } } } } }