Monday, March 1, 2021

Collections Programs

Q1. Write a program to traverse (or iterate) ArrayList?

Q2 Write a program to convert List to Array.

Q3 Write a program to traverse(or iterate) HashSet?  

Q4 Given an element write a program to check if element(value) exists in ArrayList?

Q5 Given an element write a program to check if element exists in HashSet?

Q6 Write a program to initialize a HashMap in java ?

Q7 Write a program to initialize an ArrayList in java?

Q8  Write a program to convert Array to List? 

Array can be converted to list using Arrays.asList() method.

Q9 Write a program to find the length of the ArrayList?

Q10 Write a program to add elements to the HashMap given the key and value data type is String?

Q11 Write a program to initialize a HashSet in java?

Q12 Write a program to add elements to ArrayList ?

Q13 Write a program to add elements to HashSet? 

Q14 Write a program to get size of HashMap?

You can use the size() method of HashMap class.

Q15  How to check if HashMap is empty?

You can check if HashMap is empty using isEmpty() method.

Q16 Write a program to iterate the HashMap ? (Solution)

There are many ways to iterate the HashMap.The best way to iterate the HashMap is using iterator.

Q17 Write a program to sort HashMap by keys ? (Solution)

You can sort HashMap by keys using TreeMap object.

Q18 Write a program to sort ArrayList using Comparable and Comparator? (Solution)

You can sort ArrayList of Objects using Comparable and Comparator.

Q19 Write a program to sort ArrayList in descending order? (Solution)

You can sort the ArrayList in descending order using Collections.reverseOrder() method.

Q20 Write a program to add element at particular index of ArrayList? (Solution)

You can add element at particular index of ArrayList using method add(int index, Object element).

Q21 Write a program to remove element from specified index of ArrayList? (Solution)


You can remove element at particular index of ArrayList using remove(int index) method.

Q22 Write a program to convert LinkedList to ArrayList? (Solution)

Q23 Write a program to convert HashSet to Array? (Solution)

Q24 Write a program to reverse ArrayList in java? (Solution)

Q25 Write a program to iterate TreeMap in java? (Solution)

Q26 Write a program to sort HashMap by value? (Solution)

Q27 How to serialize a HashMap in java? (Solution)

Q28 How to synchronize a HashMap in java? (Solution)

Q29 How to serialize an ArrayList in java? (Solution)

Q30 How to synchronize an ArrayList in java? (Solution)

--------------------

HashMap

1. Write a Java program to associate the specified value with the specified key in a HashMap. 

2. Write a Java program to count the number of key-value (size) mappings in a map. 

3. Write a Java program to copy all of the mappings from the specified map to another map. 

4. Write a Java program to remove all of the mappings from a map. 

5. Write a Java program to check whether a map contains key-value mappings (empty) or not. 

6. Write a Java program to get a shallow copy of a HashMap instance. 

7. Write a Java program to test if a map contains a mapping for the specified key. 

8. Write a Java program to test if a map contains a mapping for the specified value. 

9. Write a Java program to create a set view of the mappings contained in a map. 

10. Write a Java program to get the value of a specified key in a map. 

11. Write a Java program to get a set view of the keys contained in this map.

12. Write a Java program to get a collection view of the values contained in this map. 


No comments:

Post a Comment

String coding questioon

  How do you reverse a given string in place? ( solution ) How do you print duplicate characters from a string? ( solution ) How do you chec...