30 July 2023

Collection Framework In Java - learngreen.net

 The Java Collections Framework is an effective and vital phase of Java programming. It offers a standardized and efficient way to store, manipulate, and system collections of objects. At its core, the Collections Framework consists of a set of interfaces, implementations, and algorithms that allow developers to work with different sorts of collections. The framework consists of interfaces such as List, Set, Queue, and Map, each serving one-of-a-kind purposes. For example, the List interface allows ordered collections, Set ensures the area of expertise of elements, Queue represents a queue-like statistics structure, and Map provides key-value pair mappings. Implementations of these interfaces, such as ArrayList, HashSet, LinkedList, and HashMap, offer extraordinary behaviors and overall performance characteristics. Additionally, the Collections Framework provides algorithms for frequent operations like sorting, searching, and filtering factors within collections. These algorithms can be applied to any collection that implements the terrific interface, making sure of code reusability and efficiency. By leveraging the Java Collections Framework, developers can build robust and environment-friendly applications, manage facts effectively, and leverage a wealth of performance provided through the framework.

   The Collection Framework in Java provides a unified and efficient way to deal with collections of objects. It consists of interfaces, implementations, and algorithms that allow developers to work with different types of collections.

Collection Framework In Java





 package Collections;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;

 public class ListExample {
 public static void main(String[] args) {
	 
 //ArrayList & LinkedList
 /* ArrayList can grow or shrink dynamically
 * as elements are added or removed.ArrayList
 * provides many other useful methods,
 * such as clear() to remove all elements, 
 * contains() to check if an element
 * exists, indexOf() to find the 
 * index of an element.You can refer to the
 * Java documentation for a complete list of 
 * methods available in 
 * the ArrayList class.
 */
	 
 ArrayList list= new ArrayList<>();
 list.add(85);
 list.add(842);
 list.remove(1);
 System.out.println(list);
 
 /* Instead, each element in a LinkedList
  * is represented by a node that contains
  * the element value and references to the
  * previous and next nodes.LinkedList
  * provides several other methods, such as
  * addFirst() and addLast() to insert elements
  * at the beginning or end of the list,
  * removeFirst() and removeLast() to remove 
  * the first or last element, indexOf() to find
  * the index of an element, and more.
  * You can refer to the Java documentation
  * for a complete list of methods available in 
  * the LinkedList class.
 */
		
 List linkedList = new LinkedList<>();
 linkedList.add("Dog");
 linkedList.add("Cat");
 linkedList.add("Elephant");
 System.out.println("LinkedList: " + linkedList);
 
 //Output-> [85]
 //         LinkedList: [Dog, Cat, Elephant]

	}
  }
 

 




 package Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.TreeMap;
 public class MapExample {
 public static void main(String[] args) {
	 
 /*
 * HashMap is a class in Java that implements
 * the Map interface.It represents a collection
 * of key-value pairs, where each key is unique.
 * It provides fast performance for basic
 * operations like insertion, retrieval, and 
 * removal of elements. HashMap uses hashing
 * techniques to store and retrieve elements
 * based on their keys.HashMap provides various
 * other useful methods, such as containsKey()
 * and containsValue() to check for key or value
 * existence, isEmpty() to check if the map is
 * empty,and keySet() to obtain a set of all keys.
 * You can refer to the Java documentation for
 * a complete list of methods available in the
 * HashMap class.The term "key" refers to the
 * unique identifier associated with a value 
 * in the map.The key is used as the identifier
 * to access and manipulate the associated value.
 */

 Map hashMap = new HashMap<>();
 hashMap.put("Apple", 10);
 hashMap.put("Banana", 5);
 hashMap.put("Cherry", 15);
 System.out.println("HashMap: " + hashMap);
 
 /* TreeMap is a class in Java that implements
  * the NavigableMap interface and represents
  * a sorted map. Similar to TreeSet, it uses
  * a self-balancing binary search tree,
  * specifically a red-black tree, to store
  * key-value pairs in sorted order based on
  * the keys. This allows for efficient
  * key-based operations with a logarithmic
  * time complexity.TreeMap provides various
  * other methods, such as firstKey()and
  * lastKey() to retrieve the first and last
  * keys, higherKey()and lowerKey() to get the
  * next higher or lower key, and more.
 */


 Map treeMap = new TreeMap<>();
 treeMap.put("John", 25);
 treeMap.put("Alice", 30);
 treeMap.put("Bob", 20);
 System.out.println("TreeMap: " + treeMap);
 //Output-> HashMap: {Apple=10, Cherry=15, Banana=5}
 //         TreeMap: {Alice=30, Bob=20, John=25}
	}
 }
 

 



 package Collections;
 import java.util.LinkedList;
 import java.util.PriorityQueue;
 import java.util.Queue;
 public class QueueExample {
 public static void main(String[] args) {
	 
 /* LinkedList is a class in Java that
  * implements the Queue interface,
  * making it a suitable choice for
  * implementing a queue data structure.
  * In this example, we create a Queue
  * using LinkedList by utilizing the
  * Queue interface. We add elements to
  * the queue using the add() method.
  * It also contains the method poll()
  * to remove elements, peek() method to
  * access the element at front of queue
  * and isEmpty() method to check if the
  * queue is empty.Note that LinkedList
  * can be used as both a Queue and a List,
  * providing flexibility in implementing
  * different data structures.
 */

 
 Queue linkedListQueue = new LinkedList<>();
 linkedListQueue.add("Apple");
 linkedListQueue.add("Banana");
 linkedListQueue.add("Cherry");
 System.out.println("LinkedList Queue: "+linkedListQueue);
       
 /* Elements in a PriorityQueue are
  * ordered based on their natural
  * ordering or a custom comparator
  * that is provided during initialization.
  * The elementwith the highest priority
  * is always at the front of the queue
  * and is the first to be removed when using
  * methods like poll() or remove().
  * PriorityQueue provides various other
  * methods, such as size() to obtain the
  * number of elements in the queue,
  * contains() to check if an element is
  * present,and more. You can refer to the
  * Java documentation for a complete list
  * of methods available in the
  * PriorityQueue class.
 */


  Queue priorityQueue = new PriorityQueue<>();
  priorityQueue.add(10);
  priorityQueue.add(20);
  priorityQueue.add(5);
  System.out.println("Priority Queue: " + priorityQueue);
  // Output-> LinkedList Queue: [Apple, Banana, Cherry]
  // Priority Queue: [5, 20, 10]
	}
  }
 

 





 package Collections;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.TreeSet;
 public class SetExample {
 public static void main(String[] args) {
	 
 /* HashSet represents an unordered collection
  * of unique elements.It uses a hash table data
  * structure to store elements and provides
  * constant-time performance for basic operations
  * like adding, removing, and checking for
  * element existence.HashSet provides various
  * other useful methods, such as clear()
  * to remove all elements, isEmpty() to
  * check if the set is empty,and addAll()
  * to add all elements from another collection.
  * You can refer to the Java documentation for
  * a complete list of methods available in
  * the HashSet class.
  */
	 
 Set hashSet = new HashSet<>();
 hashSet.add(10);
 hashSet.add(20);
 hashSet.add(30);
 System.out.println("HashSet: " + hashSet);
     
 /* TreeSet is a class in Java that implements
  * the NavigableSet interface and represents
  * a sorted set of elements. It uses
  * a self-balancing binary search tree,
  * specifically a red-black tree,to store
  * elements in sorted order. This allows for
  * efficient element retrieval and provides
  * logarithmictime complexity for basic
  * operations like adding, removing, and
  * searching for elements. TreeSet provides
  * various other methods, such as first()
  * and last()to retrieve the first and last
  * elements, higher()and lower() to get the
  * next higher or lower element,and more.
  * You can refer to the Java documentation for
  * a complete list of methods available in the
  * TreeSet class.
 */

 
 Set treeSet = new TreeSet<>();
 treeSet.add("John");
 treeSet.add("Alice");
 treeSet.add("Bob");
 System.out.println("TreeSet: " + treeSet);
  // HashSet: [20, 10, 30]
 //  TreeSet: [Alice, Bob, John]
	}
 }
 

 













No comments:

Post a Comment