Posts

Usage of map,max,sorted,comparing java 8 methods

  Java 8 Stream Methods: map , max , sorted , and comparing Java 8 introduced powerful functional programming features with its Stream API, allowing developers to write more expressive and readable code. Some of the key methods provided by this API include map , max , sorted , and comparing . map : The map method is used to transform each element of a stream into another form. It takes a function as a parameter, which is applied to each element in the stream, producing a new stream of transformed elements. This is particularly useful for converting data types or extracting certain properties from objects. max : The max method is used to find the maximum element in a stream according to the specified comparator. It returns an Optional containing the maximum element, or an empty Optional if the stream is empty. This method is commonly used for finding the largest value or object based on a specific attribute. sorted : The sorted method is used to sort the elements of a stream. ...

Merging two arrays as alternative elements of third array in java

MergeAlernateElementOfTwoSplittedArrayIntoThirdArray The goal of the program is to merge two arrays into a third array by alternating their elements. 1. Program Overview The program first splits an input array (arr) into two separate arrays (first and second). It then merges these two arrays into a third array (third), alternating elements from each array. Initialization: The main method begins by initializing an integer array arr with 8 elements. It calculates the length len of this array. Two new arrays, first and second, are created to hold the split halves of arr. The first array is of size (len + 1) / 2 to handle both even and odd lengths, ensuring the first half gets the extra element if the length is odd. The second array is the remainder (len - first.length). Splitting the Array: The splitGivenArrayInTwo method is called to divide arr into first and second arrays. Printing the Arrays: The print method is called to display the contents of first and second...

Index of first repeated character in a string.

Let me break down and elaborate on the Java code snippet you've provided. This code is designed to find the first repeated character in a string and then print its index. Code Explanation 1. String Initialization java Copy code String repeadted = "adffda" ; A string repeadted is initialized with the value "adffda" . The word seems like a typo, and it probably meant "repeated." 2. Creating a Set to Track Characters java Copy code Set<Character> set1 = new HashSet <>(); A HashSet named set1 is created to store characters from the string repeadted . The HashSet data structure is chosen because it does not allow duplicate elements, making it perfect for detecting repeated characters. 3. Finding the First Repeated Character java Copy code char ch1 ...

First Repeating character in a string

Please read the post for explanation: Index of first repeated character in a string. System.out.println("First repeating character in a string"); Set set=new HashSet (); "adffda".chars() .filter(c->!set.add((char)c)) .findFirst() .ifPresent(ch->System.out.print((char)ch));

Reverse a String using Recursion

class ReverseAStringInRecusive {          public static String result="";          public static void main(String[] args) {         reverseString("inayathulla");         System.out.println(result);     }          public static String reverseString(String origin){          if(origin.length()>0){          result=result+origin.charAt(origin.length()-1);          return reverseString(origin.substring(0,origin.length()-1));          }          return origin;         }              } =