Skip to main content

Collection API

Collection Interface 

Included Signatures
size()
isEmpty()
contains(Object)
iterator()
toArray()
toArray(T[])
toArray(IntFunction<T[]>)

Example

System.out.println(list);
Student[] s = list.toArray(Student[]::new);
System.out.println(s);
for(Student x:s) {
    System.out.println(x);
}

add(E)
remove(Object)
containsAll(Collection<?>)
addAll(Collection<? extends E>)
removeAll(Collection<?>)
removeIf(Predicate<? super E>)
retainAll(Collection<?>)
clear()
equals(Object)
hashCode()
spliterator()
stream()
parallelStream()

Collections Class

Sorting 

Natural Order Sorting 

List<Integer> list = Arrays.asList(4, 3, 8, 6, 9, 8, 10);
Collections.sort(list);
System.out.println(list);

Custom Comparator Sorting

Comparator<Integer> com = (o1, o2) -> o1 < o2 ? -1 : 1;
Collections.sort(list,com);
System.out.println(list);

Reverse Sorting

Collections.reverse(list);
System.out.println(list);

Swap by indexes

Collections.swap(list, 0, list.size()-1);
System.out.println(list);

Fill all the element with one value

Collections.fill(list, 5);
System.out.println(list);

Get the natural min value

Integer x = Collections.min(list);
System.out.println(x);

Get the custom min value

Comparator<Integer> com = (o1, o2) -> o1 > o2 ? -1 : 1;
Integer x = Collections.min(list, com);
System.out.println(x);

Get the natural max value

x = Collections.max(list);
System.out.println(x);

Binary search for a value and get the index

Collections.sort(list);
System.out.println(list);
Integer x = Collections.binarySearch(list, 8);

Find no of occurrence of a value

Integer x = Collections.frequency(list, 8);
System.out.println(x);

Sort Custom Classes

Method 1

public class Application {
    public static void main(String[] args) {
        List<Student> list = Arrays.asList(new Student(30, "Raja"), new Student(20, "Kumar"));
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);

    }
}

class Student implements Comparable<Student> {
    private int age;
    private String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public int compareTo(Student incommingStudent) {
        if (this.age > incommingStudent.age) {
            return 1;
        }
        return -1;
    }

    @Override
    public String toString() {
        return "Student [age=" + age + ", name=" + name + "]";
    }
   
}


Method 2 Using Lamda

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Application {
    public static void main(String[] args) {
        List<Student> list = Arrays.asList(new Student(30, "Raja"), new Student(20, "Kumar"));
        System.out.println(list);
       
        Comparator<Student> com = (o1, o2) -> o1.age < o2.age ? -1 : 1;
        Collections.sort(list,com);
        System.out.println(list);

    }
}

class Student  {
    int age;
    String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student [age=" + age + ", name=" + name + "]";
    }
   
}

Method 3 Without Collections Class

Comparator<Student> com = (o1, o2) -> o1.age < o2.age ? -1 : 1;
list.sort(com);
System.out.println(list);

Comments

Popular posts from this blog

Spring Boot 4 Ready

Spring Boot 4.x Features Explained:  Java 25, AI Agents, MCP, Resilience, gRPC & Cloud Native Evolution Building on the massive architectural shift introduced in version 3, Spring Boot 4.x bridges the gap between traditional enterprise stability and the rapid demands of modern cloud architecture and AI integration. While Spring Boot 3 introduced the foundations for GraalVM Native Images and Virtual Threads , Spring Boot 4 takes these capabilities mainstream, introduces native resilience patterns, and upgrades the underlying Spring ecosystem. Spring Boot 4 Major Theme: Modern enterprise development with cloud-native architecture, AI integration, improved resilience, better modularity, and optimized JVM performance. Table of Contents 1. Upgraded Foundation & Baselines 2. Built-in Core Resilience Features 3. First-Class Agentic AI & Model Context Protocol (MCP) 4. Native API Versioning Strategy 5. Modern...

Most questions in 2025

Java Spring Boot Senior Engineer Interview Questions & Answers A complete interview preparation guide for Senior Java Engineers, Spring Boot Developers, and Backend Technical Leads covering Java, Spring Boot, Microservices, Cloud, Database, Security, and Performance Engineering concepts. Target Audience: Senior Java Developers Spring Boot Engineers Microservice Architects Backend Technical Leads Table of Contents 1. Describe Yourself 2. RESTful APIs and Standards 3. Latest Spring Boot Version & Features 4. Java Optional 5. WebFlux Reactive Programming 6. WebFlux Retry Mechanism 7. WebFlux Pagination APIs 8. Circuit Breaker 9. Resilience Patterns 10. Rate Limiter 1. Describe Yourself A senior engineer introduction should highlight technical experience, architecture knowledge, ownership, and problem-solving capabilities. Sample Answer I am a Java backend engineer with 7+ years of experience desi...

Data Structure

Java Data Structures A highly-scannable architectural cheat sheet for developers and technical interviews. 1. Linear vs. Non-Linear: The Core Division Data Structure Classification Hierarchy ┌───────────────────────┐ │ Data Structures │ └───────────┬───────────┘ │ ┌───────────────┴───────────────┐ ▼ ▼ ┌────────────────────┐ ┌────────────────────┐ │ Linear (Seq) │ │ Non-Linear │ └──────────┬─────────┘ └──────────┬─────────┘ │ │ ┌──────┴──────┐ ┌──────┴──────┐ ▼ ▼ ▼ ▼ [Static] [Dynamic] [Trees] [Graphs] (Array) (Queue, Stack) (BST) (Network) ...