ARRAYS_DAY-4
2D Arrays and Sub-Array
Q24) Given a non-empty array of integers arr, every element appears twice except for one. Find that single one.
int []arr = {5,1,2,4,3,1,2,4,3};
Every Element is appearing twice except 5 so the answer should be 5.package com.ravi.array_primitive;
import java.util.HashMap;
public class ArrayExDemo23 {
public static void main(String[] args)
{
int []arr = {5,1,2,4,3,1,2,4,3};
HashMap<Integer, Integer> map = new HashMap<>(); //5 1 2 4 3 1
for(int element : arr)
{
map.put(element, map.getOrDefault(element,0) + 1);
}
for(int key : map.keySet()) //5 1 2 4 3
{
if(map.get(key) == 1)
{
IO.println(key);
}
}
}
}package com.ravi.array_primitive;
import java.util.HashMap;
public class ArrayDemoEx24 {
public static void main(String[] args)
{
HashMap<String,Integer> map = new HashMap<>();
String str = "Java is a fun language, Java is awesome, Java is
beautiful";
String[] words = str.split(" ");
for(String word : words)
{
map.put(word, map.getOrDefault(word,0)+1);
}
IO.println("Frequency is :"+map);
}
}What is a subarray in an original array
• A susubarrays theart of the original array.
• It represents a continuous (adjacent) element in the existing array.
int []arr = {1,2,3,4,5};Range of Index → Sub array of Original Array
0 - 0 1
0 - 1 1 2
0 - 2 1 2 3
0 - 3 1 2 3 4
0 - 4 1 2 3 4 5
1 - 1 2
1 - 2 2 3
1 - 3 2 3 4
1 - 4 2 3 4 5
2 - 2 3
2 - 3 3 4
2 - 4 3 4 5
3 - 3 4
3 - 4 4 5
4 - 4 5Logic
for(int i=0; i<arr.length; i++)
{
for(int j=i; j<arr.length; j++)
{
for(int k=i; k<=j; k++) //How to print from i to j
{
IO.println(arr[k]);
}
}
}package com.ravi.array_primitive;
public class ArrayDemoEx25 {
public static void main(String[] args)
{
int []arr = {1,2,3,4,5};
for(int i=0; i<arr.length; i++)
{
for(int j=i; j<arr.length; j++)
{
for(int k=i; k<=j; k++)
{
IO.print(arr[k]+" ");
}
IO.println();
}
}
}
}Comparable<T> and Comparator<T> interface
• Both are functional interfaces because both contain exactly one abstract method.
Get Aman Verma’s stories in your inbox
Join Medium for free to get updates from this writer.
• We can only say two objects are comparable, if the corresponding class implements the Comparable interface.
Note String, StringBuffer, StringBuilder a,nd all the Wrapper classes are implementing from Comparable<T>.
Comparable<T>
- Available in jJavalang package
- public int compareTo(T x);
- Due to current class support, we need to modify the BLC class
- Can write only one sorting logic.
- Provides default natural sorting order
- Arrays.sort(T[] obj);
OR
Collections.sort(List<E> obj)
Comparator<T>
- Available in Java.util package
- public int compare(T x, T y);
- No need to modify the BLC class
- Can write multiple sorting logics
- Provides custom (user-defined) sorting order
- Arrays.sort(T[] obj, Comparator<T> cmp);
Collections.sort(List<E> obj, Comparator<T> cmp);
list.sort(Comparator<T> cmp)
Program on Comparable
package com.ravi.comparable;
public record Customer(Integer id, String name) implements
Comparable<Customer>
{
@Override
public int compareTo(Customer c2)
{
return this.name.compareTo(c2.name);
}
}package com.ravi.comparable;
import java.util.Arrays;
public class CustomerComparable {
public static void main(String[] args)
{
int size = Integer.parseInt(IO.readln("Enter the size :"));
Customer customers[] = new Customer[size];
for(int i=0; i<size; i++)
{
int id = Integer.parseInt(IO.readln("Enter Customer id :"));
String name = IO.readln("Enter custom Name :");
customers[i] = new Customer(id, name);
}
IO.println("Original Data :");
for(Customer customer : customers)
{
IO.println(customer);
}
Arrays.sort(customers);
IO.println("After sorting based on the name :");
for(Customer customer : customers)
{
IO.println(customer);
}
}
}Limitation of Comparable
We have 2 limitations :
- Can write only one sorting logic
- If the BLC class is provided by some 3rd party and we cannot modify the BLC class, then to provide custom sorting, we should usea Comparator.
Program on Comparator
package com.ravi.comparator;
public record Product(Integer id, String name, Double price)
{
}package com.ravi.comparator;
import java.util.Arrays;
import java.util.Comparator;
public class ProductComparator {
public static void main(String[] args)
{
Product products[] = new Product[3];
products[0] = new Product(222, "Mobile", 24900);
products[1] = new Product(111, "Laptop", 29890);
products[2] = new Product(333, "Camera", 21900);
IO.println("Original Data :");
for(Product product : products)
{
IO.println(product);
}
IO.println("Sorting based on the Product Id :");
Comparator<Product> cmpId = new Comparator<Product>()
{
@Override
public int compare(Product p1, Product p2)
{
return Integer.compare(p1.id(), p2.id());
}
};
Arrays.sort(products, cmpId);
for(Product product : products)
{
IO.println(product);
}
IO.println("Sorting based on the Product Name :");
Comparator<Product> cmpName = (p1,p2) -> p1.name().compareTo(p2.name());
Arrays.sort(products, cmpName);
for(Product product : products)
{
IO.println(product);
}
IO.println("Sorting based on the Product price :");
Arrays.sort(products, (p1,p2) -> Double.compare(p1.price(), p2.price()));
for(Product product : products)
{
IO.println(product);
}
}
}Explanation from diagram:• HashMap has 16 buckets, and the entire HashMap gets locked, so only one thread can access it at a time.
• ConcurrentHashMap uses Bucket Level Locking.
• For 16 buckets, 16 locks are available, so multiple threads can access different buckets simultaneously.
• Developers can set the number of locks using the concurrency level constructor.








