Found 2616 Articles for Java

How to Sort a String in Java alphabetically in Java?

Maruthi Krishna
Updated on 02-Sep-2023 10:27:00

76K+ Views

Using the toCharArray() methodThe toCharArray() method of this class converts the String to a character array and returns it. To sort a string value alphabetically −Get the required string.Convert the given string to a character array using the toCharArray() method.Sort the obtained array using the sort() method of the Arrays class.Convert the sorted array to String by passing it to the constructor of the String array.Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class SortingString {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string value: ");       ... Read More

In how many ways you can retrieve the elements of a collection in Java?

Maruthi Krishna
Updated on 14-Oct-2019 09:03:48

2K+ Views

You can retrieve the contents of a collection object in three ways −Using for each loopThe foreach loop or enhanced for loop, which enables you to traverse the complete collection object sequentially.Example Live Demoimport java.util.ArrayList; public class RetrievingData {    public static void main(String[] args) {       ArrayList list = new ArrayList();       //Instantiating an ArrayList object       list.add("JavaFX");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       list.add("OpenNLP");       list.add("JOGL");       list.add("Hadoop");       list.add("HBase");       list.add("Flume");       ... Read More

How many ways are there to convert an Array to ArrayList in Java?

Maruthi Krishna
Updated on 14-Oct-2019 08:59:09

183 Views

By adding each element of the arrayThe add() method of the ArrayList class accepts an element and adds it to the current array list. To convert an array to array list using this method −Get the string array.Create an empty ArrayList object.Add each element of the array to the ArrayList.Print the contents of the array list.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class ArrayToArrayList {    public static void main(String args[]) {       String stringArray[] = {"JavaFX", "Java", "WebGL", "OpenCV", "OpenNLP", "JOGL", "Hadoop", "HBase", "Flume", "Mahout", "Impala"};       ArrayList arrayList = new ArrayList();       for(int ... Read More

Remove all non-alphabetical characters of a String in Java?

Maruthi Krishna
Updated on 14-Oct-2019 08:54:42

2K+ Views

The split() method of the String class accepts a String value representing the delimiter and splits into array of tokens (words), treating the string between the occurrence of two delimiters as one token.For example if you pass single space “ ” as a delimiter to this method and try to split a String. This method considers the word between two spaces as one token and returns an array of words (between spaces) in the current String.If the String does not contain the specified delimiter this method returns an array containing the whole string as element.The regular expression “\W+” matches all ... Read More

How to convert LinkedList to Array in Java?

Maruthi Krishna
Updated on 14-Oct-2019 08:51:15

5K+ Views

The toArray() method of the LinkedList class converts the current Linked List object into an array of object type and returns it. This array contains all of the elements in this list in proper sequence (from first to last element). This acts as bridge between array-based and collection-based APIs.Therefore, to convert a LinkedList to an array −Instantiate the LinkedList class.Populate it using the add() method.Invoke the toArray() method on the above created linked list and retrieve the object array.Convert each and every element of the object array to string.Example Live Demoimport java.util.Arrays; import java.util.LinkedList; public class LinkedListToArray {    public static ... Read More

How to remove an element from ArrayList or, LinkedList in Java?

Maruthi Krishna
Updated on 17-Feb-2020 07:04:08

614 Views

The ArrayList and, LinkedList classes implements the List interface of the java.util package. This interface provided two variants of the remove() method to remove particular elements as shown below −E remove(int index)boolean remove(Object o) −Using one of these methods you can delete a desired element from the List or, linkedList in Java.E remove(int index) − This method accepts an integer representing a particular position in the List object and, removes the element at the given position. If the remove operation is successful, this method returns the element that has been removed.If the index value passed to this method is less ... Read More

How to make an ArrayList read only in Java?

Maruthi Krishna
Updated on 14-Oct-2019 08:37:04

440 Views

The unmodifiableList() method of the java.util.Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object. The user has only read-only access to the obtained list.ExampleFollowing Java program creates an ArrayList object, adds elements to it, converts it into a read-only List object. Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArrayListReadOnly {    public static void main(String[] args) {       //Instantiating an ArrayList object       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");   ... Read More

How to send data over remote method in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:33:54

520 Views

RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM.RMI is used to build distributed applications; it provides remote communication between Java programs. It is provided in the package java.rmi.To write an RMI Java application, you would have to follow the steps given below −Step1 − Define the Remote InterfaceA remote interface provides the description of all the methods of a particular remote object. The client communicates with this remote interface. Therefore, you need to create an interface that extends the predefined interface, ... Read More

What are the key steps in read/write from/to a URL connection in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:26:34

216 Views

The URL class of the java.net package represents a Uniform Resource Locator which is used to point a resource(file or, directory or a reference) in the world wide web.This class provides various constructors one of them accepts a String parameter and constructs an object of the URL class.The openStream() method of this class opens a connection to the URL represented by the current object and returns an InputStream object using which you can read data from the URL.Therefore, to read data from web page (using the URL class) −Instantiate the java.net.URL class by passing the URL of the desired web ... Read More

How to handle EOFException in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:20:58

1K+ Views

While reading the contents of a file in certain scenarios the end of the file will be reached in such scenarios a EOFException is thrown.Especially, this exception is thrown while reading data using the Input stream objects. In other scenarios a specific value will be thrown when the end of file reached.ExampleLet us consider the DataInputStream class, it provides various methods such as readboolean(), readByte(), readChar() etc.. to read primitive values. While reading data from a file using these methods when the end of file is reached an EOFException is thrown.import java.io.DataInputStream; import java.io.FileInputStream; public class EOFExample {    public ... Read More

Advertisements