Common Formats
Common Formats
List
Example:
java
Copy code
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
Operations:
Size: size()
o Returns the number of elements in the list.
o Example: int size = list.size(); // 2
Check Presence: contains(element)
o Checks if the list contains the specified element.
o Example: boolean containsApple = list.contains("Apple");
ArrayList
Example:
java
Copy code
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(10);
arrayList.add(20);
Operations:
2. HashMap
A HashMap is a collection that stores data in key-value pairs, allowing fast retrieval,
insertion, and deletion based on keys.
Example:
java
Copy code
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Apple"); // key = 1, value = "Apple"
Operations:
Retrieve: get(key)
o Returns the value associated with key.
o Example: String fruit = map.get(1); // "Apple"
Remove: remove(key)
o Removes the entry for key.
o Example: map.remove(1);
Check Key Presence: containsKey(key)
o Checks if key is present in the map.
o Example: boolean hasKey = map.containsKey(1);
3. HashSet
A HashSet stores unique elements without duplicates and provides fast access and
modification.
Example:
java
Copy code
HashSet<String> set = new HashSet<>();
set.add("Apple"); // Adds "Apple" to the set
Operations:
Size: size()
o Returns the number of elements in the set.
o Example: int size = set.size();
4. String
Example:
java
Copy code
String str = "Hello";
Operations:
Length: length()
o Returns the number of characters in the string.
o Example: int len = str.length();
Character at Index: charAt(index)
o Returns the character at the specified index.
o Example: char ch = str.charAt(0); // 'H'
Concatenate: concat(otherString)
o Concatenates otherString to the original string.
o Example: String result = str.concat(" World"); // "Hello
World"
5. StringBuilder
Example:
java
Copy code
StringBuilder sb = new StringBuilder("Hello");
Operations:
Append: append(value)
o Adds value to the end of the StringBuilder.
o Example: sb.append(" World"); // "Hello World"
Length: length()
o Returns the number of characters in the StringBuilder.
o Example: int len = sb.length();
6. List
A List is a collection of elements that can contain duplicates and maintain insertion
order.
Example:
java
Copy code
List<String> list = new ArrayList<>();
list.add("Apple");
Operations:
7. Stack
A Stack is a last-in, first-out (LIFO) data structure where the last element added is
the first one to be removed.
Example:
java
Copy code
Stack<Integer> stack = new Stack<>();
stack.push(1);
Operations:
8. Queue
A Queue is a first-in, first-out (FIFO) data structure where the first element added is
the first one to be removed.
Example:
java
Copy code
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
Operations:
9. Deque
Example:
java
Copy code
Deque<String> deque = new LinkedList<>();
deque.addFirst("First");
Operations: