Java Collections Framework Study Notes
27 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which method is used to clear all the elements in a Hashtable?

  • delete()
  • removeAll()
  • clear() (correct)
  • reset()
  • What will be the maximum size of a List created using Arrays.asList()?

  • It is limited to 10 elements
  • It can only hold primitive data types
  • It can grow dynamically
  • It is fixed and cannot be altered (correct)
  • What is the output of calling the max() method with just a Comparator parameter?

  • It returns the maximum value based on the comparator
  • It returns the first element of the list
  • It throws an IllegalArgumentException
  • It requires a collection to function (correct)
  • Which collection type can be synchronized using Collections.synchronizedMap()?

    <p>HashMap</p> Signup and view all the answers

    What is the output of the following Java program involving a TreeSet with the elements {3, 9, 1, 4, 8}?

    <p>[1, 3, 4, 8, 9]</p> Signup and view all the answers

    What is the purpose of the contains(Object o) method in the Set interface?

    <p>To check if a specified element exists</p> Signup and view all the answers

    Which iterator type can be used specifically with List collections?

    <p>ListIterator</p> Signup and view all the answers

    Which method retrieves the element at a specific location in a properties object?

    <p>getProperty()</p> Signup and view all the answers

    What method is used to add a key-value pair to a map in Java?

    <p>put()</p> Signup and view all the answers

    What will be the output of the Java code that uses a stack to push and pop integer values?

    <p>[3, 5]</p> Signup and view all the answers

    Which method is correctly used to randomize the elements in a list?

    <p>shuffle()</p> Signup and view all the answers

    Which method can convert an ArrayList object to a static array?

    <p>toArray()</p> Signup and view all the answers

    When inserting a value and its key into a map, which method is used?

    <p>put()</p> Signup and view all the answers

    Which method removes all key-value pairs from a map?

    <p>clear()</p> Signup and view all the answers

    What will be printed by the provided Java program that utilizes ListIterator if called on an empty list?

    <p>EMPTY</p> Signup and view all the answers

    What does the Java program using reflection print about 'java.awt.Dimension'?

    <p>Prints all the methods of 'java.awt.Dimension'</p> Signup and view all the answers

    What occurs when an existing key object is put into a HashMap?

    <p>The new object replaces the older object</p> Signup and view all the answers

    What does the method peek() do in a collection?

    <p>It returns the next item in line without removing it.</p> Signup and view all the answers

    Which method is used to retrieve the current size of an ArrayList object?

    <p>size()</p> Signup and view all the answers

    Which of the following is NOT a legacy class?

    <p>ArrayList</p> Signup and view all the answers

    What is the initial capacity and load factor of a HashSet in Java?

    <p>16, 0.75</p> Signup and view all the answers

    What output will the Java program with the LinkedList produce?

    <p>2 8 5 1</p> Signup and view all the answers

    In the provided HashSet example, what will be printed along with the size?

    <p>3 items in sequence</p> Signup and view all the answers

    Does the HashSet class have a get(Object o) method?

    <p>No, it does not allow retrieving by object.</p> Signup and view all the answers

    Which of the following collections implements a linked list data structure?

    <p>LinkedList</p> Signup and view all the answers

    What will be the output of the HashSet example with added elements 'A', 'B', 'C'?

    <p>[A, B, C] 3</p> Signup and view all the answers

    What is the correct terminology for the collection of elements in a HashSet?

    <p>Set</p> Signup and view all the answers

    Study Notes

    Java Collections Framework - Study Notes

    • Stack:

      • push(): Adds an element to the top of the stack.
      • pop(): Removes and returns the element at the top of the stack.
      • println(obj): Prints the stack elements as a string.
      • Output of import java.util.*; class stack { public static void main(String args[]) { Stack obj = new Stack(); obj.push(new Integer(3)); obj.push(new Integer(2)); obj.pop(); obj.push(new Integer(5)); System.out.println(obj); } } -> [3, 5]
    • List:

      • shuffle(): Randomizes the order of elements in a list.
    • ArrayList:

      • toArray(): Converts an ArrayList to a standard array.
    • HashMap:

      • put(): Inserts a key-value pair into the map.
      • If a key already exists, the new value replaces the older value.
    • Map:

      • clear(): Removes all key-value pairs from the map.
    • remove(): Removes a key-value pair from the map.

    • ListIterator:

      • listIterator(): Creates a ListIterator object.
    • java.awt.Dimension:

      • Class.forName("java.awt.Dimension"): Returns the class of Dimension.
      • c.getMethods(): retrieves all methods of that class.
      • Output of the following code: import java.lang.reflect.*; class Additional_packages { public static void main(String args[]) { try { Class c = Class.forName("java.awt.Dimension"); Method methods[] = c.getMethods(); for (int i = 0; i < methods.length; i++) System.out.println(methods[i]); } catch (Exception e) { System.out.print("Exception"); } } } -> prints all the methods of java.awt.Dimension
    • HashSet:

      • Implements HashMap internally.
      • contains(Object o): Checks if a set contains an object.
      • Example: HashSet obj = new HashSet(); obj.add("A"); obj.add("B"); obj.add("C"); System.out.println(obj + " " + obj.size()); outputs [A, B, C] 3
    • TreeSet:

      • Stores elements in a sorted order.
      • Output of import java.util.*; class Output { public static void main(String args[]) { TreeSet t = new TreeSet(); t.add("3"); t.add("9"); t.add("1"); t.add("4"); t.add("8"); System.out.println(t); } } -> [1, 3, 4, 8, 9]
    • Arrays:

      • Arrays.fill(): Fills a portion of an array with a specified value.
      • Example: import java.util.*; class Array { public static void main(String args[]) { int array[] = new int[5]; for (int i = 5; i > 0; i--) array[5-i] = i; Arrays.fill(array, 1, 4, 8); for (int i = 0; i < 5 ; i++) System.out.print(array[i]); } } -> 58881
    • LinkedList:

      • Iterator: An object used to traverse collection elements.
      • The while(i.hasNext()) loop iterates and prints the next element in the linked list.
      • Output of import java.util.*; class Collection_Algos { public static void main(String args[]) { LinkedList list = new LinkedList(); list.add(new Integer(2)); list.add(new Integer(8)); list.add(new Integer(5)); list.add(new Integer(1)); Iterator i = list.iterator(); while(i.hasNext()) System.out.print(i.next() + " "); } }-> 2 8 5 1
    • Collections:

      • synchronizedMap(new HashMap()): Creates a thread-safe HashMap.
    • Iterator:

      • hasNext(): Checks if there are more elements in the iteration. Returns a boolean.
    • List:

      • ListIterator is specific to List interface; used to traverse elements in a specific order of the List.
    • Arrays.asList():

      • Returns a fixed-size list backed by the specified array. Modifying the returned list directly affects the original array.
    • Dequeue/Peek:

      • dequeue(): Removes and returns the first element in the queue;
      • peek(): Returns the first element in the queue without removing it.
    • Properties:

      • getProperty(): Retrieves a property value from a Properties object.
    • List manipulation: - To remove duplicates from a List, use a HashSet to store the unique elements (since a HashSet doesn't allow duplicates).

    • General Note: The Java Collections Framework provides a set of classes and interfaces for working with collections of objects, offering various functionalities like insertion, removal, traversal, searching, sorting, etc, and the legacy classes are older (Stack, Hashtable, Vector) classes that might have different structure and design compared to the current classes.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Explore the essential concepts of the Java Collections Framework, covering components like Stack, List, ArrayList, and HashMap. Understand key methods such as push, pop, shuffle, and put, along with their functionalities. This quiz is designed to test your knowledge and understanding of Java's powerful data structures.

    More Like This

    Use Quizgecko on...
    Browser
    Browser