0% found this document useful (0 votes)
0 views2 pages

Java Collections Questions

The document contains a series of Java code snippets demonstrating the usage of various data structures such as List, Set, Map, Queue, and Deque. Each snippet includes operations like adding, retrieving, and manipulating elements, along with print statements to display results. The examples illustrate fundamental concepts of Java collections and their behaviors.

Uploaded by

babaj5604
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)
0 views2 pages

Java Collections Questions

The document contains a series of Java code snippets demonstrating the usage of various data structures such as List, Set, Map, Queue, and Deque. Each snippet includes operations like adding, retrieving, and manipulating elements, along with print statements to display results. The examples illustrate fundamental concepts of Java collections and their behaviors.

Uploaded by

babaj5604
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/ 2

Q1:

List<String> list = new ArrayList<>(); list.add("A"); list.add("B");


System.out.println(list.get(1));
Q2:
Set<Integer> set = new HashSet<>(); set.add(1); set.add(1);
System.out.println(set.size());
Q3:
Map<String, Integer> map = new HashMap<>(); map.put("a", 1); map.put("b", 2);
System.out.println(map.get("a"));
Q4:
List<String> list = new LinkedList<>(); list.add("X"); list.add("Y"); list.remove(0);
System.out.println(list);
Q5:
Queue<Integer> q = new LinkedList<>(); q.add(10); q.add(20);
System.out.println(q.poll());
Q6:
Deque<String> deque = new ArrayDeque<>(); deque.add("first"); deque.push("top");
System.out.println(deque.peek());
Q7:
Set<String> treeSet = new TreeSet<>(); treeSet.add("banana"); treeSet.add("apple");
System.out.println(treeSet);
Q8:
Map<Integer, String> map = new TreeMap<>(); map.put(3, "C"); map.put(1, "A");
System.out.println(map);
Q9:
List<Integer> list = Arrays.asList(1, 2, 3); Collections.reverse(list);
System.out.println(list);
Q10:
List<String> list = Arrays.asList("A", "B", "C");
System.out.println(list.contains("B"));
Q11:
Set<String> set = new LinkedHashSet<>(); set.add("one"); set.add("two");
System.out.println(set);
Q12:
Map<String, String> map = new LinkedHashMap<>(); map.put("key", "value");
System.out.println(map);
Q13:
List<Integer> list = new ArrayList<>(Arrays.asList(10, 20)); list.clear();
System.out.println(list.isEmpty());
Q14:
PriorityQueue<Integer> pq = new PriorityQueue<>(); pq.add(30); pq.add(10);
System.out.println(pq.peek());
Q15:
Map<String, Integer> map = new HashMap<>(); map.put(null, 100);
System.out.println(map.get(null));
Q16:
List<String> list = Collections.singletonList("Only"); System.out.println(list);
Q17:
List<Integer> list = Arrays.asList(1, 2, 3); list.set(1, 5); System.out.println(list);
Q18:
List<String> list = new ArrayList<>(); System.out.println(list.isEmpty());
Q19:
Set<Integer> set = new TreeSet<>(Arrays.asList(3, 2, 1)); System.out.println(set);
Q20:
Map<String, Integer> map = new HashMap<>(); map.put("x", 1); map.remove("x");
System.out.println(map);
Q21:
Deque<Integer> deque = new ArrayDeque<>(); deque.addFirst(1); deque.addLast(2);
System.out.println(deque);
Q22:
Queue<Integer> queue = new PriorityQueue<>(); queue.add(2); queue.add(1);
System.out.println(queue.poll());
Q23:
List<String> list = new CopyOnWriteArrayList<>(); list.add("A"); list.add("B");
for(String s : list) list.remove(s); System.out.println(list);
Q24:
Map<String, String> map = Collections.emptyMap(); System.out.println(map.isEmpty());
Q25:
List<String> list = new ArrayList<>(List.of("one", "two")); list.add("three");
System.out.println(list);
Q26:
Map<Integer, String> map = new HashMap<>(); map.put(1, "one"); map.put(1, "uno");
System.out.println(map);
Q27:
Set<String> set = new HashSet<>(Arrays.asList("a", "b", "a"));
System.out.println(set.size());
Q28:
List<Integer> list = Arrays.asList(1, 2, 3); System.out.println(list.indexOf(2));
Q29:
List<String> list = new ArrayList<>(Arrays.asList("X", "Y", "Z")); list.subList(0,
2).clear(); System.out.println(list);
Q30:
Map<String, String> map = new HashMap<>(); System.out.println(map.put("k", "v"));

You might also like