Found 7442 Articles for Java

Java program to find all duplicate characters in a string

Shriansh Kumar
Updated on 01-Aug-2024 10:46:49

65K+ Views

The duplicate characters in a string are those that occur more than once. In Java, String is a non-primitive datatype that contains one or more characters and is enclosed in double quotes(“ ”). As per the problem statement, we are given a string and our task is to write a Java program to find all the duplicate characters from that string. We can use for loop and nested for loop to solve this given problem. Example Scenario Input: String str = "Apple"; Output: duplicate_char = p; p is a duplicate character as it occurs more than once. Using ... Read More

Java program for removing n-th character from a string

Samual Sam
Updated on 24-Sep-2024 21:44:16

908 Views

In this article, we will learn how to remove the n-th character from a string in Java. The program takes an input string and an index, removes the character at the given index, and returns the modified string and this process will be demonstrated through a simple example, using the substring() method. Problem Statement Write a program in Java that removes the n-th character from a given string. The program will display the original string and the string after the character is removed − Input Original string: Apple Output String with 4th character removed: Appe Steps to remove n-th character from ... Read More

Swapping Characters of a String in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:23

4K+ Views

For swapping characters of a string in Java we can use string builder which is mutable so we need not to take care of new object creation during swapping. In this we would create a method which swap characters of string based on location of swapping characters.This method will take location of swapping characters as its parameters.First store both characters need to be swapped and using set character method of string builder swap the targeted characters. Example Live Demo public class SwapCharacters { public static void main(String[] args) { ... Read More

Swap two variables in one line in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:23

1K+ Views

In order to swap two variable using single expression or in single line we could use bitwise XOR operator of Java. As we now that in Java XOR functions as XOR of two numbers a and b returns a number which has all the bits as 1 wherever bits of a and b differs. So for swapping of two variable we would use this operator as Example Live Demo public class SwapUsingBitwise { public static void main(String[] args) { int a = 8 ; int b = 10; ... Read More

Swap two Strings without using third user defined variable in Java

Arushi
Updated on 30-Jul-2019 22:30:23

281 Views

In order to swap two strings i.e interchange the content of two strings we will use sub string method of string class in Java.First of all get the length of both strings before making any change in any of string.Now modify one string as concatenate both strings and assign to one string. After this use sub string method of String class using begin index as length of new modified string 1 and last index as initial length of string 1.This will give us the swiped string 1 which contain content of string 2. Now to get swiped string 2 again ... Read More

Streams in Java

Fendadis John
Updated on 30-Jul-2019 22:30:23

1K+ Views

Stream is a new abstract layer introduced in Java 8. Using stream, you can process data in a declarative way similar to SQL statements. For example, consider the following SQL statement. SELECT max(salary), employee_id, employee_name FROM Employee The above SQL expression automatically returns the maximum salaried employee's details, without doing any computation on the developer's end. Using collections framework in Java, a developer has to use loops and make repeated checks. Another concern is efficiency; as multi-core processors are available at ease, a Java developer has to write parallel code processing that can be pretty error-prone. To resolve ... Read More

Static methods vs Instance methods in Java

Revathi Satya Kondra
Updated on 28-Jan-2025 11:39:41

3K+ Views

In Java, the behaviour of any variable or method is defined by the keyword used in front of its declaration. One of the non-access modifiers is static, which can be used with both methods and variables. The static methods are defined at the class level and can be accessed without creating an instance of the class, while instance methods require an object of the class for accessibility. Static methods exist as a single copy for the entire class, whereas instance methods exist as multiple copies, depending on the number of instances created for that particular class. Static methods cannot directly ... Read More

Stack in Java Programming

Arushi
Updated on 30-Jul-2019 22:30:23

295 Views

Stack is a subclass of Vector that implements a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own. Stack( ) Apart from the methods inherited from its parent class Vector, Stack defines the following methods − Sr.No. Method & Description 1 boolean empty() Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements. 2 Object peek( ) Returns the element ... Read More

Sorting a HashMap according to values in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:23

687 Views

As we know that Hash map in Java does not maintain insertion order either by key or by order.Also it does not maintain any other order while adding entries to it. Now in order to sort a hash map according to the values mapped to its corresponding keys we first need to get all values of map considering that hash map has unique values only.Now put all the values in a list and sort this list with the comparator or comparable interface of Java. As we get sorted list of unique values now get corresponding keys from the map and ... Read More

Sorting collection of String and StringBuffer in Java

Fendadis John
Updated on 30-Jul-2019 22:30:23

867 Views

In order to sort in Java as we know we can use either Comparable or Comparator interfaces in which we could also define our custom logic to sort.One of the approach to sort is to add the entity in TreeSet or TreeMap which would sort the entries as internally they also uses the comparable interface. Now String class in Java internally implements comparable interface so whenever we add string to a tree set or map it uses comparable logic of string class and sort the input entry strings.But String buffer do not have the implementation of comparable interface so ... Read More

Advertisements