Input: item[] = {“toy”, “pen”, “notebook”, “pen”}, price[] = {2, 1, 3, 2}
Output:
Item Min Max Average
pen 1 2 1.5
toy 2 2 2.0
notebook 3 3 3.0
Input: item[] = {“car”, “car”}, price[] = {20000, 30000};
Output:
Item Min Max Average
car 20000 30000 25000.0
import java.util.HashMap;import java.util.Map;
public class Example01 {
static class Item{
int min, max, total, sum;
Item(int price){
min = price;
max = price;
total = 1;
this.sum = price;
}
}
static void findPrices(String item[], int price[], int n){
HashMap<String, Item> map = new HashMap<>();
for(int i=0; i<n; i++){
if(map.containsKey(item[i])){
Item currItem = map.get(item[i]);
currItem.min = Math.min(currItem.min, price[i]);
currItem.max = Math.max(currItem.max, price[i]);
currItem.total++;
currItem.sum += price[i];
} else{
Item currItem = new Item(price[i]);
map.put(item[i], currItem);
}
}
System.out.println("Item Min Max Avr");
for(Map.Entry<String, Item> ob : map.entrySet()){
String key = ob.getKey();
Item currItem = ob.getValue();
System.out.println(key + " " + currItem.min + " " + currItem.max + " " + ((float)currItem.sum/ (float)currItem.total));
}
}
public static void main(String[] args) {
String item[] = { "toy", "pen", "notebook", "pen" };
int n = item.length;
int price[] = { 2, 1, 3, 2 };
findPrices(item, price, n);
}
}
----
import java.util.HashMap;
public class Example03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("key1", 1);
map.put("key2", 2);
map.put("key3", 3);
map.put("key4", 4);
System.out.println("Hashmap1: " + map.toString());
map.replaceAll((key, oldValue) -> oldValue * oldValue);
System.out.println("new Hashmap : " + map.toString());
HashMap<String, Integer> map2 = new HashMap<>();
map2.put("A", 2000);
map2.put("B", 2001);
map2.put("C", 1994);
System.out.println("Year of birth: " + map2.toString());
map2.replaceAll((key, yearOfBirth) -> 2019 - yearOfBirth);
System.out.println("Ages: " + map2.toString());
}
}
---------
Given an array arr[] having 4 integer digits only. The task is to return the maximum 24 hour time that can be formed using the digits from the array.
Note that the minimum time in 24 hour format is 00:00, and the maximum is 23:59. If a valid time cannot be formed then return -1.
Input: arr[] = {1, 2, 3, 4}
Output: 23:41
Input: arr[] = {5, 5, 6, 6}
Output: -1
import java.util.HashMap;
public class MaxTime {
public static void main(String[] args) {
int arr[] = {0,0,0,9};
System.out.println(getMaxTime(arr));
int arr2[] = {1,2,3,4};
System.out.println(getMaxTime(arr2));
int arr3[] = {5,6,7,8};
System.out.println(getMaxTime(arr3));
}
static String getMaxTime(int arr[]) {
String time = "";
boolean possible = false;
HashMap<Integer, Integer> freqs = new HashMap<>();
freqs = findFreqs(arr);
//for hour
for(int i=2; i>= 0; i--){
if(hasDigit(freqs, i)){
possible = true;
time += i;
break;
}
}
if(possible==false){
return "No Possible with That input";
}
possible = false;
if(time.charAt(0) == '2'){
for(int i=3; i>=0;i--){
if(hasDigit(freqs, i)){
time += i;
possible = true;
break;
}
}
} else{
for(int i=9; i>=0;i--){
if(hasDigit(freqs, i)){
time += i;
possible = true;
break;
}
}
}
if(possible==false){
return "No Possible with That input";
}
possible = false;
time += " : ";
for(int i=5; i>=0;i--){
if(hasDigit(freqs, i)){
time += i;
possible = true;
break;
}
}
if(possible==false){
return "No Possible with That input";
}
possible = false;
for(int i=9; i>=0;i--){
if(hasDigit(freqs, i)){
time += i;
possible = true;
break;
}
}
if(possible==false){
return "No Possible with That input";
}
return time;
}
private static HashMap<Integer, Integer> findFreqs(int[] arr) {
HashMap<Integer, Integer> freqs = new HashMap<>();
for(int i=0; i<arr.length; i++){
if(!freqs.containsKey(arr[i])){
freqs.put(arr[i], 1);
} else{
freqs.replace(arr[i], freqs.get(arr[i])+1);
}
}
return freqs;
}
private static boolean hasDigit(HashMap<Integer, Integer> map, int j) {
if(map.containsKey(j) && map.get(j) >0){
map.replace(j, map.get(j) -1);
return true;
}
return false;
}
}
----
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class IterationExample {
public static void main(String[] args) {
int arr[] = {2, 4, 6, 8, 1, 6};
HashMap<Integer, Integer> map = new HashMap<>();
for(int i=0;i< arr.length; i++){
map.put(arr[i], i);
}
for(Map.Entry<Integer, Integer> s : map.entrySet()){
System.out.println(s.getKey() + "-" + s.getValue());
}
Iterator<Integer> it = map.keySet().iterator();
while(it.hasNext()){
Integer key = it.next();
System.out.println(key + "/" + map.get(key));
}
}
}
public class AnagramExample {
static boolean checkForAnagram(String s1, String s2) {
if(s1.length() != s2.length()) {
return false;
}
HashMap charMap1 = new HashMap();
for(int i =0;i< s1.length(); i++) {
if(charMap1.containsKey(s1.charAt(i))) {
charMap1.put(s1.charAt(i), charMap1.get(s1.charAt(i)) +1);
} else {
charMap1.put(s1.charAt(i), 1);
}
}
HashMap charMap2 = new HashMap();
for(int i =0;i< s2.length(); i++) {
if(charMap2.containsKey(s2.charAt(i))) {
charMap2.put(s2.charAt(i), charMap2.get(s2.charAt(i)) +1);
} else {
charMap2.put(s2.charAt(i), 1);
}
}
Iterator
Hiç yorum yok:
Yorum Gönder