Object Oriented Programming Articles

Page 196 of 589

Can we convert an array to list and back in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 335 Views

Yes, Java provides built-in methods to convert between arrays and lists. Use Arrays.asList() to convert an array to a list, and list.toArray() to convert a list back to an array. Array to List Use Arrays.asList() to convert an array into a list. Wrap it in an ArrayList constructor to get a modifiable list − List list = new ArrayList(Arrays.asList(array)); List to Array The List interface provides two toArray() methods − 1. Without parameter − Returns an Object[] array. Object[] items = list.toArray(); 2. With typed array − Returns ...

Read More

How to convert a Java list to a set?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 328 Views

A Java List can be converted to a Set to eliminate duplicate entries. The resulting Set will contain only unique values. There are three common ways to perform this conversion − Method 1: Using Set Constructor Pass the list directly to the HashSet constructor − Set set = new HashSet(list); Method 2: Using addAll() Create an empty set and use addAll() to add all elements from the list − Set set = new HashSet(); set.addAll(list); Method 3: Using Streams (Java 8+) Use the Stream API to collect list ...

Read More

How to check the Java list size?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 872 Views

The Java List interface provides a size() method to check the current number of elements in the list. The size updates automatically as elements are added or removed. Syntax int size() Returns the number of elements in the list. If the list contains more than Integer.MAX_VALUE elements, it returns Integer.MAX_VALUE. Example The following example shows how the list size changes after adding and removing elements ? import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { ...

Read More

How do I add multiple items to a Java ArrayList in single statement?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 3K+ Views

We can use Arrays.asList() method to add multiple items to a Java ArrayList in a single statement. This method returns a fixed-size list backed by the specified array, which we then pass to the ArrayList constructor to create a modifiable list. Syntax public static List asList(T... a) Returns a fixed-size list backed by the specified array. T − The runtime type of the array. a − The array by which the list will be backed. Note − The list returned by Arrays.asList() is fixed-size, meaning you cannot add or remove ...

Read More

Difference between Java and Kotlin in Android with Examples

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 369 Views

Kotlin was introduced for Android development as a modern alternative to Java, offering concise syntax, null safety, and many language-level improvements. Google announced Kotlin as the preferred language for Android development in 2019. Code Comparison Examples Kotlin reduces boilerplate code significantly compared to Java − Setting Text on a View // Java: requires casting and explicit reference TextView displayText = (TextView) findViewById(R.id.textView); displayText.setText("Hello World"); // Kotlin: concise with synthetic view binding textView.setText("Hello World") Null Safety // Kotlin enforces null check at compile time var value: String = "abc" ...

Read More

Difference between Traits and Abstract Classes in Scala.

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 1K+ Views

In Scala, both traits and abstract classes can contain abstract and non-abstract methods. Traits are similar to Java interfaces (with default methods), while abstract classes are similar to Java abstract classes. The key difference is that traits support multiple inheritance, while abstract classes support only single inheritance. Traits Traits are created using the trait keyword. They can contain both abstract and concrete methods. A class can mix in multiple traits using the with keyword, and traits can also be added to individual object instances at creation time. Abstract Classes Abstract classes are created using the abstract ...

Read More

Difference between the largest and the smallest primes in an array in Java

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 1K+ Views

Given an array of integers (all elements less than 1, 000, 000), find the difference between the largest and smallest prime numbers in the array. Worked Example Array: [1, 2, 3, 4, 5] Primes in array: 2, 3, 5 Largest Prime = 5 Smallest Prime = 2 Difference = 5 - 2 = 3 Solution Approach Use the Sieve of Eratosthenes to pre-compute all prime numbers up to 1, 000, 000. Then scan the array to find the largest and smallest primes and return their difference. The sieve runs in ...

Read More

Difference between sums of odd level and even level nodes of a Binary Tree in Java

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 491 Views

Given a binary tree, find the difference between the sum of nodes at odd levels and the sum of nodes at even levels. The root is at level 1 (odd), its children are at level 2 (even), and so on. Problem Statement The tree structure for our example is − Level 1 (odd) Level 2 (even) Level 3 (odd) Level 4 (even) ...

Read More

Difference between sums of odd and even digits.

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 611 Views

Given a number n, check whether the difference between the sum of digits at odd positions and the sum of digits at even positions is zero. Positions are indexed starting from 0 (rightmost digit). Key Insight A number is divisible by 11 if and only if the alternating sum of its digits (difference between sum of digits at odd and even positions) is 0 or divisible by 11. So a simple n % 11 == 0 check can determine if the difference is zero. Worked Example For n = 1212112 − Digits: ...

Read More

Difference between sum of the squares of and square of sum first n natural numbers.

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 1K+ Views

Given a number n, find the difference between the square of the sum and the sum of the squares of the first n natural numbers. This is a classic mathematical problem that can be solved efficiently using direct formulas. Formulas The two formulas used are − Sum of squares of first n natural numbers: n(n+1)(2n+1) / 6 Sum of first n natural numbers: n(n+1) / 2, then square it The difference is: (Square of Sum) − (Sum of Squares). Worked Example For n = 3 − Sum of squares = ...

Read More
Showing 1951–1960 of 5,881 articles
« Prev 1 194 195 196 197 198 589 Next »
Advertisements