Meeting Maximum Guests Problem – Java implementation

    Imagine a party. Many guests are coming. You want to calculate the maximum number of guests in the party hall at a particular time given to you.

How will you do this????

First, Get the inputs. The inputs are two array of integers given below.

‘arrive[]’ – This represents the guests who enters the party hall at that time.

‘depart[]’ – This represents the guests who leaves the party hall at that moment.

Note:

Always check both array’s length should be same in size.

Arrival entry should be entered in ‘arrive[]’ and Departure entry should be noted in ‘depart[]’.

Algorithm:

  • Get the inputs ‘arrive[]’ and ‘depart[]’;
  • Sort the arrays.
  • Let us declare two pointers ‘i’ and ‘j’. ‘i’ points arrive[] and ‘j’ points depart[].
  • Call the function finditMaxGuests()
  • This function check the condition to proceed.
  •   If arrive[i]<=depart[j] then, a guest came into the party hall. Increase the count of arrive[].
  •   Otherwise, decrease the count of depart and move on.
  • Finally, print the value of maximum guests in the party at a particular time.

Here, is the program.

import java.util.Arrays;

import java.util.Scanner;

public class MeetingMaxGuestsEg {

    public static void main(String[] args) {

         Scanner scanner1 = new Scanner(System.in);

          System.out.print("Enter number of guests arrived: ");

          int no = scanner1.nextInt();

          int[] arrive = new int[no];

          for (int i = 0; i < no; i++)

          {

           System.out.print("Enter the next one: ");

           arrive[i] = scanner1.nextInt();

          }

          Scanner scanner2 = new Scanner(System.in);

          System.out.print("Enter number of guests leaved: ");

          int no1 = scanner2.nextInt();

          int[] depart = new int[no];

          for (int i = 0; i < no1; i++)

          {

           System.out.print("Enter the next one: ");

           depart[i] = scanner2.nextInt();

          }

        System.out.println("Maximum number of guests at the party: " + finditMaxGuests(arrive, depart));

    }

     public static int finditMaxGuests(int[] arrive, int[] depart) {

        Arrays.sort(arrive);

        Arrays.sort(depart);

        int mGuests = 0;

        int cGuests = 0;

        int i = 0, j = 0;

        while (i < arrive.length && j < depart.length) {

            if (arrive[i] <= depart[j]) {

                cGuests++;

                mGuests = Math.max(mGuests, cGuests);

                i++;

            } else {

                cGuests--;

                j++;

            }

        }

        return mGuests;

    }

}

Output:

C:\raji\blog>javac MeetingMaxGuestsEg.java

C:\raji\blog>java MeetingMaxGuestsEg

Enter number of guests arrived: 10

Enter the next one: 1

Enter the next one: 3

Enter the next one: 5

Enter the next one: 7

Enter the next one: 9

Enter the next one: 11

Enter the next one: 15

Enter the next one: 2

Enter the next one: 4

Enter the next one: 6

Enter number of guests leaved: 4

Enter the next one: 6

Enter the next one: 3

Enter the next one: 45

Enter the next one: 34

Maximum number of guests at the party: 2

That’s all. The java program to find the maximum guests in the party is clearly written. Keep coding!!!

Different ways of Sorting using built in functions-Java Programming

     Sorting is a process of arranging elements in particular order. It may be ascending and descending order.

Here, two examples are given to sort the elements using built in functions.

  • ·       Arrays.sort()
  • ·       Custom comparators

Java Program to sort integers using ‘Arrays.sort()’:

              This program reads the integer array from user. It process it using ‘Arrays.sort()’ function.

Finally, it prints the sorted integers.

Program:

   import java.util.Arrays;

   import java.util.Collections;

   import java.util.Scanner;

   public class SortEg1 {

       public static void main(String[] args) {

          Scanner scanner = new Scanner(System.in);

          System.out.print("Enter number of elements: ");

          int no = scanner.nextInt();

          int[] integerArray = new int[no];

          for (int i = 0; i < no; i++)

          {

           System.out.print("Enter element: ");

           integerArray[i] = scanner.nextInt();

          }

           Arrays.sort(integerArray);

           System.out.println(Arrays.toString( integerArray));

       }

   }

Output:

C:\raji\blog>javac SortEg1.java

C:\raji\blog>java SortEg1

Enter number of elements: 5

Enter element: 45

