Introduction
Swapping two variables is a fundamental concept in programming. It becomes a bit more interesting when applied to strings, as strings are immutable in Java. While the traditional approach uses a temporary variable, Java 8 allows us to explore more elegant ways of performing this swap. In this guide, we’ll demonstrate how to swap two strings using both the traditional method and Java 8 Streams.
Problem Statement
The task is to create a Java program that:
- Accepts two string variables.
- Swaps the values of these two strings.
- Outputs the strings after the swap.
Example:
- Input: String1 = "Hello",String2 = "World"
- Output: String1 = "World",String2 = "Hello"
Solution Steps
- Initialize Strings: Start with two string variables containing different values.
- Swap the Strings: Use either the traditional approach with a temporary variable or explore alternative methods using Java 8 features.
- Display the Result: Print the strings after they have been swapped.
Java Program
Java 8: Swap Two Strings Using Traditional Method
/**
 * Java 8: Swap Two Strings Using Traditional Method
 * Author: https://www.rameshfadatare.com/
 */
public class SwapStringsTraditional {
    public static void main(String[] args) {
        // Step 1: Initialize two strings
        String str1 = "Hello";
        String str2 = "World";
        // Step 2: Display the strings before swapping
        System.out.println("Before Swap:");
        System.out.println("String 1: " + str1);
        System.out.println("String 2: " + str2);
        // Step 3: Swap the strings using a temporary variable
        String temp = str1;
        str1 = str2;
        str2 = temp;
        // Step 4: Display the strings after swapping
        System.out.println("After Swap:");
        System.out.println("String 1: " + str1);
        System.out.println("String 2: " + str2);
    }
}
Java 8: Swap Two Strings Using List
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
 * Java 8: Swap Two Strings Using List
 * Author: https://www.rameshfadatare.com/
 */
public class SwapStringsUsingList {
    public static void main(String[] args) {
        // Step 1: Initialize two strings
        String str1 = "Hello";
        String str2 = "World";
        // Step 2: Display the strings before swapping
        System.out.println("Before Swap:");
        System.out.println("String 1: " + str1);
        System.out.println("String 2: " + str2);
        // Step 3: Swap the strings using a List and Collections.swap()
        List<String> stringList = Arrays.asList(str1, str2);
        Collections.swap(stringList, 0, 1);
        str1 = stringList.get(0);
        str2 = stringList.get(1);
        // Step 4: Display the strings after swapping
        System.out.println("After Swap:");
        System.out.println("String 1: " + str1);
        System.out.println("String 2: " + str2);
    }
}
Explanation of the Programs
- 
Traditional Method: The first example shows the traditional way to swap two strings using a temporary variable. This is straightforward and effective. 
- 
Using Java 8 Features: The second example uses a Listcombined withCollections.swap(). This method is a bit more advanced and leverages the power of Java’s collection framework. It’s especially useful when you want to swap elements in a list, but it can also be applied to individual string variables.
Output Example
For both methods, the output will be:
Before Swap:
String 1: Hello
String 2: World
After Swap:
String 1: World
String 2: Hello
Advanced Considerations
- 
Immutability of Strings: Strings in Java are immutable, meaning the original strings remain unchanged after swapping. When we assign new values to the variables, we are actually pointing them to different memory locations rather than modifying the original strings. 
- 
Swapping More Than Two Variables: While this guide focuses on swapping two strings, the concept can be extended to multiple variables. However, with more variables, the traditional method might become cumbersome, making the use of data structures like lists more appealing. 
- 
Performance Considerations: The traditional method is generally faster and more direct. The use of collections introduces additional overhead, but it also adds flexibility, especially in more complex scenarios. 
Conclusion
This Java 8 guide shows two methods for swapping strings: the traditional approach using a temporary variable and a more advanced method using Java 8’s collections framework. While the traditional method is quick and effective, leveraging Java 8 features can lead to more versatile and flexible solutions. Depending on the context, you can choose the method that best suits your needs.