0% found this document useful (0 votes)
9 views

Java

This document discusses common Java data structures including arrays, ArrayList, LinkedList, HashMap, HashSet, Stack, and Queue. It provides code examples for initializing each data structure, adding/retrieving elements, and iterating over elements. Key methods covered include add, get, size, push, pop, peek, offer, poll for common operations.

Uploaded by

amanibchir07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Java

This document discusses common Java data structures including arrays, ArrayList, LinkedList, HashMap, HashSet, Stack, and Queue. It provides code examples for initializing each data structure, adding/retrieving elements, and iterating over elements. Key methods covered include add, get, size, push, pop, peek, offer, poll for common operations.

Uploaded by

amanibchir07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

ValueType retrievedValue = map.

get(key);
for (Map.Entry<KeyType, ValueType> entry : map.entrySet()) {
Java Data Structures KeyType key = entry.getKey();
ValueType value = entry.getValue();
}
1. Arrays
int[] arr = new int[size]; 5. HashSet
int element = arr[index];
for (int i = 0; i < arr.length; i++) {
HashSet<Integer> set = new HashSet<>();
// Access arr[i]
} set.add(element);
boolean contains = set.contains(element);

2. ArrayList ( dynamic array )


6. Stack
ArrayList<Integer> list = new ArrayList<>();
list.add(element);
int element = list.get(index); Stack<Integer> stack = new Stack<>();
for (int i = 0; i < list.size(); i++) { stack.push(element);
// Access list.get(i) int top = stack.pop();
} int peek = stack.peek();
for (int i :list) { Iterator iterator = stack.iterator();
System.out.println(i) while(iterator.hasNext()){
} Object values =iterator.next();
System.out.println(values);
}

3. LinkedList
LinkedList<Integer> linkedList = new LinkedList<>();
7. Queue
linkedList.add(element); Queue<Integer> queue = new LinkedList<>();
int element = linkedList.get(index); queue.offer(element); // Enqueue
for (Integer value : linkedList) { int front = queue.poll(); // Dequeue
// Access linkedList.get(index) int peek = queue.peek(); // Peek
}

4. HashMap
HashMap<KeyType, ValueType> map =
new HashMap<>();
map.put(key, value);

You might also like