Posts

Showing posts with the label Java

Java - Convert negative number to positive

In this section, we will write a Java program to convert negative number to positive (absolute value ) using  Math.abs() . public class Main { public static void main ( String []args) { //Example 1 int sum = 2 + 2 + 2 + 2 + Math . abs (- 2 ); System . out .println( "Sum (absolute value) : " + sum ); //Example 2 int num1 =- 9 ; int num2 = Math . abs ( num1 ); System . out .println( num2 ); } } Console Output: Total 1 (absolute value) : 10 9 More related topics, Java - Convert comma-separated String to a List and List to a comma-separated String Java - Convert hyphen-separated String to a List and List to a hyphen-separated String Java - Convert colon-separated String to a List and List to a colon-separated String Java - How to get current timestamps Java - How to count duplicated items in a List Java – How to Convert Integer to Long Java - Program to Find Largest Element in an Array Java - How to loop an enum

Java - How to capitalize the first letter of a String

In this section, we will show you how to capitalize the first letter of a String. In Java, we can use  str.substring( 0 , 1 ).toUpperCase() + str.substring( 1 )  to make the first letter of a String as a capital letter (uppercase letter) String str = "java" ; String cap = str .substring( 0 , 1 ).toUpperCase() + str .substring( 1 ); //cap = "Java" or We can use Apache's common library StringUtils.capitalize(str) API to capitalize first letter of the String. String str = "java" ; String cap = StringUtils . capitalize ( str ); //cap == "Java" 1. str.substring(0,1).toUpperCase() + str.substring(1) A complete Java example to capitalize the first letter of a String. public class Main { public static void main ( String [] args) { System . out .println( capitalize ( "knowledgefactory" )); // Knowledgefactory System . out .println( capitalize ( "java" )); // Java } // with some null an

Java 8 - forEach method that iterates with an index of an Array or List

In this section we will demonstrate how we can use the forEach() method with a specific index of an Array or List . 1. forEach() Method with an Array Index Generate the index with IntStream.range . import java.util.List ; import java.util.stream.Collectors ; import java.util.stream.IntStream ; public class Main { public static void main ( String [] args) { // Creating a list of string String [] list = { "Java" , "Kotlin" , "Go" , "Ruby" }; // Using forEach with index List < String > collect = IntStream . range ( 0 , list . length ) .mapToObj(index -> index + ":" + list [index]) .collect( Collectors . toList ()); collect .forEach( System . out ::println); } } Console Output: 0:Java 1:Kotlin 2:Go 3:Ruby 2. forEach() Method with a List and HashMap Index import java.util.Arrays ; import java.util.HashMap ; import java.util.List ; public class Main {

Java - How to loop an enum

  In this section, we will  write a Java program to loop an enum. Call the .values() method of the enum class to return an array, and loop it with the for loop: for ( Country country : Country . values ()) { System . out .println( country ); } For Java 8, convert an enum into a stream and loop it: Stream . of ( Country . values ()).forEach( System . out ::println); 1. For Loop Enum 1.1 An  enum  to contain a list of the countries: Country.java public enum Country { USA , India , China , Russai , Brazil , France ; } 1.2 To loop over the above  enum   class, just call  .values()  and do a normal for loop Main.java public class Main { public static void main ( String [] args) { for ( Country country : Country . values ()) { System . out .println( country ); } } } Console Output: USA India China Russai Brazil France 2. Java 8 Stream APIs 2.1 Convert an  enum   into a stream and loop it. Main.java import java.util.stream.St

Java - Program to Find Largest Element in an Array

In this section, we will  show you how to find the largest element in an Array. Using Iterative Method Using  Arrays.sort()  Method 1. Using Iterative Method public class Main { public static void main ( final String [] args) { final int [] array = { 1 , 4 , 44 , 5 , 66 , 2 }; largestElement ( array ); } private static int largestElement ( final int [] array){ // Initialize maximum element int max = array[ 0 ]; // Traverse array elements from second and // compare every element with current max for ( int i = 1 ; i < array. length ; i ++){ if (array[ i ] > max ){ max = array[ i ]; } } System . out .println( max ); return max ; } } Console Output: 66 2. Using Arrays.sort() Method import java.util.Arrays ; public class Main { public static void main ( final String [] args) { final int [] array = { 1 , 4 , 44 , 5 , 66 , 2 }; final

Java - How to count duplicated items in a List

In this section, we will show you h ow to count duplicated items in a List,using Collections.frequency and Map . Main.java import java.util.ArrayList ; import java.util.Collections ; import java.util.HashMap ; import java.util.HashSet ; import java.util.List ; import java.util.Map ; import java.util.Set ; import java.util.TreeMap ; public class Main { public static void main ( String [] args) { List < String > list = new ArrayList<>(); list .add( "Java" ); list .add( "Kotlin" ); list .add( "Python" ); list .add( "PHP" ); list .add( "Angular" ); list .add( "Java" ); list .add( "Kotlin" ); list .add( "React" ); list .add( "PHP" ); System . out .println( " \n Example 1 - Count 'Java' with frequency" ); System . out .println( "Java : " + Collections . frequency (

Java - How to get current timestamps

In this section, we will show you examples  to get the current date time or timestamp in Java. 1. Java Timestamp examples The below program uses  java.sql.Timestamp  to get the current timestamp and format the display with  SimpleDateFormat . import java.sql.Timestamp ; import java.text.SimpleDateFormat ; import java.util.Date ; public class Main { // 2023.01.23.00.40.49 private static final SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat( "yyyy.MM.dd.HH.mm.ss" ); // 2023-01-23T00:40:49.761+05:30 private static final SimpleDateFormat simpleDateFormat12 = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" ); // 2023-01-23 00:40:49 private static final SimpleDateFormat simpleDateFormat13 = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); public static void main ( String [] args) { // method 1 Timestamp timestamp = new Timestamp( System . currentTimeMillis ());