Enter element: 56

Enter element: 9

Enter element: 23

Enter element: 34

[9, 23, 34, 45, 56]

This is a normal sorting.

If you want to sort the data in reverse manner, let us use the custom comparators.

Java Program to sort the string elements in reverse order:

              This program reads the number of String elements and input strings from the user. Using the custom comparator, it sorts in reverse order.

At last, it prints the data.

Program:

   import java.util.Arrays;

   import java.util.Comparator;

   import java.util.Scanner;

   public class CustomSortEg {

       public static void main(String[] args) {

          Scanner scanner = new Scanner(System.in);

          System.out.print("Enter number of Strings: ");

          int no = scanner.nextInt();

          String[] a = new String[no];

          for (int i = 0; i < no; i++)

          {

           System.out.print("Enter element: ");

           a[i] = scanner.next();

          }

           Arrays.sort(a, Comparator.reverseOrder());

           System.out.println("Sorting in reverse order");

           System.out.println(Arrays.toString(a));

       }

   }

Output:

C:\raji\blog>javac CustomSortEg.java

C:\raji\blog>java CustomSortEg

Enter number of Strings: 5

Enter element: car

Enter element: van

Enter element: bike

Enter element: truck

Enter element: bus

Sorting in reverse order

[van, truck, car, bus, bike]

That’s all. These are the ways to sort the elements using built in functions.

Java Program to create a List and perform functions using Collections

    A set of elements grouped together under a common name is called a collection. To implement this, a package “java.util” is used. This package has many static methods and operates the collections.

Collections has some set of operations like reverse, search and sorting. Let us list the some of methods in the ‘Collections’ class.

Java Program to create a List and perform functions using Collections:

This program starts by importing the header files and has following steps.

Steps:

  • A public class is created with main() function.
  • A list object list1 is created as integer ArrayList.
  • To insert the values, use add() function.
  • Print the newly created list.
  • Now, sort the elements by sort() function using Collections.
  • Print the sorted list.
  • To search an element using binary search, a function binarySearch() is used.
  • It display the key position as output.
  • Next,the list is reversed using reverse() function.
  • The list is printed after the reversal.
  • To shuffle the data at random, a function shuffle() is used.
  • Finally, the list is displayed after shuffled.

Program:

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class CollEg1 {

    public static void main(String[] args) {

        // A list is created here

        List<Integer> list1 = new ArrayList<>();     

        // Let us add the elements to the list

        list1.add(43);

        list1.add(81);

        list1.add(25);

        list1.add(12);

        list1.add(56);

        System.out.println("The original list is:" + list1);

        //Sorting the list using collections

        Collections.sort(list1);

        // Display the list

        System.out.println("The Sorted List is: " + list1);

        //Searching an element using Binary Search

        int a =Collections.binarySearch(list1,25);

        System.out.println("The key is at Position :"+ a);

      //Reverse the list

        Collections.reverse(list1);

        //Print the list after reverse

        System.out.println("The Reversed List is: " + list1);

        //Shuffle the list

        Collections.shuffle(list1);

        //Print the list after shuffling

        System.out.println("The shuffled List is: " + list1);

            }

}

Output:

C:\raji\blog>javac CollEg1.java

C:\raji\blog>java CollEg1

The original list is:[43, 81, 25, 12, 56]

The Sorted List is: [12, 25, 43, 56, 81]

The key is at Position :1

The Reversed List is: [81, 56, 43, 25, 12]

The shuffled List is: [81, 25, 43, 56, 12]

This is the simple way of creating a list and perform operations using Collections. Hope you understand it. Keep coding!!!

Java Program to implement ‘Arrays’ and its member functions.

     Array has collection of elements. ‘Arrays’ is a built in class in java package java.util. This class contains many static methods to process array operations.

Some of the methods are listed below.

sort() :This function sorts the array elements.

parrelSort() : Suits for larger array to sort the elements parallel.

equals():It compares two arrays for equality.

binarySearch(): It uses binary Search method to find an element.

fill() :it fills a data for set of elements.

toString(): It converts the array to String

asList():It converts the array to List.

Stream():it is used for Stream operations.

copyOf(): It copys the array elements into another array.

copyOfRange(): it copys the data in the specified range and add to another array.

setAll(): All the elements are set by this method.

An example is given below.

#Java Program to implement ‘Arrays’ and its member functions.

