int f1 = 0, f2 = 1;
int f3 = 0;
System.out.print(f1+" ");
System.out.print(f2+" ");
for (int i = 1; i <=8; i++) {
f3 = f1 + f2;
System.out.print(f3+" ");
f1 = f2;
f2 = f3;
}
int f1 = 0, f2 = 1;
int f3 = 0;
System.out.print(f1+" ");
System.out.print(f2+" ");
for (int i = 1; i <=8; i++) {
f3 = f1 + f2;
System.out.print(f3+" ");
f1 = f2;
f2 = f3;
}
package arrayoperations;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MainApp {
public static void main(String[] args) {
int[] a = { 1, 2, 2, 3, 4, 4, 4 };
Map<Integer, Integer> frequency = new HashMap();
for (int i = 0; i < a.length; i++) {
if (frequency.containsKey(a[i])) {
frequency.put(a[i], frequency.get(a[i]) + 1);
} else {
frequency.put(a[i], 1);
}
}
Set<Integer> keySet = frequency.keySet();
for(Integer i: keySet) {
System.out.println("item: "+ i+ " frequency: "+frequency.get(i));
}
}
}
fetch
('https://crossorigin.me/https://www.metaweather.com/api/location/2487956/')
.then(result => {
console.log(result);
return result.json(); //fetch () will return a promise and converting to json object
})
.then(data=>console.log(data))
.catch(error => {
console.log(error);
});
here https://crossorigin.me is used to prevent same origin policy.
const ids = new Promise((resolve, reject) => { //promise 1
setTimeout(() => {
resolve([100, 200, 300, 400, 500]);
}, 1500);
});
const getReceipe = recId => { //promise 2
return new Promise((resolve, reject) => {
setTimeout(id => {
const receipe = {
title: 'potato',
publisher: 'jonas'
};
resolve(`${id}:${receipe.title}`);
}, 1500, recId);
});
};
async function getReceipeAW() {
const allIds = await ids; // here ids is the promise . await keyword is used to wait
//until the promise resolved
console.log(allIds);
const receipe = await getReceipe(allIds[2]);
console.log(receipe);
return receipe;
}
getReceipeAW().then(result=>{ // here getReceipeAW() will return a promise then
// we need to consume it.
console.log(`${result} is the best food ever`);
})
--async and await is used to consume promises , not to produce them .
--async and await makes easier to consume promise over then and catch .
--await can only be used inside async function.
--an async function is always returns a promise automatically.
Example:
const ids = new Promise((resolve, reject) => { //promise 1
setTimeout(() => {
resolve([100, 200, 300, 400, 500]);
}, 1500);
});
const getReceipe = recId => { //promise 2
return new Promise((resolve, reject) => {
setTimeout(id => {
const receipe = {
title: 'potato',
publisher: 'jonas'
};
resolve(`${id}:${receipe.title}`);
}, 1500, recId);
});
};
//Consuming promises
//Async & await
async function getReceipeAW() { //here async is the keyword
const allIds = await ids; // here ids is the promise . await keyword is used to wait
//until the promise resolved
console.log(allIds);
const receipe = await getReceipe(allIds[2]);
console.log(receipe);
}
getReceipeAW();
Let's see the below example,
const ids = new Promise((resolve, reject) => {
setTimeout(() => {
resolve([100, 200, 300, 400, 500]);
}, 1500);
});
const getReceipe = recId => {
return new Promise((resolve, reject) => {
setTimeout(id => {
const receipe = {
title: 'potato',
publisher: 'jonas'
};
resolve(`${id}:${receipe.title}`);
}, 1500, recId);
});
};
ids.then(ids => {
console.log(ids);
return getReceipe(ids[2]);//returning another promise object
})
.then(receipe => {
console.log(receipe);
})
.catch(error => {
console.log(error);
})
Promises is an object that keeps track of some event is happened or not .
--Determines what happens after the event has happened.
Promise have different states -
public class UserConfigurationManager { private String userName; private String password; private UserConfigurationManager() { ...