Posted on by Kalkicode
Code Java Core

Program to move items from one list to another in Java

Basically this problem is based on how to remove and add items of list collection in java programming language. There can be many possibilities and situation to adding and remove list elements. Such as.

1) Moving all the elements of one list to the last position of another list?.

2) Moving all the elements of one list to the beginning position of another list?.

3) Moving all the elements of one list to the middle position of another list?.

4) Moving the specific element from one list to another list?

And many more questions can be belong to this category. The solution is very simple, you need to understand how to deal list with using of insertion deletion and search operation. This post are providing all possible ways to use list in different java collections.

How to create list

List is an interface in java programming language it can implemented by following classes.

AbstractList
AbstractSequentialList
ArrayList
AttributeList
CopyOnWriteArrayList
LinkedList
RoleList
RoleUnresolvedList
Stack
Vector

This classes are implements list interface functionality. Let's see an example to create instance of this class using list.

import java.util.List; // For List
import java.util.ArrayList; // For ArrayList
import java.util.Vector; // For Vector
import java.util.Stack; // For Stack
// List Creation example
class ListCreation {
    public static void main(String[] args) {
        // Create instance of ArrayList
        // This list is an integer Type
        List < Integer > num = new ArrayList < Integer > ();
        // Create instance of Vector
        // This is an String type
        List < String > name = new Vector < String > ();
        // Create instance of Vector
        // This is an Double type
        List < Double > salary = new Stack < Double > ();
        // etc for other
      
        // Print size
        System.out.println(num.size()); // 0
        System.out.println(name.size()); // 0
        System.out.println(salary.size()); // 0
    }
}

We mentioned few classes which is commonly used in java programming language.

How to Move all the elements of one list to the last position of another list

// Example move list element 1 to another
import java.util.List; // For List
import java.util.ArrayList; // For ArrayList
// List Move example 
class ListMoveExample {
    public static void main(String[] args) {
        // Create first list x
        List < Integer > x = new ArrayList < Integer > ();
        // Element in first element
        x.add(10);
        x.add(20);
        x.add(30);
      
        // Create second list y
        List < Integer > y = new ArrayList < Integer > ();
        // Some element in y list (may be alredy)
        y.add(100);
      
        // Add all elements from a x list to y list
        y.addAll(x);
      
        // Remove all element from x list
        x.clear();
      
        // Display list
        System.out.println(x); // []
        System.out.println(y); // [100, 10, 20, 30]
    }
}

Moving all the elements of one list to the beginning position of another list

// Example move list element 1 to another
import java.util.List; // For List
import java.util.ArrayList; // For ArrayList
// List Move example 
class ListMoveExample {
    public static void main(String[] args) {
        // Create first list x
        List < Integer > x = new ArrayList < Integer > ();
        // Element in first element
        x.add(10);
        x.add(20);
        x.add(30);
        // Create second list y
        List < Integer > y = new ArrayList < Integer > ();
        // Some element in y list (may some element is alredy exist)
        y.add(100);
        // Add element at the first place of list
        for (int value: x) {
            // Insert element at the beginning
            // Here zero indicates index
            y.add(0, value);
        }
        // Remove all element from x list
        x.clear();
        // Display list
        System.out.println(x); // []
        System.out.println(y); // [30, 20, 10, 100]
    }
}

Here we are use a loop to iterating list element. You can add list element at the end and reverse resultant list by using of collection.reverse(list) method. For example.

// Example move list element 1 to another
import java.util.List; // For List
import java.util.ArrayList; // For ArrayList
import java.util.Collections;
// List Move example 
class ListMoveExample {
    public static void main(String[] args) {
        // Create first list x
        List < Integer > x = new ArrayList < Integer > ();
        // Element in first element
        x.add(10);
        x.add(20);
        x.add(30);
      
        // Create second list y
        List < Integer > y = new ArrayList < Integer > ();
        // Some element in y list (may be alredy)
        y.add(100);
      
        // Add all elements from a x list to y list
        y.addAll(x);
        
        // Important 
        Collections.reverse(y);
      
        // Remove all element from x list
        x.clear();
      
        // Display list
        System.out.println(x); // []
        System.out.println(y); // [30, 20, 10, 100]
    }
}

Moving all the elements of one list to the middle position of another list

// Example move list element 1 to another
import java.util.List; // For List
import java.util.ArrayList; // For ArrayList
// List Move example 
class ListMoveExample {
    public static void main(String[] args) {
        // Create first list x
        List < Integer > x = new ArrayList < Integer > ();
        // Element in first element
        x.add(10);
        x.add(20);
        x.add(30);
        // Create second list y
        List < Integer > y = new ArrayList < Integer > ();
        // Some element in y list (may some element is alredy exist)
        y.add(100);
        y.add(200);
        // Add element at the middle place of list
        for (int value: x) {
            // Insert element at the beginning
            // Here y.size()/2 indicates index
            y.add(y.size()/2, value);
        }
        // Remove all element from x list
        x.clear();
        // Display list
        System.out.println(x); // []
        System.out.println(y); // [100, 20, 30, 10, 200]
    }
}

Moving the specific element from one list to another list?

A specific element satisfies a condition. Suppose we want to move only even element to another list. And some other condition can be possible.

// Example move list element 1 to another
import java.util.List; // For List
import java.util.ArrayList; // For ArrayList
// List Move example 
class ListMoveExample {
    public static void main(String[] args) {
        // Create first list x
        List < Integer > x = new ArrayList < Integer > ();
        // Element in first element
        x.add(11);
        x.add(20);
        x.add(30);
        x.add(31);
        x.add(62);
        // Create second list y
        List < Integer > y = new ArrayList < Integer > ();
        int index = 0;
        while (index < x.size()) {
            if (x.get(index) % 2 == 0) {
                y.add(x.get(index));
                x.remove(index);
            } else {
                index += 1;
            }
        }
        // Display list element
        System.out.println(x); // [11, 31]
        System.out.println(y); // [20, 30, 62]
    }
}

Conclusion

1) Add a list element by using add() method.

2) Remove all elements by using of clear() method.

3) Remove a specific position element by using of remove(index) method.

4) After moving all the elements in one list to the second list, all the elements of the first list are removed.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment