Java Articles - Page 243 of 745

How to sort an ArrayList in Descending Order in Java

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

662 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

97 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

96 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

127 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

How to sort an ArrayList in ascending order in Java

AmitDiwan
Updated on 29-Sep-2024 02:48:17

1K+ Views

In this article, we will learn how to sort an ArrayList in ascending order using Java. Sorting allows us to arrange elements in a specific order. We will use the Collections.sort() method, which simplifies sorting, whether you're dealing with integers or strings. The program will demonstrate sorting an ArrayList of numbers and names in ascending order. Problem Statement Write a Java program that sorts an ArrayList of elements in ascending order. We'll look at two examples—one with integer values and another with string values. Input 1 Points[50, 29, 35, 11, 78, 64, 89, 67] Output 1 Points (ascending)[11, 29, 35, ... Read More

How can we check if a JSON object is empty or not in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 15:38:38

11K+ Views

In this article, let's learn how to check if a JSON object has any values or not in Java. If you don't know about JSON, refer JSON. We will use the org.json library and its methods like isEmpty(), length(), and keys() to check if a JSON object is empty or not. We can check if a JSON object is empty or not using the following methods: Using isEmpty() method Using length() method Using keys() method Let's see how to use each of them one by one. ... Read More

How to get the values of the different types from a JSON object in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 16:05:02

31K+ Views

We often need to extract values from a JSON object. In this article, we will learn how to get the values of the different types from a JSON object in Java. What is JSONObject? Java JSONObject is a class in the org.json package that represents a JSON object. Refer JSONObject for more information. Let's see the steps to get the values of different types from a JSON object in Java. Getting different values from a JSON object Before we start, let's add the required library to our program. We can add a .jar file, or we can use a Maven ... Read More

When can we use a JSONStringer in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 14:33:12

539 Views

A JSONStringer provides a convenient way of producing JSON text, and it can strictly follow to JSON syntax rules. Each instance of JSONStringer can produce one JSON text. A JSONStringer instance provides a value method for appending values to the text and a key-method for adding keys before values in objects. We have array () and endArray() methods that create and bind array values, and object() and endObject () methods that create and bind object values. It is part of the org.json library. It is generally used in Android and lightweight Java programs. In this article, we will learn when to use a JSONStringer in Java. When to use ... Read More

Advertisements