Samual Sam has Published 2491 Articles

Check whether two HashSet are equal in Java

Samual Sam

Samual Sam

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

309 Views

At first, create the first HashSet and add elements to it −// create hash set 1 HashSet hs1 = new HashSet(); hs1.add("G"); hs1.add("H"); hs1.add("I"); hs1.add("J"); hs1.add("K");Create the second HashSet and add elements to it −// create hash set 2 HashSet hs2 = new HashSet(); hs2.add("G"); hs2.add("H"); hs2.add("I"); hs2.add("J"); hs2.add("K");To check ... Read More

Clone an ArrayList in Java

Samual Sam

Samual Sam

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

360 Views

An ArrayList can be cloned using the java.util.ArrayList.clone() method. This method does not take any parameters but returns a shallow copy the specified ArrayList instance. This means that the new ArrayList created using the ArrayList.clone() method refers to the same elements as the original ArrayList but it does not duplicate ... Read More

Copy all the elements from one set to another in Java

Samual Sam

Samual Sam

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

888 Views

Use the clone() method to copy all elements from one set to another.First HashSet −HashSet set = new HashSet (); set.add("One"); set.add("Two");Create another set and clone first set into the second −HashSet newSet = new HashSet ();Copy (clone) all elements to the second set −newSet = (HashSet)set.clone();The following ... Read More

Check if a given key exists in Java HashMap

Samual Sam

Samual Sam

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

8K+ Views

Use the containsKey() method and check if a given key exists in the HashMap or not.Let us first create HashMap and add some elements −// Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", ... Read More

Create a TreepMap in Java and add elements

Samual Sam

Samual Sam

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

36 Views

Let us create TreeMap in Java. It stores unique elements in ascending order −TreeMap m = new TreeMap();Now let us add some elements −m.put(1, "PHP"); m.put(2, "jQuery"); m.put(3, "JavaScript"); m.put(4, "Ruby"); m.put(5, "Java"); m.put(6, "AngularJS"); m.put(7, "ExpressJS");The following is an example to create a TreeMap −Example Live Demoimport java.util.*; public class ... Read More

Get the name of the file and path in Java

Samual Sam

Samual Sam

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

6K+ Views

The name of the file and the name of the path can be obtained using the methods java.io.File.getName() and java.io.File.getPath() respectively. The getName() returns the name of the file or the directory. The getPath() returns the abstract pathname in the form of a pathname string.A program that demonstrates this is ... Read More

Java Program to get the name of the parent directory of the file or directory

Samual Sam

Samual Sam

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

554 Views

The name of the parent directory of the file or directory can be obtained using the method java.io.File.getParent(). This method returns the parent directory path name string or null if there is no parent named.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo { ... Read More

Java Program to check if a particular value exists in TreeMap

Samual Sam

Samual Sam

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

244 Views

To check if a particular value exists in TreeMap, use the containsValue() method.Create a TreeMap first and add some elements −TreeMap m = new TreeMap(); m.put(1, "PHP"); m.put(2, "jQuery"); m.put(3, "JavaScript"); m.put(4, "Ruby"); m.put(5, "Java"); m.put(6, "AngularJS"); m.put(7, "ExpressJS");Now, let’s say we need to check that value “Java” exists or ... Read More

A One-Bit Sliding Window Protocol

Samual Sam

Samual Sam

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

14K+ Views

Sliding window protocols are data link layer protocols for reliable and sequential delivery of data frames. The sliding window is also used in Transmission Control Protocol. In these protocols, the sender has a buffer called the sending window and the receiver has buffer called the receiving window.In one – bit ... Read More

Java Program to check whether a file exists or not

Samual Sam

Samual Sam

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

175 Views

The method java.io.File.exists() is used to check whether a file exists or not. This method returns true if the file specified by the abstract path name exists and false if it does not exist.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {   ... Read More

Advertisements