Initialize HashMap in Java

AmitDiwan
Updated on 20-Sep-2019 07:57:58

750 Views

The HashMap class uses a hashtable to implement the Map interface. This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets.Following is the list of constructors supported by the HashMap class.Sr.NoConstructor & Description1HashMap( )This constructor constructs a default HashMap.2HashMap(Map m)This constructor initializes the hash map by using the elements of the given Map object m.3HashMap(int capacity)This constructor initializes the capacity of the hash map to the given integer value, capacity.4HashMap(int capacity, float fillRatio)This constructor initializes both the capacity and fill ratio of the hash map by using its ... Read More

Implement Triplet Class with Pair Class in Java using Javatuples

AmitDiwan
Updated on 20-Sep-2019 07:52:17

116 Views

At first, create a Pair class −Pairpair = new Pair(Integer.valueOf(25), "green");Now, implement Triplet class with Pair class −Triplet triplet = pair.add("magenta");Following is an example to implement Triplet class with Pair class in Java −Exampleimport org.javatuples.Pair; import org.javatuples.Triplet; public class MyDemo {    public static void main(String[] args) {       Pairpair = new Pair(Integer.valueOf(25), "green");       System.out.println("Pair class = " + pair);       Triplettriplet = pair.add("magenta");       System.out.println("Triplet class elements (Implemented from Pair class) = " + triplet);    } }OutputPair class = [25, green] Triplet class elements (Implemented from Pair class) = [25, green, magenta]

Product of the Alternate Nodes of Linked List

Sunidhi Bansal
Updated on 20-Sep-2019 07:50:04

223 Views

Given with n nodes the task is to print the product of alternate node in a linked list. The program must only print the product of alternate nodes without actually changing the locations of the nodes.ExampleInput -: 10 20 30 40 50 60 Output -: 15000In the above example, starting from first node which is 10 alternate nodes are 10, 30, 50 and their product is 10*30*50 = 15000.In the above diagram, blue coloured nodes are the alternate nodes, if we start from first node and red coloured nodes are non considerable nodes.Approach used below is as followsTake a temporary ... Read More

Implement Pair Class with Unit Class in Java using Javatuples

AmitDiwan
Updated on 20-Sep-2019 07:44:41

127 Views

