Remove Last Entry of the TreeMap in Java

Ankith Reddy
Updated on 29-Jun-2020 13:38:04

815 Views

To remove the last entry of the TreeMap, use the pollLastEntry() method.Let us first create a TreeMap and add elementsTreeMap m = new TreeMap(); m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");Remove the last entry nowm.pollLastEntry()The following is an example to remove the last entry of the TreeMapExample Live Demoimport java.util.*; public class Demo {    public static void main(String args[]){       TreeMap m = new TreeMap();       m.put(1, "India");       m.put(2, "US");       m.put(3, "Australia");       m.put(4, "Netherlands");       m.put(5, "Canada");       System.out.println("TreeMap Elements ... Read More

Define Abstract Class Without Abstract Method in Java

Maruthi Krishna
Updated on 29-Jun-2020 13:37:31

6K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Abstract classA class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.And yes, you can declare abstract class without defining an abstract method in it. Once you declare a class abstract it indicates that the class is incomplete ... Read More

Get OffsetDateTime and LocalDate in Android Using Offset DateTime API Class

Nishtha Thakur
Updated on 29-Jun-2020 13:37:26

479 Views

This example demonstrate about How to get offsetdatetime local date in android using offset date time API class.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have taken textview to show current date.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import java.time.OffsetDateTime; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) { ... Read More

Get Month Information in Android Using YearMonth API Class

Anvi Jain
Updated on 29-Jun-2020 13:36:51

185 Views

This example demonstrate about How to get month information in android using yearmonth API class.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have taken textview to show current year with month.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import java.time.YearMonth; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {   ... Read More

Create Object for Abstract Class in Java

Maruthi Krishna
Updated on 29-Jun-2020 13:36:39

2K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.Instantiating an abstract classOnce a class is abstract it indicates that it may contain incomplete methods hence you cannot create an object of the abstract class.If you try ... Read More

Get Local Time in Android Using LocalTime API Class

Nishtha Thakur
Updated on 29-Jun-2020 13:36:10

450 Views

This example demonstrate about How to get local time in android using localtime API class.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have taken textview to show time.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import java.time.LocalTime; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState); ... Read More

Declare Abstract Method as Final or Static in Java

Maruthi Krishna
Updated on 29-Jun-2020 13:36:04

12K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Declaring abstract method staticIf you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.If you still, try to declare an abstract method static a compile time error is generated saying ... Read More

Loop Through ArrayList in Java

Ankith Reddy
Updated on 29-Jun-2020 13:35:59

811 Views

The elements of the ArrayList can be accessed one by one by using a for loop. A program that demonstrates this is given as followsExample Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Sun");       aList.add("Moon");       aList.add("Star");       aList.add("Planet");       aList.add("Comet");       System.out.println("The ArrayList elements are:");       for (String s : aList) {          System.out.println(s);       }    } }OutputThe output of the above ... Read More

Get Local Time and Date in Android Using LocalDateTime API Class

Anvi Jain
Updated on 29-Jun-2020 13:35:36

748 Views

This example demonstrate about How to get local time and date in android using LocalDateTime API class.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have taken textview to show time and date.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle ... Read More

Get Local Date in Android Using Local Date API Class

Nishtha Thakur
Updated on 29-Jun-2020 13:34:55

448 Views

This example demonstrate about How to get local date in android using local date API class.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have taken textview to show date.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; import android.icu.util.LocaleData; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import java.awt.font.TextAttribute; import java.time.LocalDate; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) ... Read More

Advertisements