Skip to content

Instantly share code, notes, and snippets.

@ahmedeltaher
Last active October 25, 2021 06:48
Show Gist options
  • Save ahmedeltaher/466a8e793fef4b6360dc46bb14d83458 to your computer and use it in GitHub Desktop.
Save ahmedeltaher/466a8e793fef4b6360dc46bb14d83458 to your computer and use it in GitHub Desktop.
DataStructure in Java
package com.algoritms;
import org.junit.Test;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
public class DataStructure {
@Test
public void testDataStructure() {
// Array
int intArray[] = new int[3];
int[] intArray2 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[][] intArray2d = new int[10][20]; //a 2D array
int intArray2d2[][] = new int[][]{{1, 2, 3}, {4, 5}};
int[][][] intArray3d = new int[10][20][10]; //a 3D array
int intArray2d3[][] = {{1, 2, 3}, {4, 5}};
/**************************************************************************************/
// List
List<String> arrayList = new ArrayList<>();
List<String> arrayList2 = new ArrayList<>();
List<Object> arrayListObjects = new ArrayList<>();
arrayList.add("Steve");
arrayList.add("Jhon");
arrayList.add("Maria");
arrayList.get(0);
arrayList.set(0, "Ahmed");
arrayList.remove("Jhon");
arrayList.size();
arrayList.clear();
arrayList.addAll(arrayList2);
/**************************************************************************************/
//Stack
Stack<Object> stack = new Stack<>();
stack.isEmpty();
stack.push("item1");
stack.pop(); // remove and return the top element
stack.peek(); //return the top element
stack.search("item1"); // return index
stack.size();//return the size of the stack
stack.clear();
Iterator iterator = stack.iterator();
while (iterator.hasNext()) {
Object value = iterator.next(); // get the value
}
while (!stack.isEmpty()) {
Object value = stack.pop();
}
/**************************************************************************************/
//Queue
Queue<Object> queue = new LinkedList<Object>();
queue.add("item1");// return boolean, throw exception if the queue is full
queue.peek(); // return item from the head, or null if the queue is Empty
Object myObjectPoll = queue.poll(); // return null , if the queue is Empty
queue.contains("item1"); // return boolean
queue.clear();
queue.size();
Iterator iterator1 = queue.iterator();
while (iterator1.hasNext()) {
Object value = iterator1.next();
}
while (!queue.isEmpty()) {
queue.poll();
}
for (Object anObject : queue) {
//do someting to anObject...
}
Object myObjectRemove = queue.remove(); // throw exception if the queue is Empty
queue.offer("item1");// return false if the queue is full // same as add
queue.element(); //return the head element from the queue ,throw exception if the queue is Empty
/**************************************************************************************/
//DeQueue
Deque<String> deque = new LinkedList<>();
Deque<String> dq = new ArrayDeque<String>();
// We can add elements to the queue
// in various ways
deque.add("Element 1 (Tail)"); // Add at the last
deque.addFirst("Element 2 (Head)"); // Add at the first
deque.addLast("Element 3 (Tail)"); // Add at the last
deque.pollFirst(); //remove from head
deque.pollLast(); // remove from tail
Iterator itr = dq.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
Iterator descendingIterator = dq.descendingIterator();
while (descendingIterator.hasNext()) {
System.out.print(descendingIterator.next() + " ");
}
deque.push("Element 4 (Head)"); // Add at the first
deque.offer("Element 5 (Tail)"); // Add at the last
deque.offerFirst("Element 6 (Head)"); // Add at the first
/**************************************************************************************/
//HashSet
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
//Scenario 1
Set<Integer> set1 = new HashSet<>(list);
System.out.println(set1);
//Scenario 2
Set<Integer> set2 = new HashSet<>(list);
Collections.addAll(set2, 1, 2, 3, 4, 5, 6);
System.out.println(set2);
//Scenario 3
Set<Integer> set3 = Collections.unmodifiableSet(set2);
System.out.println(set3);
set1.add(1); // add to set.
set1.remove(2); //remove from the set
set2.clear();
set1.retainAll(list);
set1.removeAll(list);
/**************************************************************************************/
//HashMap
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.get(2);
map.clear();
map.containsKey(1);
map.containsValue("one");
map.size();
map.isEmpty();
map.getOrDefault(200, "");
map.putAll(map); // add another map to the current map.
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
/**************************************************************************************/
//PriorityQueue
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> pq = new PriorityQueue<>(5, (o1, o2) -> {
if (o1 < o1) return 1;
else if (o1 > o1) return -1;
return 0;
});
PriorityQueue<Integer> pQueue = new PriorityQueue<>(); // you can the size of pq
pQueue.add(10);
pQueue.add(20);
pQueue.add(15); // Adding items to the pQueue using add()
Integer value = pQueue.peek(); // Printing the top element of PriorityQueue
Integer value2 = pQueue.poll();// Printing the top element and removing it ,
// from the PriorityQueue container
pQueue.size();
pQueue.clear();
/**************************************************************************************/
//Sort Arrays
int[] arr = { 13, 7, 6, 45, 21, 9, 101, 102 };
Arrays.sort(arr); //ascending order
Arrays.sort(arr, 1, 5);
Arrays.sort(arr, Collections.reverseOrder()); //descending order
/**************************************************************************************/
//Sort Arrays
List list = new ArrayList();
Collections.sort(list);
ArrayList<String> al = new ArrayList<String>();
Collections.sort(al); //ascending order
Collections.sort(al, Collections.reverseOrder()); //descending order
Collections.sort(listDevs, new Comparator<Developer>() {
@Override
public int compare(Developer o1, Developer o2) {
return o1.getAge() - o2.getAge();
/**
A negative value means that the first object was smaller than second object.
The value 0 means the two objects are equal.
A positive value means that the first object was larger than the second object.
**/
}
});
}
Collections.sort(list, (s1, s2) -> {
if(s1.length()>s2.length()) return 1;
return 0;
});
}
public class MainClass {
public static void main(String[] args){
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment