[go: up one dir, main page]

0% found this document useful (0 votes)
25 views3 pages

Collection programs

Uploaded by

rasalshitals
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Collection programs

Uploaded by

rasalshitals
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Collection Programs

Convert Array into ArrayList


import java.util.*;
class Main {
public static void main(String[] args) {
String s [] = {"Mihir","Shital"};
ArrayList<String> arr = new ArrayList <>(Arrays.asList(s));
System.out.println(arr);
arr.add("Krishna");
System.out.println(arr);
}
}

Convert ArrayList into Array


import java.util.*;

class Main {

public static void main(String[] args) {


ArrayList<String> a = new ArrayList <>(Arrays.asList("God", "avadhut", "Lokmanya", "Mihir"));
System.out.println(a);
String s[] = a.toArray(new String [0]);
for (String str :s)
{
System.out.println(str);
}
}
}

Find the Duplicate Elements in a List. Concepts: HashSet, Collections.frequency.


Input: List<Integer> nums = Arrays.asList(1, 2, 3, 2, 4, 5, 1); Output: [1, 2]
import java.util.*;
class Main {
public static void main(String[] args) {
List <Integer>list = Arrays.asList(1, 2, 3, 2, 4, 5, 1);
System.out.println(list);
Set <Integer> s = new HashSet<>();
Set <Integer> p = new HashSet<>();
for (int i=0; i<list.size();i++){
int l = list.get(i);
if (!s.add(l)){
p.add(l);
}
} System.out.println(p);

}
}

Basic Programs:

1. Sort a List of Strings in Ascending/Descending Order

o Input: List<String> names = Arrays.asList("Tom", "Anna", "John");

o Output (Ascending): [Anna, John, Tom]


Collection Programs
2. Convert a List to a Set and Remove Duplicates

o Input: List<Integer> nums = Arrays.asList(1, 2, 2, 3, 4, 4);

o Output: [1, 2, 3, 4]

3. Find the Maximum and Minimum Elements in a Collection

o Input: List<Integer> nums = Arrays.asList(5, 2, 9, 1, 7);

o Output: Max: 9, Min: 1

o Concepts: Collections.max, Collections.min.

4. Reverse a List Without Using Collections.reverse()

o Input: List<Integer> nums = Arrays.asList(1, 2, 3, 4);

o Output: [4, 3, 2, 1]

Intermediate Programs:

6. Find the First Non-Repeated Character in a String Using Map

o Input: "swiss"

o Output: 'w'

o Concepts: LinkedHashMap.

7. Group Elements of a List Using Map

o Input: List<String> words = Arrays.asList("cat", "bat", "apple", "dog", "ant");

o Output: {a=[apple, ant], b=[bat], c=[cat], d=[dog]}

o Concepts: Map, Stream API.

8. Check If Two Lists Are Equal

o Input: List<Integer> list1 = Arrays.asList(1, 2, 3); List<Integer> list2 = Arrays.asList(3, 2, 1);

o Output: false

o Concepts: Collections.sort.

9. Find Common Elements Between Two Lists

o Input:
List<Integer> list1 = Arrays.asList(1, 2, 3, 4);
List<Integer> list2 = Arrays.asList(3, 4, 5, 6);

o Output: [3, 4]

o Concepts: retainAll.

10. Remove Null Values from a List

o Input: List<String> names = Arrays.asList("Alice", null, "Bob", null);


Collection Programs
o Output: [Alice, Bob]

o Concepts: Stream API, removeIf.

Advanced Programs:

11. Implement a Custom ArrayList

o Create a custom implementation of ArrayList with basic methods like add(), remove(), size(), and get().

12. Sort a Map by Values

o Input: {A=2, B=1, C=3}

o Output: {B=1, A=2, C=3}

o Concepts: Stream API, Comparator.

13. Find the Frequency of Each Element in a List

o Input: List<Integer> nums = Arrays.asList(1, 2, 2, 3, 3, 3);

o Output: {1=1, 2=2, 3=3}

o Concepts: HashMap.

14. Merge Two Maps and Resolve Key Conflicts

o Input:
Map<Integer, String> map1 = {1="A", 2="B"};
Map<Integer, String> map2 = {2="C", 3="D"};

o Output: {1="A", 2="C", 3="D"}

o Concepts: merge().

15. Check If a List Is a Subset of Another List

o Input:
List<Integer> list1 = Arrays.asList(1, 2);
List<Integer> list2 = Arrays.asList(1, 2, 3, 4);

o Output: true

Theoretical Questions:

16. Difference Between HashSet and TreeSet

17. How Does HashMap Handle Collisions?

18. Explain the Internal Working of ArrayList

19. Why Is HashSet Faster Than List for Search Operations?

20. Explain Fail-Fast vs. Fail-Safe Iterators

You might also like