Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java Articles
Page 36 of 450
Get last entry from NavigableMap in Java
To display the last entry from NavigableMap in Java, use the lastEntry() method.Let us first create NavigableMap.NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); n.put("C", 868); n.put("D", 988); n.put("E", 686); n.put("F", 888); n.put("G", 999); n.put("H", 444); n.put("I", 555); n.put("J", 666);Get the last entry now.n.lastEntry()The following is an example to get the last entry from NavigableMap.Exampleimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); ...
Read MoreHow to use headSet() method of Java NavigableSet Class
The headset() method returns elements up to a limit defined as a parameter.First, create a NavigableSet and add elements −NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100);Now, use the headset() method −set.headSet(55));The following is an example −Exampleimport java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100); System.out.println("Returned Value = " + set.headSet(55)); ...
Read MoreCreate a new ArrayList from another collection in Java
An ArrayList can be created from another collection using the java.util.Arrays.asList() method. A program that demonstrates this is given as follows −Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Demo { public static void main(String args[]) throws Exception { String str[] = { "John", "Macy", "Peter", "Susan", "Lucy" }; List aList = new ArrayList(Arrays.asList(str)); System.out.println("The ArrayList elements are: " + aList); } }OutputThe ArrayList elements are: [John, Macy, Peter, Susan, Lucy]Now let us understand the above program.The string array str[] is defined. Then an ArrayList is created using the ...
Read MorePrint a Vector in a comma-delimited list, in index order and surrounded by square brackets ([]) in Java
A Vector can be printed in a comma-delimited list, in index order and surrounded by square brackets ([]) by simply using System.out.println() along with the Vector object.A program that demonstrates this is given as follows −Exampleimport java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(5); vec.add(4); vec.add(1); vec.add(3); vec.add(9); vec.add(6); System.out.println(vec); } }The output of the above program is as follows −[4, 1, 3, 9, 6]Now let us understand the ...
Read MoreReplace all the elements of a Vector with Collections.fill() in Java
All the elements of a vector can be replaced by a specific element using java.util.Collections.fill() method. This method requires two parameters i.e. the Vector and the element that replaces all the elements in the Vector. No value is returned by the Collections.fill() method.A program that demonstrates this is given as follows:Exampleimport java.util.Collections; import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(5); vec.add(7); vec.add(4); vec.add(1); ...
Read MoreEnumerate through the Vector elements in Java
The Vector elements can be traversed using the Enumeration interface. The method hasMoreElements( ) returns true if there are more elements to be enumerated and false if there are no more elements to be enumerated. The method nextElement( ) returns the next object in the enumeration.A program that demonstrates this is given as follows −Exampleimport java.util.Enumeration; import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(); vec.add(7); vec.add(3); vec.add(5); vec.add(9); vec.add(2); ...
Read MoreLoop through the Vector elements using an Iterator in Java
An Iterator can be used to loop through the Vector elements. The method hasNext( ) returns true if there are more elements in the Vector and false otherwise. The method next( ) returns the next element in the Vector and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows −Exampleimport java.util.Iterator; import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(); vec.add(4); vec.add(1); vec.add(3); vec.add(9); ...
Read MoreFind the minimum element of a Vector in Java
The minimum element of a Vector can be obtained using the java.util.Collections.min() method. This method contains a single parameter i.e. the Vector whose minimum element is determined and it returns the minimum element from the Vector.A program that demonstrates this is given as follows −Exampleimport java.util.Collections; import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(); vec.add(7); vec.add(3); vec.add(9); vec.add(5); vec.add(8); System.out.println("The Vector elements are: " + vec); ...
Read MoreConvert a Vector to an array in Java
A Vector can be converted into an Array using the java.util.Vector.toArray() method. This method requires no parameters and it returns an Array that contains all the elements of the Vector in the correct order.A program that demonstrates this is given as follows −Exampleimport java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(); vec.add(7); vec.add(3); vec.add(5); vec.add(2); vec.add(8); Object[] arr = vec.toArray(); System.out.println("The Array elements are: "); ...
Read MoreClass declaration with one method in Java
A class declaration can contain a single method. A program that demonstrates this is given as follows:Exampleclass Message { public void messagePrint() { System.out.println("This is a class with a single method"); } } public class Demo { public static void main(String args[]) { Message m = new Message(); m.messagePrint(); } }OutputThis is a class with a single methodNow let us understand the above program.The Message class is created with a single member function messagePrint(). A code snippet which demonstrates this is as follows −class Message { ...
Read More