Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, List, Set, and Map are part of the Java Collections Framework, which provides a set of interfaces and classes to store and manipulate groups of data as a single unit. Here’s a deep dive into each:

List

A ‘List‘ is an ordered collection that allows duplicate elements. It is indexed-based, allowing random access to elements.

Key Interfaces and Classes:

  • Interface: java.util.List
  • Implementations: ArrayList, LinkedList, Vector, Stack

Characteristics:

  • Order: Maintains the insertion order.
  • Duplicates: Allows duplicate elements.
  • Index-Based: Elements can be accessed by their index.

Common Methods:

  • add(E e): Appends the specified element to the end of the list.
  • get(int index): Returns the element at the specified position in the list.
  • set(int index, E element): Replaces the element at the specified position with the specified element.
  • remove(int index): Removes the element at the specified position in the list.
  • size(): Returns the number of elements in the list.
  • contains(Object o): Returns true if the list contains the specified element.
  • clear(): Removes all elements from the list.

Example:

				
					List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Apple"); // Allows duplicates
System.out.println(list.get(1)); // Outputs: Banana

				
			

Set

A ‘Set‘ is a collection that does not allow duplicate elements. It models the mathematical set abstraction.

Key Interfaces and Classes:

  • Interface: java.util.Set
  • Implementations: HashSet, LinkedHashSet, TreeSet

Characteristics:

  • Order: Does not maintain any order (HashSet), maintains insertion order (LinkedHashSet), or sorts elements (TreeSet).
  • Duplicates: Does not allow duplicate elements.

Common Methods:

  • add(E e): Adds the specified element to the set if it is not already present.
  • remove(Object o): Removes the specified element from the set if it is present.
  • size(): Returns the number of elements in the set.
  • contains(Object o): Returns true if the set contains the specified element.
  • clear(): Removes all elements from the set.

Example:

				
					Set<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // Does not allow duplicates
System.out.println(set.size()); // Outputs: 2

				
			

Map

A Map is an object that maps keys to values. It cannot contain duplicate keys, and each key can map to at most one value.

Key Interfaces and Classes:

  • Interface: java.util.Map
  • Implementations: HashMap, LinkedHashMap, TreeMap, Hashtable

Characteristics:

  • Order: Does not maintain any order (HashMap), maintains insertion order (LinkedHashMap), or sorts keys (TreeMap).
  • Keys: Each key is unique.
  • Values: Values can be duplicated.

Common Methods:

  • put(K key, V value): Associates the specified value with the specified key in the map.
  • get(Object key): Returns the value to which the specified key is mapped.
  • remove(Object key): Removes the mapping for the specified key from the map.
  • size(): Returns the number of key-value mappings in the map.
  • containsKey(Object key): Returns true if the map contains a mapping for the specified key.
  • containsValue(Object value): Returns true if the map contains one or more mappings for the specified value.
  • keySet(): Returns a set view of the keys contained in the map.
  • values(): Returns a collection view of the values contained in the map.

Example:

				
					Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Apple", 3); // Replaces the previous value
System.out.println(map.get("Apple")); // Outputs: 3

				
			
  • List: Ordered collection, allows duplicates, index-based.
  • Set: Unordered (or ordered depending on implementation), no duplicates.
  • Map: Key-value pairs, no duplicate keys, values can be duplicated.
Scroll to Top