Professional Documents
Culture Documents
DURAME CAMPUS
FACULTY OF ENGINEERING AND TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY
Name: ID
Mitin Tadesse 520
1
1; 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.
Java Collections are the one-stop solutions for all the data manipulation jobs such as
storing data, searching, sorting, insertion, deletion, and updating of data. Java collection
responds as a single object, and a Java Collection Framework provides various
Interfaces and Classes.
Any group of individual objects which are represented as a single unit is known as a
collection of objects. In Java, a separate framework named the “Collection Framework”
has been defined in JDK 1.2 which holds all the collection classes and interface in it.
Java Collections are the one-stop solutions for all the data manipulation jobs such as
storing data, searching, sorting, insertion, deletion, and updating object.
Example:
2
10. // Now add the elements in the List
11. al.add("BMW");
12. al.add("Hundai");
13. al.add("Toyota");
14. al.add("Swift");
15.
16. // Iterating the List
17. // element using for-each loop
18. for (String cars : al)
19. System.out.println(cars);
20. }
21. }
Output:
BMW
Hundai
Toyota
Swift
What is Set?
The Set is present in Java and uses an unordered approach. Java's collection interface
enhances with the util package. In the Set, duplicate items will not be printed in the
output and will not be taken into account. For a better understanding, let's look at an
example where you will see how to add items using a set interface in Java. Let's look at
it.
xample:
3
9. // Set demonstration via using HashSet
10. Set<String> Set = new HashSet<String>();
11.
12. // Adding some Elements
13. Set.add("Java");
14. Set.add("Python");
15. Set.add("DBMS");
16. Set.add("Machine Learning");
17. Set.add("Operating System");
18.
19. // Here Set follows unordered way.
20. System.out.println(Set);
21. }
22. }
Output:
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:
4
8. // Creating object for a Map.
9. Map<String, Integer> map
10. = new HashMap< String, Integer>();
11.
12. // Adding Elements using Map.
13. map.put( "Rajat", 101);
14. map.put("Shyam", 102);
15. map.put("Rahul", 103);
16. map.put("Krishna", 104);
17. // here, elements may traverse in any order
18. for (Map.Entry m : map.entrySet()) {
19. System.out.println(m.getKey() + " "
20. + m.getValue());
21. }
22. }
23. }
Output:
Rahul 103
Shyam 102
Krishna 104
Rajat 101
The elements can be Duplicate elements are not Duplicate elements are not allowed on
duplicated in the list interface. permitted in a set. the map.
The list preserves the order of There is no insertion order Furthermore, there is no insertion order
inclusion. maintained by sets. maintained by the map.
Any quantity of null values can But there is only really one Null values can be present in any number
be added. null value in the set. and up to one null key in the map.
LinkedList and Array List are The classes used to implement HashMap, HashTable,
5
classes used for list sets include HashSet, ConcurrentHashMap, and
implementation. LinkedHashSet, and TreeSet. LinkedHashMap are the different map
implementation classes.
The list has a get() method that The elements at a given index The map provides no get method to
allows you to retrieve an entry cannot be retrieved from a set retrieve the elements at a certain index.
by index. using the get method.
If you regularly need to access Set can be used to build a The map can be used to store data in the
the elements using the index, collection of distinct elements form of key-value pairs.
you can utilise the list. if you wish to.
Advantages of Set:
Set can be used to store unique values in order to avoid duplications of
elements present in the set.
Elements in a set are stored in a sorted fashion which makes it efficient.
Set are dynamic, so there is no error of overflowing of the set.
Searching operation takes O(logN) time complexity.
Sets provide fast and efficient operations for checking if an element is
present in the set or not.
Sets can be implemented using different data structures, such as HashSets
and TreeSets, each with its own advantages and use cases.
Sets can be used in a variety of applications, including algorithms, data
analysis, and databases.
Disadvantages of Set:
Elements in a set can only be accessed with pointers, there is no indexing in
set like arrays.
Set is very complex to implement because of its structure and properties.
A set takes O(logN) time complexity for basic operations like insertion and
deletion.
Not suitable for large data sets.
Sets can only store elements of a specific data type.
Sets can use more memory than other data structures, such as arrays or
lists, because they store each element in a separate location.
Advantages and Disadvantages of Linked List
Advantages Disadvantages
6
Advantages Disadvantages
Advantages Disadvantages
7
Example:
Let us suppose that our list contains
{"Geeks For Geeks", "Friends", "Dear", "Is", "Superb"}
Java Beans are reusable software components that adhere to a specific set of conventions and
guidelines defined by Sun Microsystems (now Oracle). They are essentially Java classes that
encapsulate data and functionality, making them easily accessible and manageable.
1. //Employee.java
2.
3. package mypack;
4. public class Employee implements java.io.Serializable{
5. private int id;
6. private String name;
7. public Employee(){}
8
8. public void setId(int id){this.id=id;}
9. public int getId(){return id;}
10. public void setName(String name){this.name=name;}
11. public String getName(){return name;}
12. }