0% found this document useful (0 votes)
3 views4 pages

Collections Final Realcode

The document provides a series of programming questions and sample code related to Java collections, including ArrayList, HashSet, and HashMap. Each question demonstrates specific operations such as adding, updating, removing elements, and checking presence in the collections. The document includes input and output examples for clarity.

Uploaded by

subudhisohana38
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)
3 views4 pages

Collections Final Realcode

The document provides a series of programming questions and sample code related to Java collections, including ArrayList, HashSet, and HashMap. Each question demonstrates specific operations such as adding, updating, removing elements, and checking presence in the collections. The document includes input and output examples for clarity.

Uploaded by

subudhisohana38
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

Q1.

ArrayList Add & Print


Question: Create list and print elements
Input: 10,20,30
Output:
10
20
30
Explanation: Shows adding and iterating list
Sample Code:
ArrayList<Integer> list=new ArrayList<>();
[Link](10); [Link](20); [Link](30);
for(int x:list) [Link](x);

Q2. Insert & Update


Question: Insert at index and update element
Input: 10,20,30
Output:
[10,15,25,30]
Explanation: Demonstrates add(index) and set()
Sample Code:
[Link](1,15);
[Link](2,25);
[Link](list);

Q3. Remove & Contains


Question: Remove element and check presence
Input: 10,20,30
Output:
[10,30]
false
Explanation: Shows remove and contains
Sample Code:
[Link]([Link](20));
[Link](list);
[Link]([Link](20));

Q4. Size & Get


Question: Print size and get element
Input: 10,20,30
Output:
3
20
Explanation: size() gives count, get() fetches value
Sample Code:
[Link]([Link]());
[Link]([Link](1));

Q5. Sorting
Question: Sort list
Input: 30,10,20
Output:
[10,20,30]
Explanation: Uses [Link]
Sample Code:
[Link](list);
[Link](list);

Q6. HashSet Unique


Question: Store unique elements
Input: 10,10,20
Output:
[10,20]
Explanation: Duplicates removed automatically
Sample Code:
HashSet<Integer> set=new HashSet<>();
[Link](10); [Link](10); [Link](20);
[Link](set);

Q7. Set Remove


Question: Remove element from set
Input: 10,20,30
Output:
[10,30]
Explanation: remove deletes element
Sample Code:
[Link](20);
[Link](set);

Q8. Set Contains


Question: Check element in set
Input: 10,20
Output:
true
Explanation: contains checks presence
Sample Code:
[Link]([Link](10));
Q9. HashMap Put
Question: Store key-value pairs
Input: (1,Ravi)
Output:
{1=Ravi}
Explanation: put adds entry
Sample Code:
HashMap<Integer,String> map=new HashMap<>();
[Link](1,"Ravi");
[Link](map);

Q10. Map Get


Question: Retrieve value
Input: (1,Ravi)
Output:
Ravi
Explanation: get returns value
Sample Code:
[Link]([Link](1));

Q11. Map Remove


Question: Remove key
Input: (1,Ravi)
Output:
{}
Explanation: remove deletes entry
Sample Code:
[Link](1);
[Link](map);

Q12. Map Contains


Question: Check key
Input: (1,Ravi)
Output:
true
Explanation: containsKey checks key
Sample Code:
[Link]([Link](1));

Q13. Map Iteration


Question: Print entries
Input: (1,Ravi)
Output:
1 Ravi
Explanation: entrySet iteration
Sample Code:
for([Link]<Integer,String> e:[Link]())
[Link]([Link]()+" "+[Link]());

Q14. List to Set


Question: Convert list to set
Input: 10,20,20
Output:
[10,20]
Explanation: Removes duplicates
Sample Code:
HashSet<Integer> s=new HashSet<>(list);
[Link](s);

Q15. Combined
Question: Use list, set, map together
Input: 10,20
Output:
0 10
1 20
Explanation: Combines all collections
Sample Code:
HashMap<Integer,Integer> m=new HashMap<>();
for(int i=0;i<[Link]();i++)
[Link](i,[Link](i));
for([Link]<Integer,Integer> e:[Link]())
[Link]([Link]()+" "+[Link]());

You might also like