Linear Data structures
commonly used linear data structures.
🔭Types of Data Structures:-
Data structures are divided into two categories:
- Linear data structures
- Non-linear data structures
Linear data structures:-
In linear data structures, elements are arranged in sequence one after the other.
Array:-
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together.
int[] intArray = new int[20];
For dynamic use of arrays, we can use ArrayList:-
ArrayList<Integer> arrli = new ArrayList<Integer>();
//EmptyArrayListArrayList<Integer> arrc = new ArrayList<Integer>(C);
//ArrayList with C collectionsArrayList<Integer> arrli = new ArrayList<Integer>();
//EmptyArrayList with int n capacity//common used functions
arrli.add(5);
arrli.remove(5);
arrli.set(index,element);//replaces the element
arrli.set(index,element);//inserts the element
Stack:-
It works in the LIFO(last in first out) principle. I use to remember this structure like placing all C.D’s one by one in the holder while searching we need to take out last in C.D. (OR) Pack of tennis balls.
Stack stack = new Stack(); // can push any datatype
Stack<String> stack1 = new Stack<String>(); // only String😁
//operations
stack.push(2);// adding element 2
stack.push(3);// adding element 3
stack.pop();// returns top element and removes it---3
stack.peek();//returns top element---2
stack.search(2);//returns index of element---0
Queue:-
It works in the FIFO(First in first out) principle. I use to remember this structure using the movie tickets queue, bus tickets queue. both the implementations, the PriorityQueue and LinkedList are not thread-safe. PriorityBlockingQueue is one alternative implementation if the thread-safe implementation is needed.
Queue<Integer> q = new LinkedList<>();// inherates LinkedList ds
Queue<String> pq = new PriorityQueue<>();//objects based on priority
Queue<Integer> pbq = new PriorityBlockingQueue<Integer>();//
q.peek();//returns top element
q.poll();//returns top element and removes it
LinkedList:-
In a linked list data structure, data elements are connected through a series of nodes. And, each node contains the data items and address to the next node.
LinkedList<String> ll = new LinkedList<String>(); ll.add("A")
ll.add("B");
ll.addLast("C");
ll.addFirst("D");
ll.add(2, "E");
System.out.println(ll);//[D, A, E, B, C]
ll.remove("B");
ll.remove(3);
ll.removeFirst();
ll.removeLast();
System.out.println(ll);//[A]
ll.set(0, "For");//[For]
HashMap:-
To Store Data in key-value pair, we will be using HashMap. We can access them by an index of another type. One Object is used as a key (index) to another Object(value). It provides 4 constructors.
- HashMap() // initial capacity of 16 and load factor of 0.75.
- HashMap(int initialCapacity)
- HashMap(int initialCapacity, float loadFactor)
- HashMap(Map map)
// Using Map in constructor
Map<Integer, String> hm1 = new HashMap<>();
hm1.put(1, "one");
hm1.put(2, "two");
hm1.put(3, "three");
HashMap<Integer, String> hm2 = new HashMap<Integer, String>(hm1);
Operations →
public class TraversalTheHashMap {public static void main(String[] args){HashMap<String, Integer> map = new HashMap<>();map.put("vishal", 10);
map.put("sachin", 30);
map.put("vaibhav", 20);
map.put("remove", 40);
map.remove("remove");for (Map.Entry<String, Integer> e : map.entrySet())System.out.println("Key: " + e.getKey()+ " Value: " + e.getValue());}}//from https://www.geeksforgeeks.org/java-util-hashmap-in-java-with-examples/
Tree Map:-
Java TreeMap class is a red-black tree based implementation. It provides an efficient means of storing key-value pairs in sorted order.
Set :-
An unordered collection or list in which duplicates are not allowed is referred to as a collection interface. The set interface is used to create the mathematical set. The set interface uses the collection interface’s methods to avoid the insertion of the same elements. SortedSet and NavigableSet are two interfaces that extend the set implementation.
Set<String> hash_Set = new HashSet<String>();
methods:-
add(element) //adding element to set
addAll(collection) //adding all elements in set hash_Set to Set OR UNION.
retainAll(collection) //retain all the elements from the set which are mentioned in the given collection.
removeAll(collection) //remove all the elements from the set which are mentioned in the given collection.
Conclusion:-
These are the commonly used linear data structures in Java.