FG DC

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

1.

String> Set = new HashSet<String>();


2.
3. // Adding some Elements
4. Set.add("Java");
5. Set.add("Python");
6. Set.add("DBMS");
7. Set.add("Machine Learning");
8. Set.add("Operating System");
9.
 Sorting: Sets do not provide specific sorting methods directly. However,
you can convert a set to a List and then use the sorting methods
available for lists. For example, you can use the ArrayList class to convert
a set to a list and then sort it using the Collections.sort() method.
 Searching: To search for an element in a set, you can use
the contains() method. It returns true if the set contains the specified
element, and false otherwise.
 List:

 Sorting: The List interface provides a sort() method that allows you to
sort the elements of the list in their natural order or using a
custom Comparator to define the sorting order.
 Searching: The List interface provides several methods for searching,
such as:
 contains(): Returns true if the list contains the specified element.
 indexOf(): Returns the index of the first occurrence of the
specified element, or -1 if the element is not found.
 lastIndexOf(): Returns the index of the last occurrence of the
specified element, or -1 if the element is not found.
 binarySearch(): Searches for the specified element using a binary
search algorithm. The list must be sorted in ascending order for this
method to work correctly.
Map:

10. Sorting: Maps are not directly sortable since they are key-value pairs. However, you c
Advantages:
 Ordered Collection: Lists maintain the order of elements as
they are inserted and allow positional access to elements
through indexing.
 Flexible Size: Lists can dynamically grow or shrink as elements
are added or removed, making them suitable for situations
where the size of the collection may change.
xDisadvantages:
 Linear Search: Searching for an element in a list can be slower
compared to other data structures like maps, especially for large lists. It
performs a linear search through the elements until a match is found.
 Map:
Advantages:
 Key-Value Pairing: Maps store elements as key-value pairs, allowing
efficient lookup and retrieval of values based on their associated keys.
 Fast Retrieval: Maps provide fast retrieval of values based on the key.
They use hashing techniques, making lookups faster than linear
searching.
Disadvantages:
11. Unique Keys: Maps require unique keys, and attempting to insert a duplicate key will
overwrite the existing entry. If you need to store multiple values with the same key, a
different approach is required, such as using a MultiMap
12. ) Discuss briefly about
java collection (set, list and
map), compare and contrast
each collection and
advantage and dis advantage
and explain available
methods to sort and search
from each collections.
13.
14.
15. What is java collection?
16.
17. A collection refers to a group of objects
that are gathered and stored together
as a single unit. Java provides the Java
Collections Framework, which is a built-
in library that offers a set of interfaces,
classes, and algorithms to manipulate
and organize collections of objects.
18.
19. The Java Collections Framework
provides a unified and standardized
way of working with collections, making
it easier for developers to handle
groups of objects efficiently. It includes
a hierarchy of interfaces, with the key
interfaces
being Collection, List, Set, Map, and
other
20.
21. What is List interface?
22. The Java collections interface's sub-
interface is called the List interface. It
offers index-based ways to add,
update, remove, and search for
elements. Duplicate
23. y.
24. System.out.println(Set);
25. }
26. }

dy
dx

[Java, Operating System, DBMS, Machine Learning, Python]

What is Map?

Java Map, or java.util.Map, is a java interface. A key and a value are


mapped by the term "map". A Java Map is able to hold
pairs of keys and values in more detail. An individual value is
connected to each key, and the value can later be retrieved using only
the key after being saved in a Map. For a better understanding, let's look
at an example where you can see how to add items in Java by utilising
the Map interface.
Example:

1. // A Java program to illustrate a Map.


2. // add elements using Map
3. import java.util.*;
4. class JavaMap {
5. public static void main(String args[])
6. {

You might also like