How to Delete folder and subfolders using Java 8

  • How to delete folder with all files and sub folders in it using java 8.
  • We can use java 8 Stream to delete folder recursively 
  • Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS)
  • .sorted(Comparator.reverseOrder())
  • .map(Path::toFile)
  • .peek(System.out::println)
  • .forEach(File::delete);

  1. Files.walk -  this method return all files/directories below the parent folder
  2. .sorted - sort the list in reverse order, so the folder itself comes after the including subfolders and files
  3. .map - map the file path to file
  4. .peek - points to processed entry
  5. .forEach - on every File object calls the .delete() method 




#1: Java Example program to delete folders and subfolders using java 8 stream

  1. package com.instanceofjava;

  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.file.FileVisitOption;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. import java.util.Comparator;
  9. /**
  10.  * @author www.Instanceofjava.com
  11.  * @category interview questions
  12.  * 
  13.  * Description: delete folders and sub folders using java 8
  14.  *
  15.  */

  16. public class DeleteFolder {

  17. public static void main(String[] args) {
  18. Path rootPath = Paths.get("C:\\Users\\Saidesh kilaru\\Desktop\\folder1");
  19. try {
  20. Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS)
  21.     .sorted(Comparator.reverseOrder())
  22.     .map(Path::toFile)
  23.     .peek(System.out::println)
  24.     .forEach(File::delete);
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }

  29. }

Output:

  1. C:\Users\Saidesh kilaru\Desktop\folder1\subfolder\file2 in sub folder.docx
  2. C:\Users\Saidesh kilaru\Desktop\folder1\subfolder
  3. C:\Users\Saidesh kilaru\Desktop\folder1\file1.docx
  4. C:\Users\Saidesh kilaru\Desktop\folder1



How to Convert integer set to int array using Java 8

  • How to convert Integer Set to primitive int array.
  • By Using java 8 Streams we can convert Set to array.
  • set.stream().mapToInt(Number::intValue).toArray();
  • Lets see an example java program on how to convert integer set to int array using java 8

#1: Java Example program on converting Integer Set to int Array

  1. package com.instanceofjava;

  2. import java.util.Arrays;
  3. import java.util.HashSet;
  4. import java.util.Set;

  5. public class SetToArray {
  6. /**
  7.  * @author www.Instanceofjava.com
  8.  * @category interview programming questions
  9.  * 
  10.  * Description: convert Integer set to int array using java 8
  11.  *
  12.  */
  13. public static void main(String[] args) {
  14. Set<Integer> hashset= new HashSet<>(Arrays.asList(12,34,56,78,99));
  15. int[] array = hashset.stream().mapToInt(Number::intValue).toArray();
  16. for (int i : array) {
  17. System.out.println(i);
  18. }

  19. }

  20. }

Output:

  1. 34
  2. 99
  3. 56
  4. 12
  5. 78





integer set to integer array java


In Java, a Set is a collection that contains no duplicate elements and is unordered. To convert a Set to an array, you can use the toArray() method of the Set interface. This method returns an array containing all of the elements in the Set in the order they are returned by the iterator.

Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(3);

Integer[] array = set.toArray(new Integer[set.size()]);



In this example, we first create a HashSet of integers, add some elements to it, then we use the toArray method to convert it to an array of integers.

toArray(T[] a) method where we can pass an empty array of a specific type, the method will fill the array with the elements from the set, this is useful if you know the size of the array that you need.

converting a Set to an array in Java can be done using the toArray() method of the Set interface. The method returns an array containing all of the elements in the Set in the order they are returned by the iterator. This method can also accept an empty array of a specific type, the method will fill the array with the elements from the set.

Initializing a boolean array in java with an example program

  • Initializing a boolean variable : boolean b=true;
  • In some cases we need to initialize all values of boolean array with true or false.
  • In such cases we can use Arrays.fill() method
  • Arrays.fill(array, Boolean.FALSE);
  • java initialize boolean array with true:  Arrays.fill(array, Boolean.FALSE);
  • Lets see an example java program on how to assign or initialize boolean array with false or true values.



#1: Java Example program on initializing boolean array.

  1. package com.instanceofjava;

  2. import java.util.Arrays;
  3. /**
  4.  * @author www.Instanceofjava.com
  5.  * @category interview questions
  6.  * 
  7.  * Description: Initialize boolean array values with false or true
  8.  *
  9.  */
  10. public class InitializeBoolean {

  11. public static void main(String[] args) {
  12. Boolean[] array = new Boolean[4];
  13. //initially all values will be null
  14. for (int i = 0; i < array.length; i++) {
  15. System.out.println(array[i]);
  16. }
  17. Arrays.fill(array, Boolean.FALSE);
  18. // all values will be false
  19. for (int i = 0; i < array.length; i++) {
  20. System.out.println(array[i]);
  21. }
  22. Arrays.fill(array, Boolean.TRUE);
  23. // all values will be false
  24. for (int i = 0; i < array.length; i++) {
  25. System.out.println(array[i]);
  26. }
  27. }

  28. }