import java.util.Arrays;

import java.util.Scanner;

public class ArrayEg3 {

    public static void main(String[] args) {

        Scanner scanner1 = new Scanner(System.in);

        //get the size of the array

        System.out.print("Please,Enter the size of the array: ");

        int size = scanner1.nextInt();

        // An array with specified size is created here

        int[] sample = new int[size];

        // Read the data elements by for loop

        System.out.println("Enter " + size + " elements:");

        for (int i = 0; i < size; i++) {

            sample[i] = scanner1.nextInt();

        }

       // Sort the data elements using sort()

        Arrays.sort(sample);

        System.out.println("Sorted: " + Arrays.toString(sample));

       // binarySearch() method of searching

        int index = Arrays.binarySearch(sample, 7);

        System.out.println("Index of 7: " + index);

       // Let us check for equality of two arrays

        int[] anotherArray = {11, 24, 45, 7, 19};

        boolean areEqual = Arrays.equals(sample, anotherArray);

        if(areEqual)

           System.out.println("Arrays are equal");

        else

           System.out.println("Arrays are not equal");

       // Fill the data with a data

        int[] fArray = new int[5];

        Arrays.fill(fArray, 12);

        System.out.println("The array after Filled data: " + Arrays.toString(fArray));

    }

}

Output:

C:\raji\blog>javac ArrayEg3.java

C:\raji\blog>java ArrayEg3

Please,Enter the size of the array: 7

Enter 7 elements:

23

4

67

56

34

98

87

Sorted: [4, 23, 34, 56, 67, 87, 98]

Index of 7: -2

Arrays are not equal

The array after Filled data: [12, 12, 12, 12, 12]

This is the way of using ‘Arrays’ in java. Enjoy Coding!!!!!

Comparable Interface in java for handling Employee data

 If you want to define the natural ordering of objects, use the ‘Comparable’ interface.

Syntax is given below…

Public interface Comparable <T> }

  Member variables

  Member function()1 {  ….. }

  ………………………………………..

 Member Functionn {……..}

Let us create a Employee list with name and id using this interface.

  • className: Employee
  • Constructor: Employee()
  • Overridden function:
  • compareTo() and toString()

Steps to follow:

  • A list ‘Employee’ is created using ArrayList.
  • In the constructor, assign the values for id and Name.
  • CompareTo – Comparing o lists
  • ToString -converts to string.
  • In side main() function, add the values to the Employee List.
  • Finally,display it.

Program:

import java.util.*;

// class creation with memeber functions and variables

class Employee implements Comparable<Employee> {

    String name;

    int id;

    //Constructor

    public Employee(String name, int id) {

        this.name = name;

        this.id = id;

    }

    // overriding methods

    @Override

    public int compareTo(Employee offline) {

        return this.id - offline.id;

    }

    @Override

    public String toString() {

        return name + ": " + id;

    }

    //main() function

    public static void main(String[] args) {

        List<Employee> emp = new ArrayList<>();

        emp.add(new Employee("Ashok", 02));

        emp.add(new Employee("Karthik", 14));

        emp.add(new Employee("Balu", 05));

        emp.add(new Employee("Sunny",56));

        emp.add(new Employee("Ranjith", 28));

                Collections.sort(emp);

        for (Employee emp1 : emp) {

            System.out.println(emp1);

        }

    }

}

Output:

C:\raji\blog>javac Employee.java

C:\raji\blog>java Employee

Ashok: 2

Balu: 5

Karthik: 14

Ranjith: 28

Sunny: 56

This is the simple way of implementing ‘Comparable’ interface in java.

Java Program to check for palindrome.

    A set of characters which is magical like these. If you read from right side or left side, it is similar. These set of characters are called palindrome.

Eg: Noon, refer, level, radar, mom, madam and so on.

Let us create a java program to this concept.

Input: String.

Output: The given string is palindrome or not.

Logic:

  • ·       User enters the input String.
  • ·       A user defined function “isitPalindrome()” gets this input.
  • ·       isitPalinfrome():
  • ·       Here, left and right sides of string is represented as ‘left’ and ‘right’.
  • ·       Both are assigned with values.
  • ·       Using a while loop, a set of statements are repeated until ‘left’ is less than ‘right’.
  • ·       If the left character is not equal to right character, then return false.
  • ·       Otherwise, continue the process. Increase the ‘left’ value by 1 and decrease the ’right’ value by 1.
  • ·       Check for all characters in  left and right, return true when all of these are equal.
  • ·       Main():
  • ·       Read the input string from the user.
  • ·       Call the function isitPalindrome().
  • ·       Check the return value from the function. if it is true, print the string is a palindrome.
  • ·       Else, print the string is not a palindrome.

