Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Basic Java

Collection

In Java, the term "Collections" refers to data structures that store and manipulate groups of objects. The Java Collections Framework provides several interfaces and classes for implementing and organizing collections. Here are the main types of collections in Java along with examples:

1. List Interface:

  • Description: Lists are ordered collections that allow duplicate elements.

Example Code:

import java.util.List;
import java.util.ArrayList;

public class ListExample {

    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        
        // Adding elements to the list
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        
        // Accessing elements by index
        System.out.println("First name: " + names.get(0));
        
        // Iterating through the list
        System.out.println("Names:");
        for (String name : names) {
            System.out.println(name);
        }
    }
}

2. Set Interface:

  • Description: Sets are unordered collections that do not allow duplicate elements.

Example Code:

import java.util.Set;
import java.util.HashSet;

public class SetExample {

    public static void main(String[] args) {
        Set<String> uniqueNames = new HashSet<>();
        
        // Adding elements to the set
        uniqueNames.add("Alice");
        uniqueNames.add("Bob");
        uniqueNames.add("Alice");  // Ignored, as "Alice" already exists
        
        // Iterating through the set
        System.out.println("Unique Names:");
        for (String name : uniqueNames) {
            System.out.println(name);
        }
    }
}

3. Map Interface:

  • Description: Maps store key-value pairs, where each key is associated with exactly one value.

Example Code:

import java.util.Map;
import java.util.HashMap;

public class MapExample {

    public static void main(String[] args) {
        Map<String, Integer> ageMap = new HashMap<>();
        
        // Adding key-value pairs to the map
        ageMap.put("Alice", 30);
        ageMap.put("Bob", 25);
        ageMap.put("Charlie", 35);
        
        // Accessing values by key
        System.out.println("Bob's Age: " + ageMap.get("Bob"));
        
        // Iterating through the map
        System.out.println("Ages:");
        for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

4. Queue Interface:

  • Description: Queues represent a collection used to hold multiple elements before processing.

Example Code:

import java.util.Queue;
import java.util.LinkedList;

public class QueueExample {

    public static void main(String[] args) {
        Queue<String> waitingQueue = new LinkedList<>();
        
        // Enqueuing elements
        waitingQueue.offer("Customer 1");
        waitingQueue.offer("Customer 2");
        waitingQueue.offer("Customer 3");
        
        // Dequeuing elements
        System.out.println("Processing: " + waitingQueue.poll());
        System.out.println("Processing: " + waitingQueue.poll());
    }
}

5. Stack Class:

  • Description: A stack is a collection that uses the Last-In-First-Out (LIFO) order.

Example Code:

import java.util.Stack;

public class StackExample {

    public static void main(String[] args) {
        Stack<String> webHistory = new Stack<>();
        
        // Pushing elements onto the stack
        webHistory.push("Page 1");
        webHistory.push("Page 2");
        webHistory.push("Page 3");
        
        // Popping elements from the stack
        System.out.println("Back: " + webHistory.pop());
        System.out.println("Back: " + webHistory.pop());
    }
}

These examples showcase various types of collections in Java along with their basic operations. The Java Collections Framework provides a rich set of interfaces and classes that can be utilized to solve a wide range of programming problems involving collections of data.

PreviousArrayNextException and Exception Handling

Last updated 1 year ago