How to convert list to set in java with example program

  • Java program to convert list to set.
  • Convert ArrayList of string to HashSet in java example program
  • How to convert List to Set in java 
  • Set<String> strSet = new HashSet<String>(arrList);
  • HashSet having a constructor which will take list as an argument.
  • Lets see how to convert list to Set using java program.



#1: Java Example Program to Convert List to Set.


  1. package com.instanceofjava;

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

  5. public class ListToSet {
  6. /**
  7. * @author www.Instanceofjava.com
  8. * @category interview questions
  9. * Description: Convert List to set in java with example program
  10. *
  11. */
  12. public static void main(String[] args) {
  13. ArrayList<String> arrList= new ArrayList<>();
  14. arrList.add("Java");
  15. arrList.add("List to String");
  16. arrList.add("Example Program");
  17. Set<String> strSet = new HashSet<String>(arrList);
  18. System.out.println(strSet);

  19. }

  20. }

Output:

  1. [Java, Example Program, List to String]

  • Using java.util.stream we can convert List to set in java 8
  • We can use java 8 java.util.stream.Collectors
  • arrList.stream().collect(Collectors.toSet());

#2: Java Example program to convert List to Set using java 8.

  1. package com.instanceofjava;

  2. import java.util.ArrayList;
  3. import java.util.Set;
  4. import java.util.stream.Collectors;

  5. public class ListToSet {
  6. /**
  7. * @author www.Instanceofjava.com
  8. * @category interview questions
  9. * Description: Convert List to set in java with example program
  10. *
  11. */
  12. public static void main(String[] args) {
  13. ArrayList<String> arrList= new ArrayList<>();
  14. arrList.add("Java");
  15. arrList.add("List to String");
  16. arrList.add("Example Program");
  17. Set<String> strSet = arrList.stream().collect(Collectors.toSet());
  18. System.out.println(strSet);

  19. }

  20. }


Output:

java convert list to set

How to convert list to comma separated string using java 8

  • Howto convert list to comma separated string using java 8 stream.
  • By using collect() method of stream  and Collectors.join(",") method we can convert list to comma separated string in java 8.
  • Java Example program to convert list of strings to comma separated string.



#1: Java Example program to convert list to comma separated string using java 8 stream.

  1. package com.instanceofjava;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.stream.Collectors;

  5. /**
  6.  * @author www.Instanceofjava.com
  7.  * @category interview programming questions
  8.  * 
  9.  * Description: convert ArrayList of strings tp comma separated string using java 8
  10.  *
  11.  */
  12. public class ArrayToString {

  13. public static void main(String[] args) {
  14. ArrayList<String> colours =new ArrayList<>();
  15. colours.add("Red");
  16. colours.add("Green");
  17. colours.add("Orange");
  18.    String result = colours.stream().collect(Collectors.joining(","));
  19.    System.out.println(result);
  20.    
  21. }
  22. }


Output:
  1. Red,Green,Orange


convert list to comma separated string using java 8

How to remove square brackets from string in java

  • Remove square brackets from string in java.
  • We can remove square brackets from string by  using regular expressions.
  • By using regular expressions we can remove any special characters from string.
  • Now , we will check how to remove brackets from a string using regular expressions in java.



#1: Java Example program to remove brackets from string using regex.

  1. package com.instanceofjava;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: remove square brackets from string 
  7.  *
  8.  */
  9. public class ArrayToString {

  10. public static void main(String[] args) {
  11.  String strbrackets = "[String nundi ][brackets][remove cheyyadam][yela?]";
  12.  strbrackets = strbrackets.replaceAll("\\[", "").replaceAll("\\]","");
  13.  System.out.println(strbrackets);
  14.  
  15. }
  16. }


Output:

  1. String nundi bracketsremove cheyyadamyela?


#2: Java program to remove curly brackets from string 



remove square brackets from string java curly

