Java Programming Exercises

Java Collection: Exercises, Practice, Solution

Java Collection Exercises [74 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

Java Collection: ArrayList Exercises [20 exercises with solution]

1. Write a Java program to create a new array list, add some colors (string) and print out the collection. Go to the editor
Click me to see the solution

2. Write a Java program to iterate through all elements in a array list. Go to the editor
Click me to see the solution

3. Write a Java program to insert an element into the array list at the first position. Go to the editor
Click me to see the solution

4. Write a Java program to retrieve an element (at a specified index) from a given array list. Go to the editor
Click me to see the solution

5. Write a Java program to update specific array element by given element. Go to the editor
Click me to see the solution

6. Write a Java program to remove the third element from a array list. Go to the editor
Click me to see the solution

7. Write a Java program to search an element in a array list. Go to the editor
Click me to see the solution

8. Write a Java program to sort a given array list. Go to the editor
Click me to see the solution

9. Write a Java program to copy one array list into another. Go to the editor
Click me to see the solution

10. Write a Java program to shuffle elements in a array list. Go to the editor
Click me to see the solution

11. Write a Java program to reverse elements in a array list. Go to the editor
Click me to see the solution

12. Write a Java program to extract a portion of a array list. Go to the editor
Click me to see the solution

13. Write a Java program to compare two array lists. Go to the editor
Click me to see the solution

14. Write a Java program of swap two elements in an array list. Go to the editor
Click me to see the solution

15. Write a Java program to join two array lists. Go to the editor
Click me to see the solution

16. Write a Java program to clone an array list to another array list. Go to the editor
Click me to see the solution

17. Write a Java program to empty an array list. Go to the editor
Click me to see the solution

18. Write a Java program to test an array list is empty or not. Go to the editor
Click me to see the solution

19. Write a Java program to trim the capacity of an array list the current list size. Go to the editor
Click me to see the solution

20. Write a Java program to increase the size of an array list. Go to the editor
Click me to see the solution

Java Collection: LinkedList Exercises [26 exercises with solution]

1. Write a Java program to append the specified element to the end of a linked list. Go to the editor
Click me to see the solution

2. Write a Java program to iterate through all elements in a linked list. Go to the editor
Click me to see the solution

3. Write a Java program to iterate through all elements in a linked list starting at the specified position. Go to the editor
Click me to see the solution

4. Write a Java program to iterate a linked list in reverse order. Go to the editor
Click me to see the solution

5. Write a Java program to insert the specified element at the specified position in the linked list. Go to the editor
Click me to see the solution

6. Write a Java program to insert elements into the linked list at the first and last position. Go to the editor
Click me to see the solution

7. Write a Java program to insert the specified element at the front of a linked list. Go to the editor
Click me to see the solution

8. Write a Java program to insert the specified element at the end of a linked list. Go to the editor
Click me to see the solution

9. Write a Java program to insert some elements at the specified position into a linked list. Go to the editor
Click me to see the solution

10. Write a Java program to get the first and last occurrence of the specified elements in a linked list. Go to the editor
Click me to see the solution

11. Write a Java program to display the elements and their positions in a linked list. Go to the editor
Click me to see the solution

12. Write a Java program to remove a specified element from a linked list. Go to the editor
Click me to see the solution

13. Write a Java program to remove first and last element from a linked list. Go to the editor
Click me to see the solution

14. Write a Java program to remove all the elements from a linked list. Go to the editor
Click me to see the solution

15. Write a Java program of swap two elements in an linked list. Go to the editor
Click me to see the solution

16. Write a Java program to shuffle the elements in a linked list. Go to the editor
Click me to see the solution

17. Write a Java program to join two linked lists. Go to the editor
Click me to see the solution

18. Write a Java program to clone an linked list to another linked list. Go to the editor
Click me to see the solution

19. Write a Java program to remove and return the first element of a linked list. Go to the editor
Click me to see the solution

20. Write a Java program to retrieve but does not remove, the first element of a linked list. Go to the editor
Click me to see the solution

21. Write a Java program to retrieve but does not remove, the last element of a linked list. Go to the editor
Click me to see the solution

22. Write a Java program to check if a particular element exists in a linked list. Go to the editor
Click me to see the solution

23. Write a Java program to convert a linked list to array list. Go to the editor
Click me to see the solution

24. Write a Java program to compare two linked lists. Go to the editor
Click me to see the solution

25. Write a Java program to test an linked list is empty or not. Go to the editor
Click me to see the solution

26. Write a Java program to replace an element in a linked list. Go to the editor
Click me to see the solution

Java Collection: HashSet Exercises [12 exercises with solution]

1. Write a Java program to append the specified element to the end of a hash set. Go to the editor
Click me to see the solution

2. Write a Java program to iterate through all elements in a hash list. Go to the editor
Click me to see the solution

3. Write a Java program to get the number of elements in a hash set. Go to the editor
Click me to see the solution

4. Write a Java program to empty an hash set. Go to the editor
Click me to see the solution

5. Write a Java program to test a hash set is empty or not. Go to the editor
Click me to see the solution

6. Write a Java program to clone a hash set to another hash set. Go to the editor
Click me to see the solution

7. Write a Java program to convert a hash set to an array. Go to the editor
Click me to see the solution

8. Write a Java program to convert a hash set to a tree set. Go to the editor
Click me to see the solution

9. Write a Java program to convert a hash set to a List/ArrayList. Go to the editor
Click me to see the solution

10. Write a Java program to compare two hash set. Go to the editor
Click me to see the solution

11. Write a Java program to compare two sets and retain elements which are same on both sets. Go to the editor
Click me to see the solution

12. Write a Java program to remove all of the elements from a hash set. Go to the editor
Click me to see the solution

Java Collection: TreeSet Exercises [16 exercises with solution]

1. Write a Java program to create a new tree set, add some colors (string) and print out the tree set. Go to the editor
Click me to see the solution

2. Write a Java program to iterate through all elements in a tree set. Go to the editor
Click me to see the solution

3. Write a Java program to add all the elements of a specified tree set to another tree set. Go to the editor
Click me to see the solution

4. Write a Java program to create a reverse order view of the elements contained in a given tree set. Go to the editor
Click me to see the solution

5. Write a Java program to get the first and last elements in a tree set. Go to the editor
Click me to see the solution

6. Write a Java program to clone a tree set list to another tree set. Go to the editor
Click me to see the solution

7. Write a Java program to get the number of elements in a tree set. Go to the editor
Click me to see the solution

8. Write a Java program to compare two tree sets. Go to the editor
Click me to see the solution

9. Write a Java program to find the numbers less than 7 in a tree set. Go to the editor
Click me to see the solution

10. Write a Java program to get the element in a tree set which is greater than or equal to the given element. Go to the editor
Click me to see the solution

11. Write a Java program to get the element in a tree set which is less than or equal to the given element. Go to the editor
Click me to see the solution

12. Write a Java program to get the element in a tree set which is strictly greater than or equal to the given element. Go to the editor
Click me to see the solution

13. Write a Java program to get an element in a tree set which is strictly less than the given element. Go to the editor
Click me to see the solution

14. Write a Java program to retrieve and remove the first element of a tree set. Go to the editor
Click me to see the solution

15. Write a Java program to retrieve and remove the last element of a tree set. Go to the editor
Click me to see the solution

16. Write a Java program to remove a given element from a tree set. Go to the editor
Click me to see the solution

Java Practice online