Program:

import java.util.Scanner;

public class PalindromeCheckEg {

    public static boolean isitPalindrome(String str1) {

        int left = 0;

        int right = str1.length() - 1;

         while (left < right) {

            if (str1.charAt(left) != str1.charAt(right)) {

                return false;

            }

            left++;

            right--;

        }

         return true;

    }

     public static void main(String[] args) {

        System.out.println("Java Program to check for palindrome");

        Scanner myObj1 = new Scanner(System.in);

        System.out.println("Enter the input String");

        String input = myObj1.nextLine();

        if (isitPalindrome(input)) {

            System.out.println(input + " is a palindrome.");

        } else {

            System.out.println(input + " is not a palindrome.");

        }

    }

}

Output:

C:\raji\blog>javac PalindromeCheckEg.java

C:\raji\blog>java PalindromeCheckEg

Java Program to check for palindrome

Enter the input String

level

level is a palindrome.

C:\raji\blog>java PalindromeCheckEg

Java Program to check for palindrome

Enter the input String

program

program is not a palindrome.

Java Program to find the largest prime factor

     A number which is divided by itself and 1 is called Prime number. Prime factors consist of prime numbers which are the multiplied to generate the original number.

Eg:

45 =3X3X5

Largest prime factor is5.

Let us implement this concept in java.

Steps to follow:

  • A public class LargestPrimeFactorEg is generated with constructor LargestPrimeFactorEg().
  • The constructor gets an input as the number.
  • Two long variables are declared with initial values.
  • One is for largestfactor(largeFactor) and another one is for divisor(div).
  • A while loop is created with the condition the number is greater than one.
  • Using % operator,the remainder is checked with zero. If so, largefactor is set to divisor value.
  • The no value is divided by divisor and stored it in no.
  • Again, in the another while loop,no % divisor value is equal to zero, no is divided by divisor and it is assigned to no.
  • Increment the divisor value by 1.
  • Repeat this process until the condition fails.
  • Finally return the largest factor value to the main function.
  • In the main() function, read the input as no from the user.
  • Call the largestPrimeFactorEg() function.
  • Get the returned value from the function and print it to the output screen.

Program:

import java.util.Scanner;

//public class with constructor

public class LargestPrimeFactorEg {

    public static long largestPrimeFactorEg(long no) {

        long largeFactor = 1;

        long div = 2;

        while (no > 1) {

            if (no % div == 0) {

                largeFactor = div;

                no /= div;

                while (no % div == 0) {

                    no /= div;

                }

            }

            div++;

        }

        return largeFactor;

    }

//main() function

    public static void main(String[] args) {

        Scanner myObj1 = new Scanner(System.in);

        System.out.println("Enter the number");

        long no = myObj1.nextLong();

        System.out.println("The Largest prime factor of the given number: " + largestPrimeFactorEg(no));

    }

}

Output:

C:\raji\blog>javac LargestPrimeFactorEg.java

C:\raji\blog>java LargestPrimeFactorEg

Enter the number

56

The Largest prime factor of the given number: 7

C:\raji\blog>java LargestPrimeFactorEg

Enter the number

765

The Largest prime factor of the given number: 17

This is the java Program to find the largest prime factor. Keep coding!!!!

Java implementation of anagram checking among Strings

    A word can be created from the characters of another words. That is called anagram. The word formation contains any combination of letters from other word. It may be scrambled or some phrases can be repeated.

Let us check the given strings are anagram or not in java programming.

Logic:

isitAnagram() function:

  • First, check for both strings length.
  • Sort the characters in both arrays.
  • Check whether both arrays are equal or not and return to the main() function.

main() function:

  • Read two input strings from the user at run time.
  • Call the isitAnagram() function.
  • Receives the output from function and display the output.

Java Program to check for anagram:

import java.util.Arrays;

import java.util.Scanner;

//public class is created with member function isitAnagram()

public class AnagramCheckEg {

    public static boolean isitAnagram(String st1, String st2) {

        if (st1.length() != st2.length()) {

            return false;

        }

        char[] chArray1 = st1.toCharArray();

        char[] chArray2 = st2.toCharArray();

        //Character arrays are sorted

        Arrays.sort(chArray1);

        Arrays.sort(chArray2);

        return Arrays.equals(chArray1, chArray2);

    }

 //main() function

    public static void main(String[] args) {

        Scanner myObj1 = new Scanner(System.in);

        System.out.println("Enter the first String");

        String string1 = myObj1.nextLine();

        Scanner myObj2 = new Scanner(System.in);

        System.out.println("Enter the second String");

        String string2 = myObj2.nextLine();

       //Function call

        if (isitAnagram(string1, string2)) {

            System.out.println("Yes. "+string1 + " and " + string2 + " are anagrams.");

        } else {

            System.out.println("No. "+string1 + " and " + string2 + " are not anagrams.");

        }

    }

}

Here is the output while executing the above program.

Output1:

C:\raji\blog>javac AnagramCheckEg.java

C:\raji\blog>java AnagramCheckEg

Enter the first String

java

Enter the second String

Programming

No. java and Programming are not anagrams.

Output2:

C:\raji\blog>javac AnagramCheckEg.java

C:\raji\blog>java AnagramCheckEg

Enter the first String

night

Enter the second String

thing

Yes. night and thing are anagrams.

This is the java implementation of anagram checking among strings.Keep coding!!!!

Pattern searching in a String - Java Program

    Patterns are some sequences of characters. Let us consider this as String. This program is developed in java. To search the pattern in the string, it follows a user defined function.

Program Logic:

  • A string and a pattern are given as input.
  • Both of the input’s length is found and assigned to variables.
  • Using these two length, two “for loops” are written and check each character in the two inputs.
  • When the input string meets the pattern, it stops.
  • Otherwise, it repeats the process until the last character.

Steps to develop the program:

  • Include the built in package java.util.Scanner.
  • A public class PatternSearchingPgm is written with main() class.
  • It uses the above logic in search () function.
  • Inside the main() function,two  objects are created for Scanner class.
  • These two objects read the input string and pattern.
  • Call the search() function with these two inputs.
  • Print the output.

Program:

import java.util.Scanner;

public class PatternSearchingPgm {

    public static void search(String iotext, String pattern1) {

        int textLength = iotext.length();

        int patternLen = pattern1.length();

        //for loops to search the pattern in the String

        for (int i = 0; i <= textLength - patternLen; i++) {

            int j;

            for (j = 0; j < patternLen; j++) {

                if (iotext.charAt(i + j) != pattern1.charAt(j)) {

                    break;

                }

            }

             if (j == patternLen) {

                System.out.println("Yes. The Pattern is found at index " + i);

            }

        }

    }

    public static void main(String[] args) {

       //myObj1,myObj2 are created to read the input String and pattern from the user.

        Scanner myObj1 = new Scanner(System.in);

        System.out.println("Enter the String");

        String iotext = myObj1.nextLine();

        Scanner myObj2 = new Scanner(System.in);

        System.out.println("Enter the pattern");

        String pattern1 = myObj2.nextLine();

        //Function call

        search(iotext, pattern1);

    }

}

Output:

C:\raji\blog>javac PatternSearchingPgm.java

C:\raji\blog>java PatternSearchingPgm

Enter the String

Java World Welcomes You!!!

Enter the pattern

World

Yes. The Pattern is found at index 5

This is the simple way of creating pattern searching program in java. Happy Coding!!!

How to write Pangram checker program in java using String?

     Pangram is a sentence which has all English alphabets at least once in it. Some of the examples of pangram sentences are given below.

  • ·       Brown jars prevented the mixture from freezing too quickly
  • ·       How vexingly quick daft zebras jump
  • ·       The quick brown fox jumps over a lazy dog

Let us create this program in java using String.

Steps:

  • ·       Import the built in packages java.util.HashSet,java.util.Set,java.util.Scanner.
  • ·       Create a class PangramCheckeralphabet.
  • ·       Member function: isitPangram ()
  • ü  This function takes the input as sentence to be checked.
  • ü  It checks the given sentence is null or not. If it is not null,then proceed the function.
  • ü  Create an object for HashSet.
  • ü  Check the sentence for all alphabets at least once.
  • ü  Return the number.
  • ·       Main() function:
  • ü  It creates a scanner object.
  • ü  Read the input from the user and pass it to isitPangram() function.
  • ü  Get the value from the function. if it is true, then print it is a pangram sentence.
  • ü  Otherwise, print it is not pangram sentence.

