0% found this document useful (0 votes)
3 views3 pages

Java DSA Essentials Sorting Conversion Overflow

The document provides essential Java concepts for interviews, focusing on sorting techniques for arrays, lists, and maps, as well as type conversions between different data types. It also addresses handling large numbers and overflow issues, offering solutions like using Long and BigInteger for safe calculations. This serves as a quick reference guide for sorting, conversions, and overflow handling in Java.

Uploaded by

ujwalsharma1748
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java DSA Essentials Sorting Conversion Overflow

The document provides essential Java concepts for interviews, focusing on sorting techniques for arrays, lists, and maps, as well as type conversions between different data types. It also addresses handling large numbers and overflow issues, offering solutions like using Long and BigInteger for safe calculations. This serves as a quick reference guide for sorting, conversions, and overflow handling in Java.

Uploaded by

ujwalsharma1748
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java DSA Interview Essentials: Sorting, Type Conversion & Overflow Handling

SECTION 1: SORTING IN JAVA

Primitive Arrays (int[], char[], etc.)

[Link](nums); // Ascending

Integer[] nums = {5, 2, 9};

[Link](nums, [Link]()); // Descending

Custom Comparator Sorting

[Link](arr, (a, b) -> (b + a).compareTo(a + b)); // For largest number problems

Sorting Lists

[Link]((a, b) -> b - a); // Descending

[Link](list); // Ascending

2D Array Sorting

Sort by 1st element: [Link](arr, (a, b) -> a[0] - b[0]);

Sort by 2nd element desc: [Link](arr, (a, b) -> b[1] - a[1]);

Sorting Maps by Value

[Link]().stream()

.sorted([Link]())

.forEach(entry -> [Link](entry));

SECTION 2: TYPE CONVERSIONS IN JAVA

Integer <-> String

int x = 123; String s = [Link](x); int y = [Link](s);

Double <-> String


double d = 10.5; String s = [Link](d); double d2 = [Link](s);

Char Array <-> String

char[] arr = [Link](); String s2 = new String(arr);

Integer <-> Long / Double

long l = (long) x; double d = (double) x;

Char <-> Integer (ASCII)

int ascii = (int) c; char ch = (char) (ascii + 1);

Array <-> List

List<Integer> list = [Link](arr); Integer[] arr2 = [Link](new Integer[0]);

Primitive to Stream List

List<Integer> list = [Link](nums).boxed().collect([Link]());

SECTION 3: HANDLING LARGE NUMBERS (OVERFLOW)

Overflow Issue

int a = 1_000_000_000; int result = a * 3; // Overflow

Fix with Long

long result = 1L * a * b;

Safe Compare from String to int

String numStr = "9999999999";

long num = [Link](numStr);

if (num > Integer.MAX_VALUE) return Integer.MAX_VALUE;

BigInteger for Super Large Numbers

BigInteger big = new BigInteger("9999999999999999");


BigInteger max = new BigInteger([Link](Integer.MAX_VALUE));

if ([Link](max) > 0) return Integer.MAX_VALUE;

Constants

Integer.MAX_VALUE = 2147483647

Integer.MIN_VALUE = -2147483648

Long.MAX_VALUE = 9223372036854775807L

Use this as your quick reference when stuck on sorting, conversions, or overflow handling during interviews

or contests.

You might also like