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

Java Methods

The document provides various Java programming techniques including counting frequency with HashMap, converting characters to integers, creating lists of lists, and breaking out of nested loops. It also includes a recursive method to find the power of a number and explains the concept of pass by value and pass by reference in Java. Additionally, it highlights the memory behavior of lists in Java when using references.

Uploaded by

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

Java Methods

The document provides various Java programming techniques including counting frequency with HashMap, converting characters to integers, creating lists of lists, and breaking out of nested loops. It also includes a recursive method to find the power of a number and explains the concept of pass by value and pass by reference in Java. Additionally, it highlights the memory behavior of lists in Java when using references.

Uploaded by

mdwivedigod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

TO COUNT FREQUENCY USING HASHMAP-[use of


getOrDefault() function]

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

Cool.put(arr[i], cool.getOrDefault(arr[i],0)+1);
}

2. TO CONVERT CHAR TO INT;


Int n=ch-‘0’; //where ch is a character

OR
char ch = 'A';
int ascii = (int) ch; // ASCII value of 'A' (65)

3. CREATING LIST OF LIST-


for (int r = 1; r <= k; r++) {
groups[r] = new ArrayList<>();
}
4. WAY TO BREAK DIRECTLY OUT OF OUTTER LOOP-

outer: for (int i = 0; i < n - 1; i++) {

for (int j = i + 1; j < n; j++) {

long xor = arr[i] ^ arr[j];

product *= xor;

if (product > r) {

productOverflow = true;

break outer;

RECURSSION-

FIND SQUARE OF ANY NUMBER


import java.util.*;

public class Main{

public static double myPow(double x, int n) {

double ans = 1.0;

long nn = n;

if (nn < 0) nn = -1 * nn;

while (nn > 0) {

if (nn % 2 == 1) {

ans = ans * x;

nn = nn - 1;

} else {

x = x * x;

nn = nn / 2;

if (n < 0) ans = (double)(1.0) / (double)(ans);

return ans;

public static void main(String args[])

System.out.println(myPow(2,10));

CREATING LIST OF LIST IN JAVA-

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

Java stores references to listing inside listOfLists, not a copy. So if you


clear listing, the list inside listOfLists also becomes empty — because they
are the same object in memory.
PASS BY VALUE AND PASS BY REFERENCE IN JAVA FOR
RECURSSION

Java doesn't support passing primitive types by reference, but arrays and
objects are passed by reference. So we can pass an int[] count = new
int[1]; and update count[0] inside the recursive method.

You might also like