Professional Documents
Culture Documents
Description of program:
The following program helps you in inserting, removing and retrieving the data
from the NavigableMap. It uses the put() method to add the element. If you want
to retrieve the data at first and last position from the NavigableMap, you use the
firstEntry() and lastEntry() methods. The descendingMap() method represents
all data to the NavigableMap in descending order.
You can retrieve the nearest less than or equal to the given number and the
greatest key strictly less than the given number floorEntry() and lowerEntry()
methods. And you retrieve a key-value associated with the least key strictly
greater than the given key, you use the higherEntry() method. The
pollFirstEntry() method removes the first data from the NavigableMap and
pollLastEntry() method also removes the data at the last position from the
NavigableMap.
import java.util.*;
import java.util.concurrent.*;
Output of program:
C:\vinod\collection>javac NavigableMapExample.java
C:\vinod\collection>java NavigableMapExample
Navigable Map Example!
C:\vinod\collection>
In the example below we have used NavigableSet method to sort the elements in ascending
order, descending order, also to retrieve the element which is immediately greater than or equal
to 35 etc. With the help of NavigableSet methods its just a method call to get the result. It is used
to return the closest matches of elements for the given elements in the collection.
Description of program:
Inserting the data in the NavigableSet by the add() method. The NavigableSet
provides the facility to getting the data in both orders: ascending and descending
orders. The descendingSet() method returns the data from the NavigableSet in
descending order. If you want to get the data in ascending order must be used an
iterator. An iterator stores the data as a index and the hasNext() method returns
'true' until, the next() method returns the element in ascending order.
Here, you remove the element from the NavigableSet at first and last position,
you use the pollFirst() and pollLast() methods. Sometimes, you want to get the
less and greater than or equal to the given element by using the floor() and
ceiling() methods. For getting the all elements of the NavigableSet that is greater
than or equal to the given element by the tailSet() method and less than or equal
to the given element of the NavigableSet by the headSet() method.
C:\vinod\collection>java NavigableSetExample
Navigable set Example!
Remove element: 10
Now navigable set: [100, 80, 50, 30, 20]
C:\vinod\collection>
HashSet Example
In this section we are discussing HashSet with example code that shows the
methods to add, remove and iterate the values of collection. A HashSet is a collection
set that neither allows duplicate elements nor order or position its elements.
Description of program:
import java.util.*;
C:\vinod\collection>javac
CollectionTest.java
C:\vinod\collection>java
CollectionTest
Collection Example!
Collection is empty
C:\vinod\collection>
Description of program:
This program helps you in storing the large amount of data as a collection. The
LinkedList is a part of collection that constructs a list containing the elements of
the specified collection. Iterator methods returns the values in the order in which
they are stored.
If you want to insert the data in the linkedList then use add() method. The
hasNext() method returns true if the iterator contains more elements and the
next() method returns the next element in the iteration. To insert and remove the
data at first, last and specified position in the linkedList, you use the addFirst(),
addLast(), add(), removeFirst(), removeLast() and remove() methods. To
retrieve the element with respect to a specified position use the getFirst(),
getLast() and get() methods.
import java.util.*;
Output of program:
C:\vinod\collection>javac
LinkedListExample.java
C:\vinod\collection>java
LinkedListExample
Linked List Example!
Linked list data: 11 22 33 44
Linked list size: 4
Adding data at 1st location: 55
Now the list contain: 55 11 22 33
44
Now the size of list: 5
Adding data at last location: 66
Now the list contain: 55 11 22 33
44 66
Now the size of list: 6
Adding data at 3rd location: 55
Now the list contain: 55 11 99 22
33 44 66
Now the size of list: 7
First data: 55
Last data: 66
Data at 4th position: 22
Data removed from 1st location:
55
Now the list contain: 11 99 22 33
44 66
Now the size of list: 6
Data removed from last location:
66
Now the list contain: 11 99 22 33
44
Now the size of list: 5
Data removed from 2nd location:
99
Now the list contain: 11 22 33 44
Now the size of list: 4
Linked list is empty
C:\vinod\collection>
In the following example, we have used the TreeMap method, which stores its elements in a tree
and orders its elements based on their values. Here in the example we have used the key of the
element to show the values of the element. To retrieve the keys and values use keySet() and
values() method respectively.
This program shows the data elements left after removing the particular element by specifying its
key. Using the Iterator interface methods, we can traverse a collection from start to finish and
safely remove elements from the underlying Collection.
import java.util.*;
C:\vinod\collection>javac TreeMapExample.java
C:\vinod\collection>java TreeMapExample
Tree Map Example!
C:\vinod\collection>
In the following example, we have used the TreeSet collection, which is similar to TreeMap that
stores its elements in a tree and maintain order of its elements based on their values. To get the
size of TreeSet collection size() method is used. Our TreeSet collection contains 4 elements and
the size of the TreeSet can be determine by calling size() method.
Similarly, we have used first() and last() to retrieve first and last element present in the
TreeSet. Program also shows the method to remove the element and then display the remaining
elements. To remove all data from the TreeSet, use the clear() method. To determine whether
TreeSet is empty or not use isEmpty() method. If the TreeSet is empty, it displays the message
"Tree set is empty." otherwise it displays the size of TreeSet.
import java.util.*;
C:\vinod\collection>javac
TreeSetExample.java
C:\vinod\collection>java
TreeSetExample
Tree Set Example!
C:\vinod\collection>
Collections Framework
The Collection interface which is the collection of objects. That permits the
duplication of the value or objects.
Set is the interface of the collections framework which extends the
Collection but forbids duplicates.
An another interface is the List which also extends the Collection. It allows
the duplicates objects on different position because it introduces the
positional indexing.
And Map is also a interface of the collection framework which extends
neither Set nor Collection.
INTERFACES CLASSES
Collection Interface HashSet Class
Iterator Interface TreeSet Class
Set Interface ArrayList Class
List Interface LinkedList Class
ListIterator Interface HashMap Class
Map Interface TreeMap Class
SortedSet Interface Vector Class
SortedMap Interface Stack Class
Every interfaces and classes are explained next in the roseidia.net java tutorial one
by one in the various examples.
Interfaces of the collections framework are very easy to use. These interfaces
can be transparently substituted to increase the developed application
performance.
In this section, you will learn how to implement the queue. A queue holds a collection of data or
elements and follows the FIFO ( First In First Out) rule. The FIFO that means which data added
first in the list, only that element can be removed or retrieved first from the list. In other sense,
You can remove or perform operation on that data which had been added first in the Collection
(list). Whenever you need to remove the last added element then you must remove all these
elements which are entered before the certain element.
The given program implements a queue. It takes all elements as input by user. These values are
added to the list and shows first, last and the rest elements present in the list separately. Some
methods and APIs are explained below which have been used in the program for the certain
purposes:
LinkedList<Integer>():
This is the constructor of the LinkedList class. This class is used by importing
the java.util.*; package. This constructor is used for constructing an empty list. It can contain
integer types data in the given program because in the declaration of the LinkedList class type
checking has been used. This is an implementation of the List interface of the Collections
Framework. The LinkeedList class provides inserting and deleting the data to/from the list.
removeFirst():
Above method removes and returns the first element of the list.
removeLast():
Above method removes and returns the last element of the list.
list.isEmpty():
This method checks whether the list is empty or not.
remove():
This method used to remove the elements in the list in a specified sequence.
In this section, you will get the detailed explanation about the hasNext() method of
interface Iterator. We are going to use hasNext() method of interface Iterator in Java. The
description of the code is given below for the usage of the method.
Here, you will get to know about the hasNext() method through the following java program. True
is return by this method in case the iteration has more elements. This means that if the iteration
has more elements then the hasNext() method will return true rather than throwing an exception.
In the program code given below, we have taken a string of elements. We have converted this
string of elements into a list of array and then we have applied the hasNext() method which
returns true because there are more elements in the list. Hence we get the following output.
C:\unique>java
hasNext
true
C:\unique>
Here is the illustration for the conversion from the collection to an array. In this section, you will
learn how to do this. The given example gives you a brief introduction for convert collection to an
array without losing any data or element present in the collection.
This program creates a List. List is a type of collection that contains a ordered list of elements.
This list (collection) is converted into an array. Length of the created array is defined by the
system according to the number of elements in the list. Each and every subscripts hold separate
element.
Code Description:
Here is the explanation of some methods and APIs whatever has been used in the program:
List:
List is the class of the java.util package which creates a list for containing some elements. In this
program, this class is used with the type checking.
add():
Above method adds elements to the list. This method holds string which is the element name.
List.toArray(new String[0]):
Above code converts a list into a String Array. This method returns a string array which length is
fixed according to the number of elements present in the list. Overall this code converts collection
to an array.
Here is the illustration for the conversion from the an array to a collection. In this section, you will learn how h
Here is the illustration for the conversion from the an array to a collection. In this section, you will learn how how to do
This program creates an array, that contains all the elements entered by you. This array is converted into a list (collecti
Code Description:
Arrays.asList(name):
Above code converts an array to a collection. This method takes the name of the array as a parameter which has to be
import java.util.*;
import java.io.*;
In this section, you will learn how to implement a stack in Java. A Stack is like a bucket in which
you can put elements one-by-one in sequence and retrieve elements from the bucket according
to the sequence of the last entered element. Stack is a collection of data and follows
the LIFO (Last in, first out) rule that mean you can insert the elements one-by-one in sequence
and the last inserted element can be retrieved at once from the bucket. Elements are inserted
and retrieved to/from the stack through the push() and pop() method.
This program implements a stack and follows the LIFO rule. It asks you for the number of
elements which have to be entered in the stack and then it takes elements for insertion in the
stack. All the elements present in the stack are shown from the last inserted element to the first
inserted element.
Stack<Integer>():
Above constructor of the Stack class creates a empty stack which holds the integer type value
which is mention with the creation of stack. The Stack class extends the Vector class and both
classes are implemented from the java.util.*; package.
Stack.push(Object obj):
Above method is used to insert or push the data or element in the stack. It takes an object like:
data or elements and push its onto the top of the stack.
Stack.pop():
This is the method to removes the objects like: data or elements at the top positions of stack.
This section gives you the best illustration for sorting elements of a Collection. You can see how
to sort all elements of a Collection in ascending or descending order.
In this section, the given program sorts all the elements of a Collection in ascending order. If your
text starts with the Upper case letter then the text comes first that mean the uppercase letter is
less priority than lowercase letter.
Code Description:
This program creates an string type array and takes some text as input from the user for the
created array. Array with elements are assigned to the List (Collection) and then all the elements
of the list are arranged in the ascending order.
Collections.sort(list):
Above method sorts all the elements of the specified Collection. List name which elements have
to be sorted is passed through the method as a parameter. This method sorts elements in
ascending order by default.
This section explains the implementation of the hash table. What is the hash table and
how to create that?
This program simply asks you for the number of entries which have to entered into the hash table
and takes one-by-one key and it's value as input. It shows all the elements with the separate key.
Code Description:
hashTable.put(key, in.readLine()):
Above method puts the values in the hash table regarding to the unique key. This method takes
two arguments in which, one is the key and another one is the value for the separate key.
n the sorted array, searching is very easy. In this section, you will learn how to sort an
array and how to find a text in the sorted array.
In the sorted array, searching is very easy. In this section, you will learn how to sort an array and
how to find a text in the sorted array. For binary search first you must sort the array and then
apply the binary search.
This section gives you a example for understanding the sorting an array an searching an
elements in the array. This example takes some inputs from the user for the array and takes a
another text for search in the array after sorting that. And it show the given text position in the
array, if text is present in the array otherwise the program shows the message : "Given word is
not available in the array." and terminate the program.
Code Description:
Arrays.sort(names):
Above method sorts all the elements present in the array. This method takes the array name as a
parameter which elements have to be sorted and it sorts elements by default in ascending order.
Arrays.binarySearch(names, in.readLine()):
Above method searches the element in the specified array in the way of binary search. This way
of searching takes more less time than the index search or the sequential search. This method
takes two arguments as follows:
This method returns the positive value from 0 (zero) to length_of_array-1, if the given string is
available in the array and returned value is the position of the text in the array otherwise this
method returns the negative value that means given string is not available in the array.
This section shows the way of getting the current time of the system.
This section shows the way of getting the current time of the system.
In this section, through the given program you can get the current time in the proper format
(Hour:Minute:Second AM/PM). This program shows the current time by using the various
methods of the Calendar class. These are explained as follows:
Code Description:
Calendar:
Date is the special instance of the time. This class helps you to convert a date format object into
the integer fields like YEAR, MONTH, HOUR, MINUTE, SECOND etc.
Calendar.get(Calendar.MINUTE):
Above method gives you the minute value from the current time and MINUTE is the predefined
field of the Calendar class which returns the integer value i.e. the minute value of the current time
Calendar.AM_PM:
Above field of the Calendar class returns the integer value 1, if the current time is in the PM
otherwise it returns 0 value which denotes the time is in the AM period.
n this section, the given program shows you how to get about all the information for the
time zones.
Time Zone: Time Zone is the specified part of the earth that holds the special standard of the
time i.e. termed as local time. Usually time zone is based on the GMT (Greenwich Mean Time).
Each and every part of the earth has categorized for holding it's own time zones that had been
fixed according to the rotation of the earth on it's own base.
Most application software uses the underlying operating system for getting all information about
the time zones for setting up the time according to the appropriate time zones. Java maintains it's
own time zones database as well as the operating system database. There need to update the
java time zone database from the operating system database when it is changed.
DST (Daylight Saving Time): Daylight Saving Time is also called the summer time somewhere
in many countries. In this period the sun rises in the morning and sets in the evening one hour
later. So, the evening became one hour longer. Benefit of the period is saving energy because in
this period artificial light is less needed during the evening. To make DST work, when DST
begins, clocks have to be adjusted one hour ahead and adjusted one hour back to standard time
every autumn. Many countries maintain the DST and many do not.
Up to now, most of the United States have observed Daylight Saving Time from the first Sunday
of April at 2:00 am to the last Sunday of October at 2:00 am. In 2007, most of the U.S. will begin
Daylight Saving Time from the second Sunday of March at at 2:00 a.m. and revert to standard
time on the first Sunday in November. In the U.S., each time zone switches at a different time.
Program Result:
In this section, the given program shows you how to get about all the information for the time
zones. This program shows all time zones available in the world with the Time Zone ID, Time
Zone Nameand it's fixed GMT (India Standard Time : 5:30).
Code Description:
TimeZone:
This class is used to getting and setting the time zone. You can get the default time zone using
thegetDefault() method i.e. the time zone on where your program is running.
TimeZone.getAvailableIDs():
Above method returns a array of all time zone ids like the time zone id for the time zone of India
isAsia/Calcutta.
TimeZone.getTimeZone(id):
Above method returns the full name of the Time Zone. The time zone id, which full name has to
be found, is passed through the method as a parameter.
TimeZone.LONG:
This field of the TimeZone class returns a static integer value which is used in
thegetDisplayName() method of the class as a parameter for specifying the style of the
name of the time zone. It determines the long (full) name of the specific time zone.
Another field is theTimeZone.SHORT which determines the short name of the time zone.
TimeZone.inDaylightTime(new Date()):
This method returns the abstract boolean type value after checking whether the given
date is in the Daylight Saving Time period or not. It returns true, if the date is in the DST
(Daylight Saving Time) otherwise false.
TimeZone.getDisplayName(TimeZone.inDaylightTime(new Date()),
TimeZone.LONG):
This method gives you the full name of the specified time zone. This method returns
String type value which is the name of the time zone for the specified Locale.
TimeZone.getRawOffset():
Above method returns the amount of time to add UTC () to get standard time for the specified
time zone. This method returns the static integer value which is the amount of time in millisecond.
Math.abs(real_number):
This method returns the absolute number for the given number. The number, which has to
become absolute, is passed through the method as a parameter.
This section shows the way of getting the current date of the system.
This section shows the way of getting the current date of the system.
In this section, through the given program you can get the current date in the proper format
(Day/Month/Year). This program shows the current date by using the various methods and fields
of theCalendar class. These are explained as follows:
Code Description:
Calendar.MONTH:
This field of the Calendar class returns the static integer which is the value of the month of the
year.
Calendar.DAY:
This field returns a static integer value which is the value of the day of the month in the year.
Calendar.YEAR:
This field returns a static integer value which is the current year.
This section will show the way of counting the number of days of a month in the specified year.
Following program takes the year as input through the keyboard and shows the number of days
in the February month of the mentioned year if the year is valid other it will show the error
message and terminate the program.
Code Description:
JAR file is the compressed file format. You can store many files in a JAR file. JAR stands for the
Java Archive. This file format is used to distribute a set of java classes. This file helps you to
reduce the file size and collect many file in one by compressing files. Downloading the files are
become completed in very short duration of time because of reducing the file size. You can make
the jar file executable by collecting many class file of your java application in it. The jar file can
execute from the javaw (Java Web Start).
The JAR file format is based on the popular ZIP file format. Usually these file format is not only
used for archiving and distribution the files, these are also used for implementing various libraries,
components and plug-ins in java applications. Compiler and JVMs (Java Virtual Machine) can
understand and implement these formats for java application.
For mentioning the product information like vendor name, product version, date of creation of the
product and many other things related to the product are mentioned in the manifest file. Such
type of files are special which are mentioned in the jar file for making it executable for the
application. This file format is to be used for collecting auxiliary files associated with the
components.
To perform basic operations for the jar file there has to be used the Java Archive Tool (jar
tool). It is provided by the jdk (Java Development Kit). Following are some jar command
which are invoked by the jar tool:
Functions Command
creation a jar file jar cf jar-file-name file-name(s)_or_directory-
viewing contents of a jar file name
viewing contents with detail of a jar jar tf jar-file-name
file jar tvf jar-file-name
extract all the files of a jar file jar xf jar-file-name
extract specific files from the jar file jar xf jar-file-name file-name(s)_from_jar-file
update jar files jar uf jar-file-name file-name(s)_from_jar-file
running a executable packaged jar file java -jar jar-file-name
Related Tags for What is a JAR file in Java:
java, c, function, jar, fun, io, definition, this, functional, ie, unc, ini, e, it, des, section, li, init,
in, j, es, func, describe, s, is, functionality, definitions, and, ar, va, scr, s, s, ri, th, av, bes, f
in, nd, on, o
This section provides you to the creating a jar file through the java source code by using
the jar tool command which is provided by the JDK (Java Development Kit).
This section provides you to the creating a jar file through the java source code by using the jar
tool command which is provided by the JDK (Java Development Kit). Here, you can learn how to
use the jar command, which is used in the dos prompt, in the java source code to perform same
operations.
Following program is given for best illustration about using the dos used command in the java
source code to perform the appropriate tasks. This program has been used the jar command "jar
cf jar-file-name directory-name" in the source code to create a jar file for the given directory. You
can also specify the file name(s) to collect it into the jar file format.
Code Description:
Runtime:
Runtime is the class of java.lang.*; package of Java. This class helps application to interface with
the environment in which the java application is running.
Runtime.getRuntime():
This method gets the current runtime environment.
Runtime.exec(String[] command):
This method helps you to run the command of that environment in which your java application is
running.