How to convert array to string without brackets in java

  • Converting array to string and string should not contain any brackets.
  • We can convert array to string by iterating array and capturing each element from an array and append to StringBuilder/StringBuffer so that final output will be string without brackets.
  • Lets see an example java program to convert array to string by removing brackets.
  • So that we can remove brackets from string.



#1 : Java Example program to convert array to string without brackets.

  1. package com.instanceofjava;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: convert array to string without brackets.
  7.  *
  8.  */
  9. public class ArrayToString {

  10. public static void main(String[] args) {
  11.  String array[]= {"java","string","without","brackets"};
  12.  StringBuffer strbuffer = new StringBuffer();
  13.  for (String str : array) {
  14.   strbuffer.append(str).append(" ");
  15.  }
  16.  String result = strbuffer.toString();
  17.  System.out.println(result);
  18.  
  19. }

  20. }

Output:

  1. java string without brackets 



convert array to string without brackets

Java Program to convert ArrayList to String array

  • Java code to convert arraylist to string array.
  • We can convert ArrayList of strings to String array by using  toArray() method.
  • Lets see an Example java program to convert ArrayList to string array.



#1:  Java example program to convert ArrayList to String Array

  1. package com.instanceofjava;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. public class ArrayListToStringArray {
  5. /**
  6. * @author www.Instanceofjava.com
  7. * @category interview programs
  8. * Description: Java Prorgam to convert ArrayList to String array
  9. *
  10. */
  11. public static void main(String[] args) {
  12. List<String> lstflowers = new ArrayList<String>();
  13. lstflowers.add("Rose");
  14. lstflowers.add("Lilly");

  15. String[] arrayflower = new String[lstflowers.size()];
  16. arrayflower = lstflowers.toArray(arrayflower);

  17. for(String flower : arrayflower)
  18.     System.out.println(flower);
  19. }

  20. }


Output:


  1. Rose
  2. Lilly

#2:  Java example program to convert ArrayList to String Array Using java 8

  1. package com.instanceofjava;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. public class ArrayListToStringArray {
  5. /**
  6. * @author www.Instanceofjava.com
  7. * @category interview programs
  8. * Description: Java Prorgam to convert ArrayList to String array using java 8
  9. *
  10. */
  11. public static void main(String[] args) {
  12. List<String> lstflowers = new ArrayList<String>();
  13. lstflowers.add("Rose");
  14. lstflowers.add("Lilly");

  15. String[] arrayflower = lstflowers.toArray(new String[lstflowers.size()]);

  16. for(String flower : arrayflower)
  17.     System.out.println(flower);
  18. }

  19. }


java program to convert arraylist to string array

Java 8 subtract N minutes from current date

  • Java 8 provides java.time.LocalDateTime class.
  • By using minusMinutes() methods of LocalDateTime class we can subtract minutes from date or current date in java.
  • Lets see an example program on how to remove / subtract n minutes from current date using java 8.



  1. package com.instanceofjava.java8;
  2. import java.time.LocalDateTime;

  3. /**
  4.  * @author www.Instanceofjava.com
  5.  * @category interview programs
  6.  * 
  7.  * Description: subtract minutes to current date using java 8
  8.  *
  9.  */
  10. public class AddMinutesToDate {

  11. public static void main(String[] args) {
  12. //create data using java 8 LocalDateTime 
  13.     LocalDateTime datetime= LocalDateTime.now();
  14. System.out.println("Before subtracting 30 minutes to date: "+datetime);
  15.     //add seconds by using minuesMinutes(seconds) method
  16. datetime=datetime.minusMinutes(30);
  17. System.out.println("After subtracting 30 minutes to date: "+datetime);

  18. }

  19. }

Output:

  1. Before subtracting 30 minutes to date: 2018-02-05T22:41:42.463
  2. After subtracting 30 minutes to date: 2018-02-05T22:11:42.463


subtract minutes from java 8 date time.png

Select Menu