0% found this document useful (0 votes)
15 views3 pages

Java Collections Cheat Sheet Guide

This document provides a cheat sheet for Java Collections, including examples for ArrayList, LinkedList, HashMap, HashSet, Stack, Queue, and PriorityQueue. Each collection type is demonstrated with code snippets showcasing common operations such as adding, removing, and retrieving elements. The examples illustrate the basic usage and functionality of each collection in Java.

Uploaded by

m02803498
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)
15 views3 pages

Java Collections Cheat Sheet Guide

This document provides a cheat sheet for Java Collections, including examples for ArrayList, LinkedList, HashMap, HashSet, Stack, Queue, and PriorityQueue. Each collection type is demonstrated with code snippets showcasing common operations such as adding, removing, and retrieving elements. The examples illustrate the basic usage and functionality of each collection in Java.

Uploaded by

m02803498
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

Java Collections Cheat Sheet with Examples

1. ArrayList

import [Link].*;

ArrayList<Integer> list = new ArrayList<>();


[Link](5);
[Link](10);
[Link]([Link](0)); // 5
[Link](0, 20);
[Link](1);
[Link]([Link]());
for (int num : list) {
[Link](num);
}

2. LinkedList

import [Link].*;

LinkedList<Integer> list = new LinkedList<>();


[Link](5);
[Link](1);
[Link](10);
[Link]();
[Link]();
for (int num : list) {
[Link](num);
}

3. HashMap

import [Link].*;

HashMap<String, Integer> map = new HashMap<>();


[Link]("apple", 2);
[Link]("banana", 5);
[Link]([Link]("apple"));
[Link]("banana");
[Link]([Link]("apple"));
for ([Link]<String, Integer> e : [Link]()) {
[Link]([Link]() + " -> " + [Link]());
}

4. HashSet

import [Link].*;

HashSet<Integer> set = new HashSet<>();


[Link](1);
[Link](2);
[Link](1);
[Link]([Link](1));
[Link](2);
for (int num : set) {
[Link](num);
}

5. Stack

import [Link].*;

Stack<Integer> stack = new Stack<>();


[Link](1);
[Link](2);
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());

6. Queue

import [Link].*;

Queue<Integer> q = new LinkedList<>();


[Link](1);
[Link](2);
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());

7. PriorityQueue (Min/Max Heap)

import [Link].*;

PriorityQueue<Integer> pq = new PriorityQueue<>(); // min-heap


PriorityQueue<Integer> maxPq = new PriorityQueue<>([Link]());
[Link](5);
[Link](10);
[Link]([Link]());
[Link]([Link]());

You might also like