0% found this document useful (0 votes)
8 views12 pages

Java Notes

Java notes

Uploaded by

vbramhankar18
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)
8 views12 pages

Java Notes

Java notes

Uploaded by

vbramhankar18
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
You are on page 1/ 12

FunctionalInterface

-------------------------

@FunctionalInterface

interface Addition{

public int sum(int x, int y);

public class Java8 {

public static void main(String[] args) {

int a=9, b=8;

Addition addition=(x, y)->{ return x+y;};

int sum=addition.sum(a, b);

System.out.println(sum);

Stream

---------

import java.util.*;

class Product{

int id;

String name;

float price;

public Product(int id, String name, float price) {

this.id = id;

this.name = name;

this.price = price;

}
public class JavaStreamExample {

public static void main(String[] args) {

List<Product> productsList = new ArrayList<Product>();

productsList.add(new Product(1,"HP Laptop",25000f));

productsList.add(new Product(2,"Dell Laptop",30000f));

productsList.add(new Product(3,"Lenevo Laptop",28000f));

productsList.add(new Product(4,"Sony Laptop",28000f));

productsList.add(new Product(5,"Apple Laptop",90000f));

List<Float> productPriceList2 =productsList.stream()

.filter(p -> p.price > 30000) // filtering data

.map(p->p.price) // fetching price

.collect(Collectors.toList()); // collecting as list

System.out.println(productPriceList2);

array sorting and convert array to list

----------------------------

Integer[] arr={1, 9, 7, 1, 4, 6, 7, 8};

Arrays.sort(arr);

List<Integer> list= Arrays.asList(arr);

System.out.println(list);

fill array with a particular value

-------------------------------------

Integer[] arr=new Integer[6];

Arrays.fill(arr, -1);

List<Integer> list= Arrays.asList(arr);


System.out.println(list);

methods of array

-------------------

tostring

---------

int[] intArr = { 10, 20, 15, 22, 35 };

System.out.println(Arrays.toString(intArr)); ----> [10, 20, 15, 22, 35]

sorting 1D array

-----------------

Arrays.sort(arr);

Arrays.sort(arr, 2, 6); ----------> Arrays.sort(arr, from, to);

array to list

---------------

Integer[] arr={1, 3, 7, 5, 4, 6, 7, 8};

List<Integer> list= Arrays.asList(arr);

fill with new element

----------------------

int[] arr2=new int[6];

Arrays.fill(arr2, -1); ----> [-1, -1, -1, -1, -1, -1]

Arrays.fill(arr2, 2, 4, -2); ---->Arrays.fill(arr2, from, to, value); [-1, -1, -2, -2, -1, -1]
search

---------

Arrays.binarySearch(arr, 1) ---> return index

Sorting 2D array

------------------

class Job{

int id;

int deadline;

int profit;

public Job(int i, int d, int p){

id=i;

deadline=d;

profit=p;

int[][] jobsInfo={{4, 20}, {1, 10}, { 1, 40}, {1, 30}};

ArrayList<Job> jobs=new ArrayList<Job>();

for(int i=0;i<jobsInfo.length;i++){

jobs.add(new Job(i, jobsInfo[i][0], jobsInfo[i][1]));

Collections.sort(jobs, (a, b)->b.profit-a.profit); //desceding

Collections.sort(jobs, (a, b)->a.profit-b.profit); //asceding

OR

---

int[] start={8, 0, 3, 1, 5, 5};

int[] end={9, 6, 4 ,2, 9, 7};


//if end time not sorted then use below way to sort otherwise ignore.

int[][] activities=new int[start.length][3];

for(int i=0; i<start.length;i++){

activities[i][0]=i;

activities[i][1]=start[i];

activities[i][2]=end[i];

Arrays.sort(activities, Comparator.comparingDouble(x->x[2]));

copy of a array into another array

-----------------------------------

int[] arr={1, 9, 7, 5, 4, 6, 7, 8};

int[] arr1=Arrays.copyOf(arr, 4); ------> [1, 9, 7, 5]

int[] arr1=Arrays.copyOf(arr, arr.length); --------> [1, 9, 7, 5, 4, 6, 7, 8]

int[] arr1=Arrays.copyOf(arr, 15); ---> [1, 9, 7, 5, 4, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0]

int[] arr3=Arrays.copyOfRange(arr, 3, 6 ); ----->Arrays.copyOfRange(arr, from, to )

comparison of arrays

--------------------

Arrays.equals(arr, arr1); ----->true/false

Arrays.equals(arr, 2, 4, arr1, 3, 5); ----> Arrays.equals(arr, from, to, arr1, from, to)

mismatch

----------

int[] arr={1, 3, 7, 5, 4, 6, 7, 8};

int[] arr1={1, 3, 4, 5, 4, 6, 7, 8};

Arrays.mismatch(arr, arr1) -------> 2

int[] arr={1, 3, 7, 5, 4, 6, 7, 8};


int[] arr1={1, 3, 7, 5, 4, 6, 7, 8};

Arrays.mismatch(arr, arr1) -------> -1

Arrays.mismatch(arr, 3, 5, arr1, 3, 8) ----> Arrays.mismatch(arr, 3, 5, arr1, 3, 8)

java8 on array

---------------

int[] arr={1, 3, 7, 5, 4, 6, 7, 8};

Arrays.stream(arr).filter(x->x>3).map(x->x+10).forEach(x->System.out.print(x+ " "));

Arrays.stream(arr).filter(x->x>3).map(x->x+10).sorted().forEach(x->System.out.print(x+" "));

convert int[] into Integer[] for applying collections method

----------------------------------------------------------------

Integer[] newArr=Arrays.stream(arr).boxed().toArray(Integer[]::new);

Arrays.asList(newArr).stream().filter(x->x>3).map(x-
>x+10).sorted(Collections.reverseOrder()).forEach(x->System.out.print(x+" "));

List<Integer> list= Arrays.asList(newArr).stream().filter(x->x>3).collect(Collectors.toList());

Set<Integer> set= Arrays.asList(newArr).stream().filter(x->x>3).collect(Collectors.toSet());

Map<Integer, Integer> map= Arrays.asList(newArr).stream().filter(x->x>3).collect(Collectors.toMap(x-


>keyGenerator(), x->x));

static int i=0;

private static int keyGenerator() {

return i++;

--------------------------------------------------------------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------------------------------------------------------------
--------------

--------------------------------------------------------------------------------------------------------------------------------------
--------------

String

-------

String s="asdfghjkl";

char ch =s.charAt(3); ---> f

char ch =s.charAt(20); ---> StringIndexOutOfBoundsException at runtime

s.equals("asdfghjkl") ---> true

s.equalsIgnoreCase("asdfghjkl") ---> true

int l=s.length(); ---> 9

String s1=s.toLowerCase(); ----> asdfghjkl

String s1=s.toUpperCase(); ----> ASDFGHJKL

String s1=s.concat("zxcvbnm"); ----> asdfghjklzxcvbnm

String s1=s.repeat(2); -----> asdfghjklasdfghjkl

String s="asdfghjkl";

String s1=s.replace('d', 'o'); ----> asofghjkl

String s1=s.replace("dfg", "o"); ----> asohjkl

String s="asdfgh899jkl";

String s1=s.replaceAll("[0-9]", "ABC"); ---> asdfghABCABCABCjkl

String s="asdagh89ajal";

String s1=s.replaceFirst("a", "ABC"); ----> ABCsdagh89ajal


String s="a,s,d,f,g,hjkl";

String[] s1=s.split(","); ----> [a, s, d, f, g, hjkl]

String s=" asdagh8 9ajal ";

String s1=s.strip(); ---->asdagh8 9ajal---> all leading and trailing white space removed.

String s1=s.stripLeading(); ---->asdagh8 9ajal ---> all leading white space removed.

String s1=s.stripTrailing(); ----> asdagh8 9ajal---> all trailing white space removed.

String s="asdfghjkl";

String s1=s.substring(3); ---> fghjkl

String s1=s.substring(3, 5); ---> fg

String s="asdfghjkl";

s.startsWith("asd") ---> true

s.endsWith("kl") ---> true

String s="asdfghjkl";

s.matches("[A-Za-z]*") --> true

String s="asdfghjkl";

char[] s1=s.toCharArray(); ---> [a, s, d, f, g, h, j, k, l]

String s="asdfghjkl";

s.contains("asd") --> true

String s="asdfghjkl";

int s1=s.indexOf("df"); --> 2

String s="";

s.isBlank() --> true


String s=" ";

s.isBlank() --> true

String s="d";

s.isBlank() --> flase

String s="";

s.isEmpty() --> true

String s=" ";

s.isEmpty() --> flase

String s="d";

s.isEmpty() --> flase

------------------------------------------------------------------------

------------------------------------------------------------------------

List<Integer> list=new ArrayList<>();

list.add(1);

list.add(5);

list.add(14);

list.add(4);

list.add(9);

System.out.println(list); ---------> [1, 4, 5, 9, 14]

list.size() --------> 5

list.isEmpty() -----------> false

list.remove(3) ---------> 3 is the index -- [1, 4, 5, 14]

list.toString() ----> [1, 5, 14, 4, 9]

list.addAll(list1);
Collections.sort(list); -----> [1, 4, 5, 9, 14]

Collections.max(list) ------> 14

Collections.min(list) --------> 1

Collections.frequency(list, 1) ---------> 1

Collections.reverse(list); --------> [9, 4, 14, 5, 1]

Collections.binarySearch(list, 14) -----> 2

list to array

--------------

Object[] arr=list.toArray();

Arrays.toString(arr) --------> [1, 5, 14, 4, 9]


------------------------------------------------------------------------

------------------------------------------------------------------------

Most Frequent/repeated Element in an Array in Java

-------------------------------------------------------

public static int freq(int[] arr, int n){

int max_count = 0;

int maxfreq = 0;

for (int i = 0; i < n; i++) {

int count = 0;

for (int j = 0; j < n; j++) {

if (arr[i] == arr[j]) {

count++;

if (count > max_count) {

max_count = count;

maxfreq = arr[i];

return maxfreq;

You might also like