Get Last Key from NavigableMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

180 Views

To display the last key from NavigableMap in Java, use the lastKey() 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 key now −n.lastKey()The following is an example to get the last key from NavigableMap.Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); ... Read More

Extend Interfaces in Java

Arushi
Updated on 30-Jul-2019 22:30:24

34K+ Views

An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. An interface extends another interface like a class implements an interface in interface inheritance.A program that demonstrates extending interfaces in Java is given as follows:Example Live Demointerface A {    void funcA(); } interface B extends A {    void funcB(); } class C implements B {    public void funcA() {       System.out.println("This is funcA");    }    public void funcB() {       System.out.println("This is funcB");    } } public class Demo {   ... Read More

Set File Attributes in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

681 Views

One of the file attribute that can be set is to make the file read-only. This can be done by using the method java.io.File.setReadOnly(). This method requires no parameters and it returns true if the file is set to read-only and false otherwise.The java.io.File.canRead() and java.io.File.canWrite() methods are used to check whether the file can be read or written to respectively.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");       ... Read More

Displaying Content of a HashMap in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

426 Views

Let us first create a HashMap and add elements −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));To display the content, just print the HashMap object −System.out.println("Map = "+hm);The following is an example to display content of a HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); System.out.println("Map = "+hm); } }OutputMap = {Belt=600, Wallet=700}

Remove Single Element from a HashSet in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

1K+ Views

To remove a single element from a HashSet, use the remove() method.First, create a HashSet −HashSet hs = new HashSet();Now, add elements to the HashSet −hs.add("R"); hs.add("S"); hs.add("T"); hs.add("V"); hs.add("W"); hs.add("Y"); hs.add("Z");Let us now remove an element −hs.remove("R");The following is an example to remove a single element from a HashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { HashSet hs = new HashSet(); // add elements to the hash set hs.add("R"); ... Read More

Recursive Fibonacci Method in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

12K+ Views

The fibonacci series is a series in which each number is the sum of the previous two numbers. The number at a particular position in the fibonacci series can be obtained using a recursive method.A program that demonstrates this is given as follows:Example Live Demopublic class Demo {    public static long fib(long n) {       if ((n == 0) || (n == 1))          return n;       else          return fib(n - 1) + fib(n - 2);    }    public static void main(String[] args) {       System.out.println("The ... Read More

Change File Attribute to Writable in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

581 Views

The file attribute can be changed to writable using the method java.io.File.setWritable(). This method has a single parameter i.e. a boolean value that if true allows the file to be writable and if false disallows the file to be writable. Also, this method returns true if the operation is successful and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          file.setReadOnly();   ... Read More

Remove Value from HashMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

782 Views

Use the remove() method to remove value from HashMap.First, create a HashMap and add elements −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));Now, remove a value, let’s say with key “Wallet” −Object ob = hm.remove("Wallet");The following is an example to remove a value from HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); ... Read More

Check for an Element in a HashSet in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

2K+ Views

To check whether an element is in a HashSet or not in Java, use the contains() method.Create a HashSet −HashSet hs = new HashSet();Add elements to it −hs.add("R"); hs.add("S"); hs.add("T"); hs.add("V"); hs.add("W"); hs.add("Y"); hs.add("Z");To check for an element, for example, S here, use the contains() −hs.contains("S")The following is an example to check for an element in a HashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { HashSet hs = new HashSet(); // add elements to the HashSet ... Read More

Add Element to Specified Index of ArrayList in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

543 Views

An element can be added to the specified index of an ArrayList by using the java.util.ArrayList.add() method. This method has two parameters i.e. the specific index at which to insert the element in the ArrayList and the element itself. If there is an element already present at the index specified by ArrayList.add() then that element and all subsequent elements shift to the right by one.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo { public static void main(String args[]) throws Exception { List ... Read More

Advertisements