Following is an example to implement Pair class from Unit class in Java −Exampleimport org.javatuples.Unit; import org.javatuples.Pair; public class MyDemo {    public static void main(String[] args) {       Unit unit = Unit.with("Tutorial");       System.out.println("Unit class element: " + unit);       Pair pair = unit.addAt0("Learning");       System.out.println("Pair (Implemented from Unit Tuple): " + pair);    } }OutputUnit class element: [Tutorial] Pair (Implemented from Unit): [Learning, Tutorial]Let us see another example wherein we will be implementing Pair class from Unit class −Exampleimport org.javatuples.Unit; import org.javatuples.Pair; public class MyDemo {    public static void ... Read More

Sort ArrayList in Descending Order in Java

AmitDiwan
Updated on 20-Sep-2019 07:42:31

677 Views

To sort an ArrayList, you need to use the Collections.sort() method. This sorts in ascending order, but if you want to sort the ArrayList in descending order, use the Collections.reverseOrder() method as well. This gets included as a parameter −Collections.sort(myList, Collections.reverseOrder());Following is the code to sort an ArrayList in descending order in Java −Example Live Demoimport java.util.ArrayList; import java.util.Collections; public class Demo {    public static void main(String args[]) {       ArrayList myList = new ArrayList();       myList.add(30);       myList.add(99);       myList.add(12);       myList.add(23);       myList.add(8);       ... Read More

Implement Octet Class from Septet Class in Java using Javatuples

AmitDiwan
Updated on 20-Sep-2019 07:40:07

107 Views

At first create Septet and add elements −Septet    septet = new Septet(    "Laptop", "Desktop", "Tablet", "Notebook", "Phone", "Reader", "LCD");Now, implement Octet from Septet −Octet octet = septet.add("LED");Following is an example to implement Octet class from Septet class in Java −Exampleimport org.javatuples.Septet; import org.javatuples.Octet; public class MyDemo {    public static void main(String[] args) {       Septet          septet = new Septet(          "Laptop", "Desktop", "Tablet", "Notebook", "Phone", "Reader", "LCD");       System.out.println("Septet elements = " + septet);       Octetoctet = septet.add("LED");       System.out.println("Octet (implemented from ... Read More

Implement Ennead Class from Octet Class in Java using Javatuples

AmitDiwan
Updated on 20-Sep-2019 07:37:27

107 Views

Following is an example to implement Ennead class from Octet class in Java −Exampleimport org.javatuples.Octet; import org.javatuples.Ennead; public class MyDemo {    public static void main(String[] args) {       Octet          octet = new Octet(          "Jack", "Tom", "Steve", "Tim", "Nathan", "Ryan", "Kevin", "Katie");       System.out.println("Octet elements = " + octet);       Enneadennead = octet.add("Scarlett");       System.out.println("Ennead (implemented from Octet): " + ennead);    } }OutputOctet elements = [Jack, Tom, Steve, Tim, Nathan, Ryan, Kevin, Katie] Ennead (implemented from Octet): [Jack, Tom, Steve, Tim, Nathan, Ryan, ... Read More

Implement Decade Class from Ennead Class in Java using Javatuples

AmitDiwan
Updated on 20-Sep-2019 07:34:59

148 Views

Following is an example to implement Decade Class from Ennead Class in Java using JavaTuples −Exampleimport org.javatuples.Decade; import org.javatuples.Ennead; public class MyDemo {    public static void main(String[] args) {       Ennead e =          Ennead.with("Katie", "Tom", "Ryan", "Tom", "Bradley", "David", "Steve", "Brad", "Jacob");       System.out.println("Ennead elements= " + e);       Decade decade = e.add("Amy");       System.out.println("Decade elements (implemented from Ennead) = " + decade);    } }OutputEnnead elements= [Katie, Tom, Ryan, Tom, Bradley, David, Steve, Brad, Jacob] Decade elements (implemented from Ennead) = [Katie, Tom, Ryan, Tom, Bradley, ... Read More

Sort TreeSet in Descending Order in Java

AmitDiwan
Updated on 20-Sep-2019 07:28:17

9K+ Views

To sort TreeSet in descending order, use the descendingSet() method in Java.The descendingSet() method is used to return a reverse order view of the elements contained in this set.At first, create a TreeSet −TreeSet treeSet = new TreeSet();Then add some elements −treeSet.add(45); treeSet.add(15); treeSet.add(99); treeSet.add(70);Sort them indecreasing order −TreeSet res = (TreeSet)treeSet.descendingSet();Following is the code to sort TreeSet in descending order −Example Live Demoimport java.util.TreeSet; public class Main {    public static void main(String[] args) {       TreeSet treeSet = new TreeSet();       treeSet.add(45);       treeSet.add(15);       treeSet.add(99);       treeSet.add(70);       ... Read More

Sort HashSet in Java

AmitDiwan
Updated on 20-Sep-2019 07:15:36

2K+ Views

To sort HashSet in Java, you can use another class, which is TreeSet.Following is the code to sort HashSet in Java −Example Live Demoimport java.util.*; public class Main {    public static void main(String args[]) {       Set hashSet = new HashSet();       hashSet.add("green");       hashSet.add("blue");       hashSet.add("red");       hashSet.add("cyan");       hashSet.add("orange");       hashSet.add("green");       System.out.println("HashSet elements"+ hashSet);       Set treeSet = new TreeSet(hashSet);       System.out.println("Sorted elements"+ treeSet);    } }OutputHashSet elements [red, orange, green, blue, cyan] Sorted elements [blue, cyan, ... Read More

Advertisements