Found 9150 Articles for Object Oriented Programming

Initialize an ArrayList in Java

Alshifa Hasnain
Updated on 18-Mar-2025 17:55:13

1K+ Views

In this article, we will learn to initialize an ArrayList in Java. 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. Different Approaches The following are the two different approaches to initialize an ArrayList in Java − Using add() Method Using asList() method Using add() Method One of the most frequent methods of ... Read More

Implement Triplet Class with Pair Class in Java using JavaTuples

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

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

Convert JSON object to Java object using Gson library in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 14:24:14

9K+ Views

Let's see learn how to change a JSON object into a Java object using the Gson library. Gson is a tool made by Google for converting Java objects into JSON and back. JSON stands for JavaScript Object Notation. In the JSON format, data is stored in key-value pairs. Mainly, it is used for sharing data between a server and a web app. Gson is a Java tool that can turn Java objects into JSON and JSON back into Java objects. It is used by many developers because it is simple and easy. Why Convert JSON Object to Java Object? Here ... Read More

Implement Pair Class with Unit Class in Java using JavaTuples

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

107 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

653 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

93 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

92 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

120 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

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

How to 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