Found 4338 Articles for Java 8

Initialize HashMap in Java

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

547 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

Initialize an ArrayList in Java

AmitDiwan
Updated on 20-Sep-2019 07:54:49

911 Views

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.Let us now see how we can initialize an ArrayList with add() method −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(50);       myList.add(29);       myList.add(35);       myList.add(11);     ... Read More

Implement Triplet Class with Pair Class in Java using JavaTuples

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

68 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]

Implement Pair Class with Unit Class in Java using JavaTuples

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

54 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

How to sort an ArrayList in Descending Order in Java

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

541 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

56 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

60 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

64 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

How to sort TreeSet in descending order in Java?

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

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

How to sort HashSet in Java

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

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