Clone HashMap in Java

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

227 Views

Use the clone() method to clone HashMap.The following is our HashMap with elements −HashMap hm1 = new HashMap(); hm1.put("Shirts", new Integer(700)); hm1.put("Trousers", new Integer(600)); hm1.put("Jeans", new Integer(1200)); hm1.put("Android TV", new Integer(450)); hm1.put("Air Purifiers", new Integer(300)); hm1.put("Food Processors", new Integer(950));Create another HashMap and clone the first HashMap into it −HashMap hm2 = (HashMap)hm1.clone();The following is an example to clone HashMap in Java −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm1 = new HashMap(); ... Read More

Set vs HashSet vs TreeSet in Java

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

20K+ Views

A Set is a generic set of values with no duplicate elements. A TreeSet is a set where the elements are sorted.A HashSet is a set where the elements are not sorted or ordered. It is faster than a TreeSet. The HashSet is an implementation of a Set.Set is a parent interface of all set classes like TreeSet, HashSet, etc.Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       int a[] = {77, 23, 4, 66, 99, 112, 45, 56, 39, 89};       Set s = new HashSet();       ... Read More

Get First Entry from NavigableMap in Java

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

166 Views

To display first entry from NavigableMap in Java, use the firstEntry() 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 first entry now −n.firstEntry()The following is an example to get first entry 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

Overloading Varargs Methods in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

889 Views

A method with variable length arguments(Varargs) can have zero or multiple arguments. Also, Varargs methods can be overloaded if required.A program that demonstrates this is given as follows:Example Live Demopublic class Demo {    public static void Varargs(int... args) {       System.out.println("Number of int arguments are: " + args.length);       System.out.println("The int argument values are: ");       for (int i : args)       System.out.println(i);    }    public static void Varargs(char... args) {       System.out.println("Number of char arguments are: " + args.length);       System.out.println("The char argument values are: "); ... Read More

Standard Body for Conventions of Research Writing

Ridhi Arora
Updated on 30-Jul-2019 22:30:24

238 Views

MLA (Modern Language Association) is a body that assigns rules and regulations for research paper writing. MLA is popular worldwide and assigns various rules on Selecting a topic, compiling a working bibliography, outlining and writing drafts (to name a few).Selecting A TopicIn order to select a topic, different instructors and different courses offer widely varying degrees of freedom to students selecting topics for research papers. The instructor of a course in a specific discipline may supply a list of topics from which to choose or any, more generally, require that the paper relates to an important aspect of the course. MLA ... Read More

Get Total Space of a Partition in Java

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

245 Views

The method java.io.File.getTotalSpace() is used to obtain the total space in the form of bytes for the partition specified by the required abstract path name. This method requires no parameters and it returns the bytes for the partition i.e. its total space.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       File[] roots = File.listRoots();       try {          for(File r : roots) {             System.out.println(r);             System.out.println("Total space ... Read More

Check Two HashMap for Equality in Java

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

2K+ Views

To check whether two HashMap are equal or not, use the equals() method.Let us first create the first HashMap −// Create hash map 1 HashMap hm1 = new HashMap(); hm1.put("Shirts", new Integer(700)); hm1.put("Trousers", new Integer(600)); hm1.put("Jeans", new Integer(1200)); hm1.put("Android TV", new Integer(450)); hm1.put("Air Purifiers", new Integer(300)); hm1.put("Food Processors", new Integer(950));Let us now create the second HashMap −HashMap hm2 = new HashMap(); hm2.put("Shirts", new Integer(700)); hm2.put("Trousers", new Integer(600)); hm2.put("Jeans", new Integer(1200)); hm2.put("Android TV", new Integer(450)); hm2.put("Air Purifiers", new Integer(300)); hm2.put("Food Processors", new Integer(950));To check whether both the HashMap are equal or not, use the equals() method like this −hm1.equals(hm2)The following is ... Read More

Get Last Key from NavigableMap in Java

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

189 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

35K+ 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

694 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

Advertisements