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

Log4j example in java using properties file

  • Logging is very important part of programming. Logging helps programmer to understand process flow and identify the problems where actually occurred.
  • Log4J will be configured externally using properties file. We can print the logging statements in the console or we can push them in to a log file based on the requirement.
  •  org.apache.log4j class will provide required classes to implement logging
  • We need to add Log4J dependency in our project.
  • Create instance of logger by using Logger.getLogger(Log4JExample.class);
  • Lets see how to create log4j.properties file in eclipse



1. Create a maven project and add Log4J dependency:


how to create log4j.properties file in eclipse

2. Create log4j.properties file


log4j.properties example file

  1. log4j.rootLogger=INFO, console

  2. log4j.appender.console=org.apache.log4j.ConsoleAppender

  3. log4j.appender.console.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS zzz}

3.Create java example program to read log4j.properties file

  1. import org.apache.log4j.BasicConfigurator;
  2. import org.apache.log4j.Logger;

  3. public class Log4JExample {

  4. static Logger logger = Logger.getLogger(Log4JExample.class);
  5.     public static void main(String[] args)
  6.     {
  7.     BasicConfigurator.configure();
  8.     logger.info("main method start!!");
  9.    
  10.     System.out.println("hi");
  11.     logger.info("log4j properties configuration example");
  12.      
  13.     logger.info("main method end!!");
  14.     }
  15. }

Output:

  1. 2018-02-08 23:09:10.747 IST0 [main] INFO Log4JExample  - main method start!!
  2. hi
  3. 2018-02-08 23:09:10.752 IST5 [main] INFO Log4JExample  - log4j properties configuration example
  4. 2018-02-08 23:09:10.753 IST6 [main] INFO Log4JExample  - main method end!!

Java program to reverse ArrayList elements

  • How to reverse an ArrayList in java.
  • By using Collections.reverse() method we can reverse ArrayList in java.



#1: Java Example program to reverse ArrayList 

  1. package com.instanceofjava;

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

  4. public class ReverseArrayList {
  5. /**
  6. * @author www.Instanceofjava.com
  7. * @category interview questions
  8. * Description: Java Example program to reverse an ArrayList
  9. *
  10. */
  11. public static void main(String[] args) {
  12. ArrayList<String> arrayList= new ArrayList<>();
  13. arrayList.add("Apple");
  14. arrayList.add("Banana");
  15. arrayList.add("Orange");
  16. Collections.reverse(arrayList);
  17. System.out.println(arrayList);
  18. }

  19. }


Output:

  1. [Orange, Banana, Apple]


#2: Java Example program to print arraylist in reverse order 


reverse arraylist in java example program

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
Select Menu