The primary purpose of the Collection Framework is used to store objects. It provides classes and interfaces such as List, ArrayList, Queue, Set, Vector, Stack, and HashSet, etc.
If two different keys are providing the same hash value that is called hash collision.
The default size of load factor is 0.75. The initial capacity is 16 so formula is
16 * 0.75 = 12
12 is the default capacity of Map.
public class ConvertArrayListtoArray { public static void main(String args[]) { List<
String> monthList = new ArrayList<>(); monthList.add("January"); monthList.add("February"); monthList.add("March"); monthList.add("April"); //Converting Arraylist to Array String[] array = monthList.toArray(new String[monthList.size()]); for(String s: array){ System.out.println(s); } } } Output: January February March April
public class ConvertArrayListtoArray { public static void main(String args[]) { List<
String> monthList = new ArrayList<>(); monthList.add("January"); monthList.add("February"); monthList.add("March"); monthList.add("April"); //Converting Arraylist to Array String[] array = monthList.toArray(new String[monthList.size()]); for(String s: array){ System.out.println(s); } List<
String> monthList1 = new ArrayList<>(); //Converting Array to Arraylist monthList1 = Arrays.asList(array); } } Output: January February March April
ArrayList can be Read-only by calling the Collections.unmodifiableCollection().
public class UnmodifiableCollectionExample { public static void main(String args[]) { List<
String> monthList = new ArrayList<>(); monthList.add("January"); monthList.add("February"); monthList.add("March"); monthList.add("April"); //Converting to a immutable Arraylist Collection<
String> unModifiedlist = Collections .unmodifiableCollection(monthList); //exception here unModifiedlist.add("May"); } } Output: Exception in thread "main" java.lang.UnsupportedOperationException
LinkedHashSet maintain insertion order also it can not store duplicate object.
Collections.reverse(list)
Collections.synchronizedList(list);
LinkedList is a linear data structure that consists of nodes holding a data field and a reference to another node.
Data structure is a logical way that defines how data will organize within a program. Data structure is a important function for the application's performance prospective.
public class SmallestLargestNumberArray { public static void main(String[] args){ Integer[] array = {4,7,2,8,2,300,23,50,250,450}; Integer samllest = array[0]; Integer largest = array[0]; for(int i=0;i< array.length;i++){ if(array[i]<
samllest){ samllest = array[i]; }else if(array[i]>largest){ largest = array[i]; } } System.out.println("Smallest number ="+samllest); System.out.println("Largest number ="+largest); } }
The Multi Dimensional array is an array that contains other arrays. Basically, the Multi Dimensional array is array of arrays.
public class MultiDimentationalArrayExample { public static void main(String args[]){ Integer[][] array = new Integer[10][20]; for(int i=0;i<
10;i++){ for(int j=0;j<
20;j++){ array[i][j] = i*j; } } } }
public class LinkedListCustom {
private Node head;
public LinkedListCustom(Node head) {
this.head = head;
}
public LinkedListCustom() {}
public void add(int data){
Node newElement = new Node(data);
if(this.head == null){
head = newElement;
}else {
Node currentNode = head;
while(currentNode.getNextNode() != null){
currentNode = currentNode.getNextNode();
}
currentNode.setNextNode(newElement);
}
}
public void addHead(int data){
Node newNode = new Node(data);
newNode.setNextNode(head);
head = newNode;
}
public void insertAt(int index, int data){
Node nodeToBeInserted = new Node(data);
Node node = head;
for(int i = 0; i< index -1; i++){
node = node.getNextNode();
}
nodeToBeInserted.setNextNode(node.getNextNode());
node.setNextNode(nodeToBeInserted);
}
public void deleteNodeAt(int index){
Node node = head;
for(int i = 0; i< index -1; i++){
node = node.getNextNode();
}
node.setNextNode(node.getNextNode().getNextNode());
}
public void display(){
if(head != null){
Node currentNode = head;
while(currentNode.getNextNode() != null){
System.out.println(currentNode.getData());
currentNode = currentNode.getNextNode();
}
System.out.println(currentNode.getData());
}
}
}
class Node{
private int data;
private Node nextNode;
public Node(int data) {
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
}
class DemoLinkedList{
public static void main(String args[]){
LinkedListCustom l = new LinkedListCustom();
l.add(6);
l.display();
}
}
public class HashMapCustomImplementation<
K, V> { private int capacity=16; private Entry<
K, V>[] table; public HashMapCustomImplementation(){ table = new Entry[capacity]; } public HashMapCustomImplementation(int capacity){ this.capacity = capacity; table = new Entry[capacity]; } private int index(K key){ if(key == null){ return 0; } return Math.abs(key.hashCode() % capacity); } public void put(K key, V value){ int index = index(key); Entry newEntry = new Entry(key, value, null); if(table[index] == null){ table[index] = newEntry; }else { Entry<
K, V> previousNode = null; Entry<
K, V> currentNode = table[index]; while(currentNode != null){ if(currentNode.getKey().equals(key)){ currentNode.setValue(value); break; } previousNode = currentNode; currentNode = currentNode.getNext(); } if(previousNode != null) previousNode.setNext(newEntry); } } public void display(){ for(int i = 0; i < capacity; i++){ if(table[i] != null){ Entry<
K, V> currentNode = table[i]; while (currentNode != null){ System.out.println(String.format("Key is %s and value is %s", currentNode.getKey(), currentNode.getValue())); currentNode = currentNode.getNext(); } } } } } class Entry<
K, V>{ private K key; private V value; private Entry<
K, V> next; public Entry(K key, V value, Entry<
K, V> next) { this.key = key; this.value = value; this.next = next; } public K getKey() { return key; } public void setKey(K key) { this.key = key; } public V getValue() { return value; } public void setValue(V value) { this.value = value; } public Entry<
K, V> getNext() { return next; } public void setNext(Entry<
K, V> next) { this.next = next; } } class DemoHashMap{ public static void main(String args[]){ HashMapCustomImplementation<
Integer, String> map = new HashMapCustomImplementation<
Integer, String>(); map.put(1,"One"); map.put(2,"Two"); map.display();; } }