0% found this document useful (0 votes)
14 views11 pages

Common Formats

Uploaded by

sec22it136
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)
14 views11 pages

Common Formats

Uploaded by

sec22it136
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
You are on page 1/ 11

Common Formats

List

A List is an interface in Java that represents an ordered collection of elements. It


allows duplicates and maintains the order of insertion.

 Syntax: List<Type> list = new ArrayList<>();

 Example:

java
Copy code
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");

Operations:

 Add Element: add(element)


o Adds element to the list.
o Example: list.add("Cherry");
 Access Element: get(index)
o Retrieves the element at the specified index.
o Example: String fruit = list.get(0); // "Apple"

 Remove Element: remove(index) or remove(element)


o Removes the element at index or the specified element.
o Example: list.remove(1); // Removes "Banana"

 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

ArrayList is a resizable array implementation of the List interface. It allows


dynamic resizing, and provides methods to manipulate the size of the array.

 Syntax: ArrayList<Type> arrayList = new ArrayList<>();

 Example:

java
Copy code
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(10);
arrayList.add(20);

Operations:

 Add Element: add(element)


o Adds element to the ArrayList.
o Example: arrayList.add(30);
 Access Element: get(index)
o Retrieves the element at the specified index.
o Example: int number = arrayList.get(0); // 10

 Remove Element: remove(index) or remove(element)


o Removes the element at index or the specified element.
o Example: arrayList.remove(1); // Removes 20
 Size: size()
o Returns the number of elements in the ArrayList.
o Example: int size = arrayList.size(); // 2

 Clear List: clear()


o Removes all elements from the list.
o Example: arrayList.clear(); // Clears the entire list

 Check Presence: contains(element)


o Checks if the ArrayList contains the specified element.
o Example: boolean contains10 = arrayList.contains(10);

2. HashMap

A HashMap is a collection that stores data in key-value pairs, allowing fast retrieval,
insertion, and deletion based on keys.

 Syntax: HashMap<Key, Value> map = new HashMap<>();

 Example:

java
Copy code
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Apple"); // key = 1, value = "Apple"

Operations:

 Add/Update: put(key, value)

o Associates key with value.


o Example: map.put(2, "Banana");

 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);

 Check Value Presence: containsValue(value)


o Checks if a value is present in the map.
o Example: boolean hasValue = map.containsValue("Apple");

3. HashSet

A HashSet stores unique elements without duplicates and provides fast access and
modification.

 Syntax: HashSet<Type> set = new HashSet<>();

 Example:

java
Copy code
HashSet<String> set = new HashSet<>();
set.add("Apple"); // Adds "Apple" to the set

Operations:

 Add Element: add(element)


o Adds element to the set.
o Example: set.add("Banana");
 Remove Element: remove(element)
o Removes element from the set.
o Example: set.remove("Apple");
 Check Presence: contains(element)
o Checks if element is present in the set.
o Example: boolean hasElement = set.contains("Banana");

 Size: size()
o Returns the number of elements in the set.
o Example: int size = set.size();

4. String

A String is a sequence of characters that is immutable, meaning it cannot be


modified once created.

 Syntax: String str = "Hello";

 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'

 Substring: substring(start, end)

o Returns a substring from start to end - 1.

o Example: String sub = str.substring(0, 3); // "Hel"

 Concatenate: concat(otherString)
o Concatenates otherString to the original string.
o Example: String result = str.concat(" World"); // "Hello
World"

 Equality Check: equals(otherString)


o Compares two strings for equality.
o Example: boolean isEqual = str.equals("Hello");

5. StringBuilder

StringBuilder is a mutable sequence of characters that can be modified, allowing


for efficient string manipulations.

 Syntax: StringBuilder sb = new 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"

 Insert: insert(index, value)

o Inserts value at the specified index.


o Example: sb.insert(5, ","); // "Hello, World"

 Delete: delete(start, end)

o Removes characters from start to end - 1.

o Example: sb.delete(5, 6); // "Hello World"

 Convert to String: toString()


o Converts the StringBuilder to a String.
o Example: String str = sb.toString();

 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.

 Syntax: List<Type> list = new ArrayList<>();

 Example:

java
Copy code
List<String> list = new ArrayList<>();
list.add("Apple");

Operations:

 Add Element: add(element)


o Adds element to the list.
o Example: list.add("Banana");
 Access Element: get(index)
o Retrieves the element at the specified index.
o Example: String fruit = list.get(0);

 Remove Element: remove(index) or remove(element)


o Removes the element at index or element.
o Example: list.remove(0);
 Size: size()
o Returns the number of elements in the list.
o Example: int size = list.size();

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.

 Syntax: Stack<Type> stack = new Stack<>();

 Example:

java
Copy code
Stack<Integer> stack = new Stack<>();
stack.push(1);
Operations:

 Push Element: push(element)


o Adds element to the top of the stack.
o Example: stack.push(2);
 Pop Element: pop()
o Removes and returns the top element of the stack.
o Example: int top = stack.pop();

 Peek (Top Element): peek()


o Returns the top element without removing it.
o Example: int top = stack.peek();

 Check Empty: isEmpty()


o Checks if the stack is empty.
o Example: boolean isEmpty = stack.isEmpty();

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.

 Syntax: Queue<Type> queue = new LinkedList<>();

 Example:

java
Copy code
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
Operations:

 Add (Enqueue): offer(element) or add(element)


o Adds element to the queue.
o Example: queue.offer(2);
 Remove (Dequeue): poll() or remove()
o Removes and returns the front element of the queue.
o Example: int front = queue.poll();

 Peek (Front Element): peek()


o Returns the front element without removing it.
o Example: int front = queue.peek();

 Check Empty: isEmpty()


o Checks if the queue is empty.
o Example: boolean isEmpty = queue.isEmpty();

9. Deque

A Deque (double-ended queue) allows elements to be added or removed from both


ends.

 Syntax: Deque<Type> deque = new LinkedList<>();

 Example:

java
Copy code
Deque<String> deque = new LinkedList<>();
deque.addFirst("First");
Operations:

 Add First: addFirst(element)


o Adds element to the front of the deque.
o Example: deque.addFirst("Second");
 Add Last: addLast(element)
o Adds element to the end of the deque.
o Example: deque.addLast("Third");
 Remove First: removeFirst()
o Removes the first element.
o Example: String first = deque.removeFirst();

 Remove Last: removeLast()


o Removes the last element.
o Example: String last = deque.removeLast();

You might also like