Search a key in hashtable

We can use containsKey() method to search a key in hashtable. Syntax: hashtable.containsKey(specifiedKey); Example: package com.w3schools;   import java.util.Hashtable;   public class Test { public static void main(String args[]){ //Create Hashtable object. Hashtable<String, String> hashtable = new Hashtable<String, String>(); //Add objects to the Hashtable. hashtable.put("1", "Jai"); hashtable.put("2", "Mahesh"); hashtable.put("3","Vivek"); System.out.println(hashtable); if(hashtable.containsKey("2")){ System.out.println("The Hashtable contains key … Read more

Copy map content to another hashtable

We can use putAll() method to copy map content to another hashtable in java. Syntax: hashtable.putAll(hashMap); Example: package com.w3schools;   import java.util.HashMap; import java.util.Hashtable;   public class Test { public static void main(String args[]){ //Create Hashtable object. Hashtable<String, String> hashtable = new Hashtable<String, String>(); //Add objects to the Hashtable. hashtable.put("1", "Jai"); hashtable.put("2", "Mahesh"); hashtable.put("3","Vivek"); System.out.println(hashtable); … Read more

Iterate through hashtable in java

To iterate through hashtable in java, first we have to get all keys as set object and then fetch each element using key. Example: package com.w3schools;   import java.util.Hashtable; import java.util.Set;   public class Test { public static void main(String args[]){ //Create Hashtable object. Hashtable<String, String> hashtable = new Hashtable<String, String>(); //Add objects to the … Read more

Remove element from linkedlist in java

Following methods are used to remove element from linkedlist in java: remove(): It retrieves and removes the first element of the list. remove(index): It removes the element at the specified position in the list. remove(object): It removes the first occurrence of the specified element from the list, if it is present. removeFirst(): It removes and … Read more

linked list push and pop in java

LinkedList: The LinkedList class extends AbstractSequentialList and implements the List and Deque interface. It uses linked list data structure to store elements. It can contain duplicate elements. It is not synchronized. Note: It not provides the random access facility. Following methods are used for linked list push and pop operations: push(): Pushes an element onto … Read more

how to iterate through linked list in reverse order?

LinkedList: The LinkedList class extends AbstractSequentialList and implements the List and Deque interface. It uses linked list data structure to store elements. It can contain duplicate elements. It is not synchronized. Note: It not provides the random access facility. We can use descendingIterator() method to iterate through linked list in reverse order. It returns an … Read more

Get last element in linked list in java

Following methods are used to get last element in linked list in java: getLast(): Returns the last element in this list. peekLast(): Retrieves, but does not remove, the last element of this list, or returns null if this list is empty. Example: package com.w3schools;   import java.util.LinkedList;   public class Test { public static void … Read more

Get first element in linked list in java

Following methods are used to get first element in linked list in java: element(): Retrieves, but does not remove, the head (first element) of this list. getFirst(): Returns the first element in this list. peek(): Retrieves, but does not remove, the head (first element) of this list. peekFirst(): Retrieves, but does not remove, the first … Read more

Add an element at first and last position of linked list

Following methods are used to add an element at first and last position of linked list: addFirst(): Inserts the specified element at the beginning of the list. offerFirst(): Inserts the specified element at the front of the list. addLast(): Appends the specified element to the end of the list. offerLast(): Inserts the specified element at … Read more

Swap two elements in a linked list java

We can use Collections.swap() method to swap two elements in a linked list in java. We have to pass the indexes which we need to swap. Syntax: Collections.swap(linkedList, element1, element2); Example: package com.w3schools;   import java.util.Collections; import java.util.LinkedList;   public class Test { public static void main(String args[]){ LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add("Jai"); linkedList.add("Mahesh"); … Read more