[go: up one dir, main page]

0% found this document useful (0 votes)
6 views3 pages

? Java Collections PDF – All Methods and Use Cases

The document provides examples of methods for List, Set, and Map data structures in Java. It includes methods such as add, get, set, remove, and contains, along with real-world examples like maintaining cart items, storing unique voter IDs, and storing student marks. Each section demonstrates how to use these methods effectively with sample code snippets.

Uploaded by

shobhitgupta2300
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

? Java Collections PDF – All Methods and Use Cases

The document provides examples of methods for List, Set, and Map data structures in Java. It includes methods such as add, get, set, remove, and contains, along with real-world examples like maintaining cart items, storing unique voter IDs, and storing student marks. Each section demonstrates how to use these methods effectively with sample code snippets.

Uploaded by

shobhitgupta2300
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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"));

You might also like