You are on page 1of 9

WACHEMO UNIVERSITY

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.

What is List interface?


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
elements are also possible, and the null entries can also be kept in the list. Lists enable
positional access and the insertion of elements while maintaining the insertion order. It
was located in the package java.util. For a better understanding, let's look at an
illustration where you can see how to add elements in Java using the list interface.

Example:

1. // A Java Program to implement the addition of elements in the List


2. import java.util.*;
3. public class Javalist {
4. public static void main(String args[])
5. {
6.
7. // here create a List
8. List<String> al = new ArrayList<>();
9.

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:

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


2. // add Elements using Set.
3.
4. import java.util.*;
5. public class JavaSet {
6.
7. public static void main(String[] args)
8. {

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:

[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. {
7.

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

Difference between List, Set and Map in Java

List Set Map

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

Dynamic Size Dilution of Ownership

6
Advantages Disadvantages

Efficient Insertion and Deletion Complex Legal Structure

Memory Flexibility Lack of Direct Control

Versatility in Implementations Potential for Mismanagement

Efficient Traversal Share Price Volatility

the advantages and disadvantages of Map

Advantages Disadvantages

Helps with Navigation Can Be Inaccurate

Provides Information Can Be Misleading

Helps with Planning Can Be Confusing

Can Help Solve Problems Can Be Expensive

Promotes Understanding and Appreciation Can Be Outdated

java.util.Collections.sort() method is present in java.util.Collections class. It is


used to sort the elements present in the specified list of Collection in ascending order.
It works similar to java.util.Arrays.sort() method but it is better than as it can sort the
elements of Array as well as linked list, queue and many more present in it.
public static void sort(List myList)

myList : A List type object we want to sort.

This method doesn't return anything

7
Example:
Let us suppose that our list contains
{"Geeks For Geeks", "Friends", "Dear", "Is", "Superb"}

After using Collection.sort(), we obtain a sorted list as


{"Dear", "Friends", "Geeks For Geeks", "Is", "Superb"}

java.util.Collections.binarySearch() method is a java.util.Collections class


method that returns position of an object in a sorted list. binarySearch()
method is a java. util. Collections class method that returns position of an object
in a sorted list. // Returns index of key in sorted list sorted in // ascending order
public static int binarySearch(List slist, T key) // Returns index of key in sorted list
sorted in // order defined by Comparator c.

2 be;write everything you can find about java

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.

A JavaBean is a Java class that should follow the following


conventions:
o It should have a no-arg constructor.
o It should be Serializable.
o It should provide methods to set and get the values of the properties, known as
getter and setter methods.

Simple example of JavaBean class

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. }

You might also like