add(E e)Appends the specified element to the end of this list.
addAll(Collection<? extends E> c)Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator.
clear()Removes all of the elements from this list.
contains(Object o)Returns true if this list contains the specified element.
get(int index)Returns the element at the specified position in this list.
indexOf(Object o)Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
peek()Retrieves, but does not remove, the head (first element) of this list.
poll()Retrieves and removes the head (first element) of this list.
pop()Pops an element from the stack represented by this list.
push(E e)Pushes an element onto the stack represented by this list.
remove(int index)Removes the element at the specified position in this list.
removeLast()Removes and returns the last element from this list.
set(int index, E element)Replaces the element at the specified position in this list with the specified element.
size()Returns the number of elements in this list.
toArray()Returns an array containing all of the elements in this list in proper sequence (from first to last element).
Implementation
package com.example.demo1.LinkedList;
public class LinkedList {
Node head;
class Node {
Node next;
int data;
public Node(int d) {
this.data = d;
this.next = null;
}
}
public void push(int data) {
Node node = new Node(data);
node.next = head;
head = node;
}
public void printList() {
Node node = head;
int position = 0;
while (node != null) {
System.out.println(" Position " + position++ + " Data " + node.data);
node = node.next;
}
}
void deleteNode(int position) {
Node node = head;
if (position == 0) {
head = node.next;
return;
}
for (int i = 0; i < position - 1; i++) {
if (node == null) {
throw new RuntimeException("Out of Bounds");
}
node = node.next;
}
Node temp = node.next.next;
node.next = temp;
}
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.push(4);
linkedList.push(12);
linkedList.push(10);
linkedList.printList();
linkedList.deleteNode(1);
linkedList.printList();
}
}
ArrayList
Methods
add(E e)Appends the specified element to the end of this list.
addAll(Collection<? extends E> c)Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator.
clear()Removes all of the elements from this list.
clone()Returns a shallow copy of this ArrayList instance.
contains(Object o)Returns true if this list contains the specified element.
forEach(Consumer<? super E> action)Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
get(int index)Returns the element at the specified position in this list.
indexOf(Object o)Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
isEmpty()Returns true if this list contains no elements.
remove(int index)Removes the element at the specified position in this list.
remove(Object o)Removes the first occurrence of the specified element from this list, if it is present.
removeAll(Collection<?> c)Removes from this list all of its elements that are contained in the specified collection.
removeIf(Predicate<? super E> filter)Removes all of the elements of this collection that satisfy the given predicate.
removeRange(int fromIndex, int toIndex)Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
replaceAll(UnaryOperator<E> operator)Replaces each element of this list with the result of applying the operator to that element.
retainAll(Collection<?> c)Retains only the elements in this list that are contained in the specified collection.
set(int index, E element)Replaces the element at the specified position in this list with the specified element.
size()Returns the number of elements in this list.
sort(Comparator<? super E> c)Sorts this list according to the order induced by the specified Comparator.
toArray()Returns an array containing all of the elements in this list in proper sequence (from first to last element).
Implementation
package com.example.demo1.LinkedList;
import java.util.Arrays;
public class ArrayList<E> {
private static final Integer DEAFULT = 10;
private Object[] list = new Object[DEAFULT];
private int size = 0;
public void add(E e) {
if (size == list.length) {
addSize(list);
}
list[size++] = e;
}
private void addSize(Object[] list2) {
list = Arrays.copyOf(list2, size* 2);
}
public E get(int position) {
if (position < 0 || position >= list.length) {
throw new RuntimeException(" Out Of Bounds");
}
return (E)list[position];
}
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Jones");
arrayList.add("Alapat");
System.out.println(" Print the ArrayList " + arrayList.get(11));
}
}
HashMap
Methods
Implementation
package com.jonesjalapat.blog.tradesman.controller;
public class CustomHashMap<K, V> {
private static final int DEFAULT_CAPACITY = 10;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private int capacity;
private float loadFactor;
private int size;
private Node<K, V> []table;
public CustomHashMap() {
this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
}
public CustomHashMap(int capacity, float loadFactor) {
this.capacity = capacity;
this.loadFactor = loadFactor;
table = new Node[capacity];
}
public V get(K key) {
int index = hashcode(key);
Node<K, V> node = table[index];
while (node != null){
if (key.equals(node.key)) {
return node.value;
}
node = node.next;
}
return null;
}
public void put(K key, V value) {
int index = hashcode(key);
Node<K, V> node = table[index];
while (node != null){
if (key.equals(node.key)) {
node.value = value;
return;
}
node = node.next;
}
Node<K,V> newNode = new Node<>(key, value);
newNode.next = table[index];
table[index] = newNode;
size++;
if (size > (int) (capacity * loadFactor)) {
resize();
}
}
private void resize() {
int newCapacity = this.capacity * 2;
Node<K, V> []newTable = new Node[newCapacity];
for (int i = 0; i< capacity; i++) {
Node<K, V> node = table[i];
while (node != null) {
Node<K, V> next = node.next;
int index = hashcode(node.key);
node.next = newTable[index];
newTable[index] = node;
node = next;
}
table = newTable;
capacity = newCapacity;
}
}
public void remove(K key) {
int index = hashcode(key);
Node<K, V> node = table[index];
Node<K, V> prev = null;
while (node != null){
if (key.equals(node.key)) {
if ( prev == null) {
table[index] = node.next;
} else {
prev.next = node.next;
}
size--;
return;
}
prev = node;
node = node.next;
}
}
public int hashcode(K key) {
return key.hashCode() % capacity;
}
private static class Node<K,V> {
public K key;
public V value;
public Node<K,V> next;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
}
public static void main(String[] args) {
CustomHashMap customHashMap = new CustomHashMap();
for (int i=0; i< 12;i++) {
customHashMap.put(i, "Jones");
}
System.out.println(customHashMap.get(11));
customHashMap.put(11, "Anu");
System.out.println(customHashMap.get(11));
}
}