1.
List Methods with Examples
Method: add(element)
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
System.out.println(fruits); // [Apple, Banana]
Method: get(index)
String fruit = fruits.get(0); // Apple
Method: set(index, element)
fruits.set(1, "Mango"); System.out.println(fruits);
// [Apple, Mango] Method: remove(index/object)
fruits.remove("Apple");
System.out.println(fruits); // [Mango]
Method: contains(object)
System.out.println(fruits.contains("Mango")); // true
Method: isEmpty()
System.out.println(fruits.isEmpty()); // false
Method: clear()
fruits.clear();
System.out.println(fruits); // []
Real Example: Maintain Cart Items in Order
List<String> cart = new ArrayList<>();
cart.add("Shirt");
cart.add("Jeans");
cart.add("Shoes");
System.out.println("Items in Cart: " + cart);
2.Set Methods with Examples
Method: add(element)
Set<Integer> numbers = new HashSet<>();
numbers.add(10);
numbers.add(20);
numbers.add(10); // Duplicate ignored
System.out.println(numbers); // [10, 20]
Method: contains(object)
System.out.println(numbers.contains(20)); // true
Method: remove(object)
numbers.remove(10);
System.out.println(numbers); // [20]
Method: isEmpty(), clear(), size()
System.out.println(numbers.isEmpty()); // false
System.out.println(numbers.size()); // 1
numbers.clear();
Real Example: Store Unique Voter IDs
Set<String> voterIds = new HashSet<>();
voterIds.add("VOTER123");
voterIds.add("VOTER456");
voterIds.add("VOTER123"); // Duplicate won't be added
System.out.println(voterIds);
3.Map Methods with Examples
Method: put(key, value)
Map<String, String> countryCapital = new HashMap<>();
countryCapital.put("India", "Delhi"); countryCapital.put("USA",
"Washington DC");
Method: get(key)
System.out.println(countryCapital.get("India")); // Delhi
Method: containsKey(key), containsValue(value)
System.out.println(countryCapital.containsKey("USA")); // true
System.out.println(countryCapital.containsValue("Delhi")); // true Method:
keySet(), values(), entrySet()
System.out.println(countryCapital.keySet()); // [India, USA]
System.out.println(countryCapital.values()); // [Delhi, Washington DC]
System.out.println(countryCapital.entrySet()); // [India=Delhi, USA=Washington DC]
Method: remove(key)
countryCapital.remove("USA");
Real Example: Storing Student Marks
Map<String, Integer> studentMarks = new HashMap<>();
studentMarks.put("Suresh", 85);
studentMarks.put("Ramesh", 92);
System.out.println("Marks of Ramesh: " + studentMarks.get("Ramesh"));