Category: Java Examples

python tutorials and learn python

Created with Sketch.

Java Program to Create String from Contents of a File

Java Program to Create String from Contents of a File In this program, you’ll learn different techniques to create a string from concents of a given file in Java. Before we create a string from a file, we assume we have a file named test.txt in our src folder. Here’s the content of test.txt This…
Read more

Java Program to Convert Byte Array to Hexadecimal

Java Program to Convert Byte Array to Hexadecimal In this program, you’ll learn different techniques to convert byte array to hexadecimal in Java. Example 1: Convert Byte Array to Hex value public class ByteHex { public static void main(String[] args) { byte[] bytes = {10, 2, 15, 11}; for (byte b : bytes) { String…
Read more

Java Program to Convert Array to Set (HashSet) and Vice-Versa

Java Program to Convert Array to Set (HashSet) and Vice-Versa In this program, you’ll learn to convert an array to a set and vice versa in Java. Example 1: Convert Array to Set import java.util.*; public class ArraySet { public static void main(String[] args) { String[] array = {“a”, “b”, “c”}; Set<String> set = new…
Read more

Java Program to Convert Map (HashMap) to List

Java Program to Convert Map (HashMap) to List In this program, you’ll learn different techniques to convert a map to a list in Java. Example 1: Convert Map to List import java.util.*; public class MapList { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, “a”); map.put(2, “b”); map.put(3, “c”); map.put(4,…
Read more

Java Program to Get Current Working Directory

Java Program to Get Current Working Directory In this program, you’ll learn to get the current working directory in Java. Example 1: Get current working directory public class CurrDirectory { public static void main(String[] args) { String path = System.getProperty(“user.dir”); System.out.println(“Working Directory = ” + path); } } When you run the program, the output…
Read more

Java Program to Convert List (ArrayList) to Array and Vice-Versa

Java Program to Convert List (ArrayList) to Array and Vice-Versa In this program, you’ll learn to convert a list to an array using toArray() and array to list using asList() in Java. Example 1: Convert list to array import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ListArray { public static void main(String[] args) { List<String>…
Read more

Java Program to Join Two Lists

Java Program to Join Two Lists In this program, you’ll learn different techniques to join two lists in Java. Example 1: Join Two Lists using addAll() import java.util.ArrayList; import java.util.List; public class JoinLists { public static void main(String[] args) { List<String> list1 = new ArrayList<String>(); list1.add(“a”); List<String> list2 = new ArrayList<String>(); list2.add(“b”); List<String> joined =…
Read more

Java Program to Add Two Dates

Java Program to Add Two Dates In this program, you’ll learn to add two dates in Java using Calendar. Since, Java epoch is 1970, any time represented in a Date object will not work. This means, your Dates will start from 1970 and when two Date objects are added, the sum misses by about 1970…
Read more

Java Program to Convert Milliseconds to Minutes and Seconds

Java Program to Convert Milliseconds to Minutes and Seconds In the above program, you’ll learn to convert milliseconds to minutes and seconds individually, and together in Java. Example 1: Convert milliseconds to minutes and seconds individually import java.util.concurrent.TimeUnit; public class Milliseconds { public static void main(String[] args) { long milliseconds = 1000000; // long minutes…
Read more

Java Program to Get Current Date/TIme

Java Program to Get Current Date/TIme In this program, you’ll learn to get the current date and time in different formats in Java.   Example 1: Get Current date and time in default format import java.time.LocalDateTime; public class CurrentDateTime { public static void main(String[] args) { LocalDateTime current = LocalDateTime.now(); System.out.println(“Current Date and Time is:…
Read more