Posts

Showing posts with the label Java

How to convert a List of String to a comma separated String in Java

In this section, we will show you how to  convert a List of String to a comma separated String in Java. 1. List 1.1 Using String.join() join (CharSequence delimiter, Iterable<? extends CharSequence> elements) : This method is useful when we have to join iterable elements. Some of the common Iterables are – List, Set, Queue, and Stack. import java.util.Arrays ; import java.util.List ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "Java" , "Kotlin" , "Python" , "C" ); String result = String . join ( "," , list ); System . out .println( result ); } } Console Output: Java,Kotlin,Python,C 1.2 Stream Collectors.joining() joining(CharSequence delimiter) :We pass a delimiter to get back a Collector, which concatenates the elements separated by the specified delimiter. import java.util.Arrays ; import java.util.List ; import jav

Java 8 Stream – How to sort a List with stream.sorted()

In this section, we will show you how to sort list using stream.sorted() in Java 8. 1. List 1.1 Sort a List in natural order. import java.util.Arrays ; import java.util.Comparator ; import java.util.List ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < String > list = Arrays . asList ( "8" , "B" , "A" , "1" , "Z" , "Y" , "2" , "b" , "d" ); List < String > sortedList1 = list .stream() .sorted( Comparator . naturalOrder ()) .collect( Collectors . toList ()); System . out .println( sortedList1 ); List < String > sortedList2 = list .stream() .sorted((o1,o2)-> o1.compareTo(o2)) .collect( Collectors . toList ()); System . out .println( sortedList2 ); List < String > sortedList3 = list .stream(). sorted().

How to convert an int to a String in Java

In this section, we will show you how to convert an int to a String in Java. Following ways can be used for converting int to String: 1. By using  Integer.toString()  method 2. By using String.valueOf()  method  Note :  Integer.toString() method is faster than String.valueOf(). String.valueOf() method internally invokes the Integer.toString() method. public static String valueOf ( int i) { return Integer . toString (i); } Example 1: Using Integer.toString() method The java.lang.Integer.toString() is an inbuilt method in Java which is used to returns the string object of the particular Integer value. By default, the argument is converted to signed decimal (radix 10) in string format. public class Main { // Driver code public static void main ( String [] args) { int val = 9 ; String str = Integer . toString ( val ); System . out .println( "String => " + str ); } } Console Output: String => 9 Example 2: Using String.valueOf() The j

How to replace a character at a specific index in a String in Java

In this section, we will show you how replace a character at a specific index in a String in Java. Note: String is an immutable class in java.  That means we cannot make any change in the String object.  Any method which seems to modify it always returns a new string object with modification.  Following ways can be used to replace a character at a specific index in a String in Java: By u sing substring () method By using StringBuilder setCharAt() method By using StringBuffer setCharAt() method By using toCharArray() method Example 1: Using substring () method Create a new string with the character replaced. public class Main { // Driver code public static void main ( String [] args) { // Get the String String string = "How ate you?" ; // Get the index int index = 5 ; // Get the character char ch = 'r' ; // Print the original string System . out .println( "Original String = " + s