Java Basics - 4-W3D2-1
~30 mins
Week 3 - Day 2
package week3.day2;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CountOfCharacters {
public static void main(String[] args) {
String desiredCompany = "Amazon Development Center";
// You need to find the count of each character
// A -> 1
// m -> 2
// a -> 1
// o -> 2
/*
* psuedocode
*
* 1) Convert String to character array
* 2) Add the character to the map with default value as 1
* 3) If it is already existing, increment the value -> get the existing value + 1
* 4) Finally print the character and count
*
*/
/*
char[] chArray = desiredCompany.toCharArray();
Map<Character, Integer> countCharacters = new HashMap<Character, Integer>();
for (int i = 0; i < chArray.length; i++) {
if(countCharacters.containsKey(chArray[i])) {
countCharacters.put(chArray[i], countCharacters.get(chArray[i])+1 );
}else {
countCharacters.put(chArray[i], 1);
}
}*/
char[] chArray = desiredCompany.replace(" ", "").toCharArray();
Map<Character, Integer> countCharacters = new HashMap<Character, Integer>();
for (int i = 0; i < chArray.length; i++)
countCharacters.put(chArray[i],countCharacters.getOrDefault(chArray[i], 0)+1);
System.out.println(countCharacters);
// default -> 0 + 1
// already -> get the value + 1
// put -> add an entry to the map
//
}
}
package week3.day2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class LearnList {
public static void main(String[] args) {
// List -> Dynamic Array
// List -> Interface -> You cannot create an object
// Hold data that can grow or shrink !!
// It allows to hold duplicate data
// Implementation class -> ArrayList (Index) , LinkedList (Next, Prev)
// Generic -> Stores the type of allowed data in the given data structure
List<String> learners = new ArrayList<String>();
// Methods
/*
* size -> how many items are there in the list -> int
* add -> add a new item to the list (by default it will add to the last)
* remove -> remove given index or data from the list (if there are multiple, removes first)
* get -> retrieve the specific item using index -> Generic value
* contains -> find whether given item is present or not -> boolean
*/
learners.add("Sherin");
learners.add("Nirmal");
learners.add("arun");
learners.add("Priyanga");
learners.add("Nirmal");
System.out.println(learners.size());
learners.add(0, "Uma");
// Sort the list
Collections.sort(learners);
Collections.reverse(learners);
// Remove Nirmal
//learners.remove("uma");
learners.set(0, learners.get(0)+" Kumar");
for (String eachLearner : learners) {
System.out.println(eachLearner);
}
}
}
package week3.day2;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class LearnMap {
public static void main(String[] args) {
// Dictionary
// Keyword (Key) -> Meaning (Value)
// Key is always unique
// Value can be duplicate
// Map is a data structure that stores key-value pair
// Map -> Interface -> Cannot create object
// It requires implementation class -> HashMap, LinkedHashMap, TreeMap
/*
* HashMap -> Hashing algorithm -> ordered by the hash value
* LinkedHashMap -> Order of insertion
* TreeMap -> Order based on comparator -> ASCII
*/
// Methods
/*
* size -> It returns the number of entries (Entry -> Key : Value)
* put -> add the entry to the map
* remove -> remove the entry from the map based on the key
* containsKey -> returns whether key is present or not -> boolean
*
*/
Map<String, String> trains = new HashMap<String, String>();
trains.put("06795", "MS TPJ EXPRESS");
trains.put("05120", "RMM FESTIVL SPL");
trains.put("08496", "BBS RMM SPL");
trains.put("06865", "MS TJ SPL");
System.out.println(trains.size());
System.out.println(trains.containsKey("05121"));
// I wanted to check by train name
System.out.println(trains.containsValue("MS TJ SPL"));
trains.put("06865", "SBC TJ SPL");
// Wanted to check how many SPL trains are available !!
/*
* 1) Get all the key value pairs (map) -> entrySet
* 2) From each entry -> get the value
* 3) Check if the value contains 'SPL'
* 4) If yes, count it !!
* 5) Finally print the count
*/
System.out.println(trains.get("06865"));
int count = 0;
System.out.println(trains.entrySet());
for (Entry<String, String> eachTrain : trains.entrySet()) {
if(eachTrain.getValue().contains("SPL")) {
count++;
}
}
System.out.println(count);
}
}
package week3.day2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class LearnSet {
public static void main(String[] args) {
// Set -> Dynamic Array
// Set -> Interface -> You cannot create an object
// Hold data that can grow or shrink !!
// It allows to hold unique data (No duplicates allowed)
// Implementation class -> HashSet, LinkedHashSet, TreeSet
/*
* HashSet -> Hashing algorithm (Java internally create hashing value for each key)
* LinkedHashSet -> Maintains the order of adding (like List)
* TreeSet -> Uses Comparator -> ASCII value
*
*/
// Generic -> Stores the type of allowed data in the given data structure
Set<String> learners = new LinkedHashSet<String>();
// Methods
/*
* size -> how many items are there in the list -> int
* add -> add a new item to the list (by default it will add to the last)
* remove -> remove given index or data from the list (if there are multiple, removes first)
* contains -> find whether given item is present or not -> boolean
*
* No get method for the Set !!
*
*/
System.out.println("Sowmya".hashCode());
System.out.println("Annapoorani".hashCode());
learners.add("Sowmya");
learners.add("Annapoorani");
learners.add("Chitra");
learners.add("Dinesh");
learners.add("Janani");
learners.add("Chitra");
System.out.println(learners.size());
//learners.remove("Chitra");
System.out.println(learners.contains("Chitra"));
for (String eachLearner : learners) {
System.out.println(eachLearner);
}
}
}
package week3.day2;
import java.util.HashSet;
import java.util.Set;
public class PrintDuplicateNumbers {
public static void main(String[] args) {
int[] data = {4,3,6,8,29,1,2,4,7,8};
/*
* Problem Statement : Print only duplicate values
* Expected output: 4 and 8
*
* How to solve?
* 1) Regular for loop
* 2) Using Set
*
* Using Set
*
* 1) Add every element into Set
* 2) If it is already there in the Set -> Print duplicate
*
*/
Set<Integer> unique = new HashSet<Integer>();
for (int i = 0; i < data.length; i++) {
if(!unique.add(data[i])) {
System.out.println("It is duplicate "+data[i]);
}
}
// Time Complexity: O(n2) -> Brute Force approach
/*for (int i = 0; i < data.length; i++) { // 1 pass
for (int j = i+1; j < data.length; j++) { // 2 pass
if(data[i] == data[j]) {
System.out.println("The duplicate number is "+data[i]);
break;
}
}
}*/
}
}
package week3.day2;
import java.util.HashSet;
import java.util.Set;
public class PrintUniqueCharacters {
public static void main(String[] args) {
String companyName = "testleaf";
/*
* 1) Convert String into character array
* 2) Add them to the Set
* 3) If it is duplicate, you don't do anything
* 4) Print
*
* int -> Integer
* char -> Character
* boolean -> Boolean
*
*/
char[] allChar = companyName.toCharArray();
Set<Character> unique = new HashSet<Character>();
for (int i = 0; i < allChar.length; i++) {
if(unique.add(allChar[i])) {
System.out.print(allChar[i]);
}
}
}
}
package week3.day2;
import java.util.HashMap;
import java.util.Map;
public class TwoSum {
public static void main(String[] args) {
int[] nums = {2,4,6,7,11,15};
int target = 8;
// Added to the map each value
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
// I traversed again, to find the difference and check in the map
for (int i = 0; i < nums.length; i++) {
int diff = target - nums[i];
if (map.containsKey(diff) && map.get(diff) != i) {
System.out.println( i +" "+ map.get(diff));
break;
}
}
}
}