Samual Sam has Published 2310 Articles

Clone an ArrayList in Java

Samual Sam

Samual Sam

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

439 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

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

62 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

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

A One-Bit Sliding Window Protocol

Samual Sam

Samual Sam

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

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

Get lowest key stored in Java TreeMap

Samual Sam

Samual Sam

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

2K+ Views

Use the firstKey() method to get the lowest key stored in TreeMap.Let us first set the TreeMap and add some elements to it −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 get the lowest key −m.firstKey()The following is an ... Read More

Check whether the given file is an existing file in Java

Samual Sam

Samual Sam

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

173 Views

The method java.io.File.isFile() is used to check whether the given file specified by the abstract path name is an existing file in Java. This method returns true if the file specified by the abstract path name is a file and false otherwise.A program that demonstrates this is given as follows ... Read More

Delete first and last element from a LinkedList in Java

Samual Sam

Samual Sam

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

407 Views

The first element can be deleted from a LinkedList by using the method java.util.LinkedList.removeFirst(). This method does not have any parameters and it returns the first element of the LinkedList.The last element can be deleted from a LinkedList by using the method java.util.LinkedList.removeLast(). This method does not have any parameters ... Read More

Check whether the file can be read in Java

Samual Sam

Samual Sam

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

631 Views

The method java.io.File.canRead() is used to check whether the file can be read in Java. This method returns true if the file specified by the abstract path name can be read by an application and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class ... Read More

Advertisements