Java Utilities-
These are the pre-defined functions or statements in a program.
All utilities are stored in util package.
Syntax-
import java.util.Utility_name;
1. ArrayList-
ArrayList is the utility which is used to store the elements of any type, it stores all elements(even duplicates)
In ArrayList, all the elements are stored in String format.
How to include the utility in a program-
import java.util.ArrayList;
How to create an object of ArrayList-
ArrayList<String > <object> = new ArrayList<String>();
add() :-
add() is used to add the elements in ArrayList.
<object>.add("element");
System.out.println(<object>); // to display the elements
Note:- We can add any type of element in ArrayList we just need to put it inside the double quotes.
get() :-
get() is used to display the desired element using its index position.
System.out.println(<object>.get(index));
As it is known that index starts from 0 here ,so we can give the index position to display the elements.
set() :-
set() is used to insert an element in an specific position.
<object>.set(index,"element");
remove() :-
<object>.remove("element");
It is used to remove the specific element.
clear() :-
It is used to delete the entire ArrayList.
<object>.clear();
size() :-
It is used to display the size of the ArrayList.
<object>.size();
Displaying elements through loop:-
for(int i=0;i<a1.size();i++)
{
System.out.println(a1.get(i));
}
For The Example Click here- ArrayList Program Using get(), set() and add()
2. HashSet-
HashSet is the utility which is used to store the elements of any type, it stores only unique elements
In HashSet, all the elements are stored in String format.
How to include the utility in a program-
import java.util.HashSet;
How to create an object of HashSet-
HashSet<String > <object> = new HashSet<String>();
add() :-
add() is used to add the elements in HashSet.
<object>.add("element");
System.out.println(<object>); // to display the elements
Note:- We can add any type of element in Hashset we just need to put it inside the double quotes.
contains() :-
contains() is used to display whether the element is present in the Hashset or not.
It returns true or false.
System.out.println(<object>.contains("elements"));
remove() :-
<object>.remove("element");
It is used to remove the specific element.
clear() :-
It is used to delete the entire HashSet.
<object>.clear();
size() :-
It is used to display the size of the HashSet.
<object>.size();
For The Example Click here- HashSet program using all functions
3. LinkedList :-
LinkedList is a linear data structure.
It is made up of NODES.
NODES consists of Two parts-
1. Data part(information)
2. Address part(address of next node)
For The Example Click here- LinkedList Program
Comments
Post a Comment