Program:

import java.util.HashSet;

import java.util.Set;

import java.util.Scanner;

//Creating a public class

public class PangramCheckeralphabet {

    public static boolean isitPangram(String asentence) {

        if (asentence == null) {

            return false;

        }

        Set<Character> alphabetSet = new HashSet<>();

        for (char c : asentence.toLowerCase().toCharArray()) {

            if (c >= 'a' && c <= 'z') {

                alphabetSet.add(c);

            }

        }

        return alphabetSet.size() == 26;

    }

//Main function

    public static void main(String[] args) {

        Scanner myObj1 = new Scanner(System.in);

        System.out.println("Enter the sentence to check for pangram");

        String asentence = myObj1.nextLine();

        if (isitPangram(asentence)) {

            System.out.println("Yes.It is a pangram sentence.");

        } else {

            System.out.println("No. It is not a pangram sentence.");

        }

}

}

Output:   

There are two outputs given below.

C:\raji\blog>javac PangramCheckeralphabet.java

C:\raji\blog>java PangramCheckeralphabet

Enter the sentence to check for pangram

Brown jars prevented the mixture from freezing too quickly

Yes.It is a pangram sentence.

 

C:\raji\blog>java PangramCheckeralphabet

Enter the sentence to check for pangram

It is a new sentence

No. It is not a pangram sentence.

This is one of the efficient way of creating pangram checker program in java. Try this with your input sentence and enjoy coding!!!

String, StringBuilder and StringBuffer-An Introduction

               A Collection of characters forms a String. The characters may be alphabets, special characters or numbers too.

Some of the built-in classes are

  • ·       String
  • ·       StringBuilder
  • ·       StringBuffer

Common methods for each class are given below.

String:

              String is a sequence of characters which are immutable. To create an object using String literally, use the below code.

String a =” Welcome”;

If you want to create a string object using “new” keyword, just follow the code given as follows.

String a = new String (“Welcome”);

If you are using character array, this code helps you.

char[] cA = { 'W', 'e', 'l', 'c', 'o',’m’,’e’ };

String a = new String(cA);

Let us code an example for Byte Array.

Byte[] bA = {87,101,108,99,111,109,101}; //ASCII value for welcome

String b = new String (bA);

Common methods for String:

import java.lang.String.*;

class StringEg

{

 public static void main(String args[])

 {

  String a = new String ("Welcome to the world of Java");

  System.out.println("The String is:"+a);

  System.out.println("The length of the String is:"+a.length());

  System.out.println("The 3rd position character in the String is:"+a.charAt(3));

 

  System.out.println("The substring index is:"+a.indexOf("world"));

  System.out.println("The String in lowercase is:"+a.toLowerCase());

  System.out.println("The String in Uppercase is:"+a.toUpperCase());

  System.out.println("The concatenated string is:"+a.concat("Programming "));

  }

}

Output:

C:\raji\blog>javac StringEg.java

C:\raji\blog>java StringEg

The String is:Welcome to the world of Java

The length of the String is:28

The 3rd position character in the String is:c

The substring index is:15

The String in lowercase is:welcome to the world of java

The String in Uppercase is:WELCOME TO THE WORLD OF JAVA

The concatenated string is:Welcome to the world of JavaProgramming

Note: String is thread safe.

StringBuilder:

              This string content can be modified. It has mutable sequence of characters. The methods in this ‘StringBuilder’ is given below.

Methods:

Insert(): it inserts the given string into the specified postion.

Delete(): it removes a specified sub string.

Append(): This function appends the given string into the string.

Reverse():This reverses the entire string.

toString(): It converts the StringBuilder into String.

Note: It is mutable but not thread safe. Suitable for single thread operations.

StringBuffer:

              It handles multi thread in safe manner. The common methods are listed below.

Methods:

toString(): This function converts the StringBuffer into String.

Append():It adds the given string into StringBuffer.

Reverse():The entire String is reversed.

Insert(): It adds the string into specific position.

Delete():It removes the set of characters.

Replace(): It replaces the set of characters.

It is useful for multi-threaded environments.