🐥
The Tech Toolbox
Visit My Website!LinkedIn Profile
  • ✍️Introduction
  • ✍️Laptop Setup Guide
  • ✍️Quotes
  • Interview Preparation
    • 🔥Topics To Cover
    • 🔥System Design
    • 🔥Google Interview
      • Data Structures
      • Algorithms
      • System Design
      • Programming Concepts
      • Behavioral
    • 🔥Documents
  • Question Answers
    • ⭐Question Set 1
    • ⭐Question Set 2
    • ⭐Question Set 3
    • ⭐Microservice Architecture
    • ⭐Company-wise
      • Publicis Sapient
        • Core Java Section
        • Solutions & Tools
        • Microservices
        • Mix Questions
      • Mastercard
      • Finicity
  • Notes
    • 🎯Java Fundamentals
      • Time & Space Complexity
      • Design Patterns
      • Collections
      • Java 8 Features
      • JVM Internals
      • Generics
      • Multithreading
    • 🎯Spring Fundamentals
      • Spring Boot
      • Spring AOP
      • Spring Cloud
      • Spring Security
      • Spring Batch
    • 🎯Database
      • Oracle SQL
        • Aggregate Functions
      • MongoDB
        • Commands
        • Aggregate Query
      • Distributed Transaction
    • 🎯Apache Kafka
      • Kafka with Spring Boot
      • Partitions
    • 🎯Redis
    • 🎯Mockito
    • 🎯Docker
      • Commands
    • 🎯Kubernetes
      • Commands
    • 🎯Prometheus
    • 🎯Build Tools
      • Gradle
      • Apache Maven
    • 🎯Architecture
      • API and Integration Strategy
  • ⚓Developer Reference
    • ❄️GCP
    • ❄️Linux
  • Structured Learning Plan
    • 🍉Java Multithreading
    • 🍉Data Structures
    • 🍉Spring AOP
    • 🍉Transaction Management
    • 🍉MongoDB
    • 🍉Design Patterns
    • 🍉System Design
Powered by GitBook
On this page
  • List
  • Set
  • Map
  • Queue
  • Stack
  • Collection utilities

Was this helpful?

  1. Notes
  2. Java Fundamentals

Collections

List

  • A List is an ordered collection of elements.

  • Elements can be accessed by their index.

  • Java provides several implementations of the List interface:

    • ArrayList: A resizable array that is backed by an array.

    • LinkedList: A doubly-linked list that allows for fast insertion and deletion at the beginning or end of the list.

    • Vector: Similar to ArrayList but is synchronized, which makes it thread-safe.

  • Example:

List<String> names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add("Joe");
System.out.println(names.get(0));

Set

  • A Set is an unordered collection of unique elements.

  • Java provides several implementations of the Set interface:

    • HashSet: Uses a hash table to store the elements, which provides constant time performance for basic operations.

    • TreeSet: Stores the elements in a sorted order, which provides faster searching.

    • LinkedHashSet: Maintains the insertion order of the elements, which provides predictable iteration order.

  • Example:

Set<String> names = new HashSet<>();
names.add("John");
names.add("Jane");
names.add("Joe");
System.out.println(names.contains("John"));

Map

  • A Map is a collection of key-value pairs.

  • Keys must be unique, but values can be duplicated.

  • Java provides several implementations of the Map interface:

    • HashMap: Uses a hash table to store the elements, which provides constant time performance for basic operations.

    • TreeMap: Stores the elements in a sorted order based on the keys.

    • LinkedHashMap: Maintains the insertion order of the elements, which provides predictable iteration order.

  • Example:

Map<String, Integer> scores = new HashMap<>();
scores.put("John", 10);
scores.put("Jane", 20);
scores.put("Joe", 30);
System.out.println(scores.get("Jane"));

Queue

  • A Queue is a collection that represents a waiting line.

  • Elements are added to the end of the queue and removed from the front.

  • Java provides several implementations of the Queue interface:

    • LinkedList: A doubly-linked list that allows for fast insertion and deletion at the beginning or end of the list.

    • PriorityQueue: A queue that orders its elements according to their natural order or a specified comparator.

  • Example:

Queue<String> queue = new LinkedList<>();
queue.add("John");
queue.add("Jane");
queue.add("Joe");
System.out.println(queue.peek());

Stack

  • A Stack is a collection that represents a stack of items.

  • Elements are added to the top of the stack and removed from the top.

  • Java provides a Stack class that implements the Stack interface.

  • Example:

Stack<String> stack = new Stack<>();
stack.push("John");
stack.push("Jane");
stack.push("Joe");
System.out.println(stack.pop());

Collection utilities

  • Java provides several utility classes for working with collections:

    • Collections: Provides methods for sorting, searching, and manipulating collections.

    • Arrays: Provides methods for working with arrays, such as sorting and searching.

  • Example:

List<String> names = Arrays.asList("John", "Jane", "Joe");
Collections.sort(names);
System.out.println(names);
PreviousDesign PatternsNextJava 8 Features

Last updated 2 years ago

Was this helpful?

🎯