Output:

  1. null
  2. null
  3. null
  4. null
  5. false
  6. false
  7. false
  8. false
  9. true
  10. true
  11. true
  12. true


java initialize boolean array with true

Java 8 initialize set with values with an example program

  • We can initialize set while defining by passing values to constructor.
  • For example to initialize HashSet we can use Arrays.asList(value1,value2).
  • Set<Integer> hashset = new HashSet<>(Arrays.asList(12, 13));

#1: Java Example program to initialize set without using java 8

  1. import java.util.Arrays;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. /**
  5.  * @author www.Instanceofjava.com
  6.  * @category interview questions
  7.  * 
  8.  * Description: Initialize set 
  9.  *
  10.  */
  11. public class InitializeSet {

  12. public static void main(String[] args) {

  13. Set<Integer> hashset = new HashSet<>(Arrays.asList(12, 13));
  14. System.out.println(hashset);
  15. }

  16. }

Output:

  1. [12, 13]


  • We can initialize set in java 8 using Stream.
  • Stream.of("initialize", "set").collect(Collectors.toSet());



#2: Java Example program to initialize set without using java 8

  1. import java.util.Set;
  2. import java.util.stream.Collectors;
  3. import java.util.stream.Stream;
  4. /**
  5.  * @author www.Instanceofjava.com
  6.  * @category interview questions
  7.  * 
  8.  * Description: Initialize set using java 8 Stream
  9.  *
  10.  */
  11. public class InitializeSet {

  12. public static void main(String[] args) {

  13. Set<String> set = Stream.of("initialize", "set").collect(Collectors.toSet());
  14. System.out.println(set);
  15. }

  16. }



Output:

  1. [set, initialize]


  • We can initialize Set in java 8 by creating stream from an Array and list


#3: Java Example program to initialize set without using java 8


java 8 initialize set with values with an example program

Top 10 Java array example programs with output

1.Java Example program to find missing numbers in an array.


2. Java interview Example program to find second maximum number in an integer array


3.Java Practice programs on arrays: find second smallest number.


4. How many ways we can print arrays in java: lets check below link for 5 different ways.


5.Advantages and disadvantages of arrays




6. Benefits of arraylist over arrays


7. Creating Array of objects in java 


8. Find top two maximum numbers in an array : java array practice programs


9. Remove duplicates from an array java


10. Sort integer array using Bubble Sort in java


How to run multiple java programs simultaneously in eclipse

  • In some cases we may need to run two java programs simultaneously and need to observe the ouput of two progarsms. In such cases we need to run multiple java programs parallel.  
  • Now we will see how to run two java programs simultaneously
  • First thing we need to understand is we can run multiple java programs at a time in eclipse.
  • Second thing is we can view multiple consoles in eclipse.   



#1. How can we open multiple consoles in eclipse?

  • In Eclipse console window right side we will have one rectangular box with Plus symbol on it to open a new console. by clicking on it we can open a new console view.

open multiple consoles  view in eclipse


2: Create two java programs.

ClassOne:
  1. package com.instanceofjava;

  2. public class ClassOne {
  3. /**
  4. * @author www.Instanceofjava.com
  5. * @category interview questions
  6. * Description: how to run two java programs simultaneously
  7. *
  8. */
  9. public static void main(String[] args) throws InterruptedException {

  10. for (int i = 0; i < 100; i++) {
  11. Thread.sleep(1000);
  12. System.out.println(i);
  13. }
  14. }
  15. }

ClassTwo
  1. package System.out;

  2. public class ClassTwo {
  3. /**
  4. * @author www.Instanceofjava.com
  5. * @category interview questions
  6. * Description: how to run two java programs simultaneously
  7. *
  8. */
  9. public static void main(String[] args) throws InterruptedException {
  10. for (int i = 100; i < 200; i++) {
  11. System.out.println(i);
  12. Thread.sleep(1000);
  13. }


  14. }

  15. }


  • Run ClassOne and ClassTwo.
  • Pin console.

pin console.png



  • You can see both the running programs with output with different console views.
how to run two java programs simultaneously

Select Menu