From the code below is it possible to get
apple 1.50
pears 6
if not then at least 1.50 and 6? I have done a few things to achieve that / read a few stack over flow but now sure how to do that. Thanks in advance for your time/comments. emphasized text
public class RandomCheck {
public static void main(String[] args) {
//Keep Track of Fruit, Quantity and Price per item
Map<String, Map<Integer, Double>> mapOuter = new HashMap<String, Map<Integer, Double>>();
//Keep Track of Quantity and Price per item
Map<Integer, Double> mapInner = new HashMap<Integer, Double>();
mapInner.put(2, .75);
mapInner.put(4, 1.25);
mapOuter.put("apple", mapInner);
mapOuter.put("pears", mapInner);
//ToDo: Get Final price of this purchase all together will be (2*.$75) + (4* $1.25)= $6.5
double finalTotal = 0;
for (Map.Entry<Integer, Double> innerData : mapInner.entrySet()) {
finalTotal = finalTotal + (innerData.getKey() * innerData.getValue());
}
System.out.println("Total price " + finalTotal);
//ToDo:Get itemized total, for Apple it will be 2* $.75 and for pears 4* $1.25
double totalByItem = 0;
/* for (Map.Entry<String, Map<Integer, Double>> outerData : mapOuter.entrySet()) {
for (Map.Entry<Integer, Double> innerData : mapInner.entrySet()) {
// System.out.println(" KEY Outer "+ outerData.getKey() + " KEY Inner " + innerData.getKey() + " Value Inner " + innerData.getValue());
totalByItem = totalByItem + (innerData.getKey() * innerData.getValue());
}
}
System.out.println("By item price " + totalByItem);*/
/* Iterator <k> itr= map.keySet().iteraotr;
while(itr.hasNext()){
K key = its.next();
V value= map.get(key);
}*/
}
}
Shouldn't your Maps be structured like this:
Map<String, Integer> quantity = new HashMap<>();
quantity.put("apple", 2);
quantity.put("pears", 4);
Map<String, Double> price = new HashMap<>();
price.put("apple", .75);
price.put("pears", 1.25);
Then you can do something like:
for(String fruit : quantity.keySet())
{
int fruitQuantity = quantity.get(fruit);
double fruitPrice = price.get(fruit);
// ...
}
By the way, pears total should be 5 not 6.
Related
void displayFeedback(){
Map<String, Integer> maths = new HashMap<String, Integer>();
maths.put("Nirmala", 70);
maths.put("Subaksha", 80);
Map<String, Integer> english = new HashMap<String, Integer>();
english.put("Nirmala", 75);
english.put("Subaksha", 60);
//same staffs taking two different subjects maths and english
//values taken as feedback for each subject
// i need to compare both subject feedback and print only max as o/p
System.out.println(maths);
System.out.println(maths.entrySet());
//maths.
//maths.entrySet();
//Collections.max(coll, comp)
Map<String, Integer> top = new HashMap<String, Integer>();
System.out.println(maths.size());
for (Map.Entry<String, Integer> math: maths.entrySet()){
for (Map.Entry<String, Integer> eng: english.entrySet()){
//for(int i = 0;i<maths.size();i++){
//math.comparingByValue()
System.out.println(math.getValue()+" "+eng.getValue());
//Collections.max(math.getValue(), eng.getValue());
if(math.getValue() <= eng.getValue()){
//System.out.println(" math <= eng");
}else
{
//System.out.println("math > eng");
}
Teachers are common in both map ,they are handling both maths and eng subject
and their feed back in each subjeck getting as values for both subjects
I need to compare and find maximum feedback value and print only max value to
each teacher....t
teacher is common key in both map
you can get the value of english feedback map using the math entrySet() iterator key and select the max value
Map<String, Integer> maths = new HashMap<String, Integer>();
maths.put("Nirmala", 70);
maths.put("Subaksha", 80);
Map<String, Integer> english = new HashMap<String, Integer>();
english.put("Nirmala", 75);
english.put("Subaksha", 60);
System.out.println(english.entrySet());
System.out.println(maths.entrySet());
Map<String, Integer> top = new HashMap<String, Integer>();
for (Map.Entry<String, Integer> math: maths.entrySet()){
System.out.println( "Teacher : " +math.getKey() + " Max Feedback :" + Math.max(math.getValue(), english.get(math.getKey())));
}
Here is mine suggstion ;)
private void printMaxValues(Map<String, Integer> first, Map<String, Integer> second) {
Set<String> keys = first.keySet();
for (String s : keys) {
Integer val1 = first.get(s);
Integer val2 = second.get(s);
if (val1 == null && val2 == null) {
System.out.println("No values for key: " + s);
} else if (val1 == null) {
System.out.println(s + "=" + val2);
} else if (val2 == null) {
System.out.println(s + "=" + val1);
} else {
System.out.println(s + "=" + Math.max(val1, val2));
}
}
}
You can do as follow :
1. build a map from the subjects as key is the teacher, and value is all its feebacks
Map<String, List<Integer>> feedbacks =
Stream.of(maths, english)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(e -> e.getKey(),
e -> new ArrayList<Integer>(Arrays.asList(e.getValue())),
(l1, l2)-> {l1.addAll(l2); return l1;}));
System.out.println(feedbacks); //{Subaksha=[80, 60], Nirmala=[70, 75]}
2. buid a map from the feedbacks as key is the teacher, and value is the max of its feedback
Map<String, Integer> maxs = feedbacks.entrySet().stream()
.collect(Collectors.toMap(e->e.getKey(),
e-> e.getValue().stream().max(Integer::compare).get()));
System.out.println(maxs); //{Nirmala=75, Subaksha=80}
You can try something like:
static <V extends Comparable<V>>
boolean valuesEquals(Map<?,V> map1, Map<?,V> map2) {
List<V> values1 = new ArrayList<V>(map1.values());
List<V> values2 = new ArrayList<V>(map2.values());
Collections.sort(values1);
Collections.sort(values2);
return values1.equals(values2);
}
or
map1.keySet().equals(map2.keySet())
to compare values. And if equals is true then you find greater Key of the two values.
Refer : http://thelogofthewook.blogspot.com/2011/12/hashmap-comparison-how-to-compare-two.html
You could create a helper Collection for each Teacher key (Map<String, Collection<Integer>>) that you fill up by iterating over both existing maps and then putting the scores into them.
Or you could create a Java 8 Stream over both map's entrySets and then merge them into one Map by calling the Collectors utils:
Map<String, Optional<Integer>> data = Arrays.asList( map1.entrySet(), map2.entrySet() )
.stream()
.flatMap( Set::stream )
.collect( Collectors.groupingBy( Entry::getKey, HashMap::new, Collectors.mapping( Entry::getValue, Collectors.maxBy( Integer::compare ) ) ) );
for( Entry<String, Optional<Integer>> entry : data.entrySet() )
{
System.out.println( entry.getKey() + ": " + entry.getValue().orElse( 0 ) );
}
If you are sure that you will get feedback for every teacher in every subject, you can use following.
Map<String, Integer> maths = new HashMap<String, Integer>();
maths.put("T1", 75);
maths.put("T2", 68);
maths.put("T3", 80);
Map<String, Integer> english = new HashMap<String, Integer>();
english.put("T1",60);
english.put("T2",70);
english.put("T3",79);
for(String s:maths.keySet())
System.out.println("Teacher "+s+" with max feedback:"+Math.max(maths.get(s),english.get(s)));
I need to compare for a single hashmap two by two successive values like in the example below:
key is: Nature ---- Value is: 2
key is: duck ---- Value is: 3
key is: sheep ---- Value is: 3
key is: WoodTable ---- Value is: 4
key is: PVCdoor ---- Value is: 4
What I'm asking for, is how can I compare :
the value of Nature with the value of duck
the value of duck of the value of sheep
the value of sheep with the value of woodTable
the value of woodTable with the value of PVCdoor
.... etc
I tried but I can't get the result I need. Please if you have any idea I need your help;
This is the function I use but the result isn't at all what I need as an output.
Thank you
public Map<String, Integer> setCoefffils(Map<String, Integer> map){
Map.Entry<String,Integer> entry=map.entrySet().iterator().next();
this.listCoeffConceptfilsfinal.put(entry.getKey(), coeffFils);
Set<Entry<String, Integer>> setHm = map.entrySet();
java.util.Iterator<Entry<String, Integer>> it = setHm.iterator();
Entry<String, Integer> e = it.next();
for( Entry<String, Integer> ee : setHm){
// Entry<String, Integer> eeee = it.next();
// for( Entry<String, Integer> eeee : setHm){
System.out.println("key current is: "+ee.getKey() + " ---- Value is: " + ee.getValue());
System.out.println("key following is: "+e.getKey() + " ---- Value is: " + e.getValue());
if(ee.getValue().equals(e.getValue()))
System.out.println(""+ee.getValue() + " et " + e.getValue()+" sont égaux ");
else
System.out.println(" ne sont pas égaux ");
// }
return this.listCoeffConceptfilsfinal;
}
One solution is to store all the keys in a list, then access them one after the other.
public static void foo(Map<String, Integer> map) {
Set<String> keySet = map.keySet();
String lastKey = null;
for (String key : keySet) {
if (null == lastKey) {
lastKey = key;
continue;
}
if (map.get(key).equals(map.get(lastKey))) {
System.out.println("Les valeurs associées aux clés " + lastKey + " et " + key + " sont égales.");
} else {
System.out.println("Les valeurs associées aux clés " + lastKey + " et " + key + " sont différentes.");
}
lastKey = key;
}
}
But be aware : Maps doesn't always guarantee that the keys stay in insertion order. Thus, your comparison could be false. If you want to conserve insertion order, you have to use a LinkedHashMap.
With key order
CODE:
import java.util.TreeMap;
import java.util.Map;
import java.util.Iterator;
/**
* Write a description of class sumAndMax here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class compareMap
{
public static void main(String[] args){
Map<String, Integer> map = initializeMap();
compareMaps(map);
}
private static void compareMaps(
Map<String, Integer> map)
{
Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
String leftKey = "";
String rightKey = "";
int leftValue = -1;
int rightValue = -1;
while (it.hasNext()) {
// Get values
Map.Entry<String, Integer> pair = it.next();
leftKey = rightKey;
leftValue = rightValue;
rightKey = pair.getKey();
rightValue = pair.getValue();
if(!leftKey.equals("")){
// Compare keys
System.out.println("Comparing key "+leftKey+" with key "+rightKey);
System.out.println("Result: "+leftKey.equals(rightKey));
// Compare values
System.out.println("Comparing value "+leftValue+" with value "+rightValue);
System.out.println("Result: "+(leftValue==rightValue));
}
}
}
private static Map<String, Integer> initializeMap(){
// Use Tree map for have a key ordered map !!!
Map<String, Integer> map = new TreeMap<>();
map.put("keyA",3);
map.put("keyB",4);
map.put("keyC",8);
map.put("keyD",8);
map.put("keyE",89);
map.put("keyF",4);
map.put("keyG",4);
return map;
}
}
RESULT:
Comparing key keyA with key keyB
Result: false
Comparing value 3 with value 4
Result: false
Comparing key keyB with key keyC
Result: false
Comparing value 4 with value 8
Result: false
Comparing key keyC with key keyD
Result: false
Comparing value 8 with value 8
Result: true
Comparing key keyD with key keyE
Result: false
Comparing value 8 with value 89
Result: false
Comparing key keyE with key keyF
Result: false
Comparing value 89 with value 4
Result: false
Comparing key keyF with key keyG
Result: false
Comparing value 4 with value 4
Result: true
I am trying to calculate the load factor of my Hashtable and print out the result. However, when I run this, the load factor is equal to 2. This is currently the size of my map - so my method loadFactor is not quite working. Is there something obvious I am missing? ( i am fairly new to Java)
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Main {
Student student;
Hashtable next;
public Hashtable<String,Student> studentMap;
public static void main(String[] args){
Hashtable<String, String> studentMap = new Hashtable<>(10000, 0.75f);
studentMap.keySet().forEach((key) -> {
String value = studentMap.get(key);
System.out.println("Key = " + key + ", Value = " + value);
});
//adding values to array
studentMap.put("16012804", "Jennifer");
studentMap.put("13747732", "Beatrice");
studentMap.put("14058392", "Bob");
Set set = studentMap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mapentry = (Map.Entry)iterator.next();
System.out.print("key is: "+ mapentry.getKey() + " & Value is: ");
System.out.println(mapentry.getValue());
}
//Get values based on key
String var= studentMap.get("16012804");
System.out.println("Value at index 1 is: "+var);
// Remove values based on key
studentMap.remove("16012804");
System.out.println("Map key and values after removal:");
Set set2 = studentMap.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry mapentry2 = (Map.Entry)iterator2.next();
System.out.print("Key is: "+mapentry2.getKey() + " & Value is: ");
System.out.println(mapentry2.getValue());
}
Set keyset = studentMap.keySet();
System.out.println("Key set values are:" + keyset);
boolean val = studentMap.isEmpty();
System.out.println("Is hash map empty: " + val);
System.out.println("Size of the Hashtable " + studentMap.size());
System.out.println("Load Factor: " + loadFactor(studentMap));
System.out.println("Hash: " + studentMap.hashCode());
System.out.println("Size of map is now: " + mapcapacity(studentMap));
}
public static float loadFactor(Map studentMap){
int count=0;
float load;
for(int i=0; i<studentMap.size(); i++){
count += studentMap.size();
}
load = count/(float)studentMap.size();
return load;
}
//if the size of the map is greater than the map capacity * load factor - then double the size of map.
public static Integer mapcapacity(Map studentMap){
Integer initCapacity=2;
float loadFactor=0.75f;
boolean capacityFound=false;
Integer capacity=initCapacity;
Integer size=studentMap.size();
while(!capacityFound){
if(size>capacity*loadFactor){
capacity=capacity*2;
}
else {
capacityFound=true;
}
}
return capacity;
}
public int hash(String key){
return (Math.abs(key.hashCode())) % studentMap.size();
}
}
I am creating a table from ajax and the getting of values using a while loop:
while (rstdb.next()) {
rstdb.getInt(1)+"~"+ rstdb.getInt(2);
}
In my while loop my rstdb.getInt(1) will be 2,2,2,2,2,3,3...... and second values rstdb.getInt(2) are 10,20,30,40,50,10,20,.....
I need to sum up the values specific to 2 and values specific to 3 seperate.
ie,
It means 10+20+30+40+50 =150 for 2 and 10+20 =30 for 3.
It may contain single values also for example it may have 4,5,5,6,7,7....
How can I do that?
I need something like:
while (rstdb.next()) {
rstdb.getInt(1)+"~"+ rstdb.getInt(2)+"~"+sum;
}
The variable sum should contain the sum up value.
Use map for this. You can have a map which should be mapping the specific number with sum of it's corresponding value.
int c1, c2;
Map<Integer, Integer> sum = new HashMap<>();
while (rstdb.next()) {
c1 = rstdb.getInt(1);
c2 = rstdb.getInt(2);
if(sum.containsKey(c1)) {
sum.put(c1, sum.get(c1) + c2);
// ^ will return current sum of second column
} else {
sum.put(c1, c2);
}
rstdb.getInt(1)+"~"+ rstdb.getInt(2)+"~"+sum.get(c1);
}
You can use an integer to integer map:
Map<Integer, Integer> integerMap = new HashMap<>();
while (rstdb.next()) {
int column1 = rstdb.getInt(1);
int column2 = rstdb.getInt(2);
if (integerMap.containsKey(column1)) {
int currentSum = integerMap.get(column1);
integerMap.put(column1, currentSum + column2);
} else {
integerMap.put(column1, column2);
}
}
Edit: to print out the map, you can use loop through the entrySet of the map:
for (Map.Entry entry : integerMap.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
I have a person class
public class Person {
private int id;
private int amount;
public Person(int id, int amount){
this.id = id;
this.amount = amount;
}
//getters
}
And I have a mainClass like this
Map<String, Person> mapOfObjects = new HashMap<String, Person>();
mapOfObjects.put("map1", new Person(1, 1000));
mapOfObjects.put("map2", new Person(1, 1000));
mapOfObjects.put("map3", new Person(1, 1000));
mapOfObjects.put("map4", new Person(2, 1000));
mapOfObjects.put("map5", new Person(2, 1000));
mapOfObjects.put("map6", new Person(3, 1000));
Map<Integer, Integer> mapOfSum = new HashMap<Integer, Integer>();
int sum = 0;
List list = new LinkedList(mapOfObjects.keySet());
for (int i = 0; i < mapOfObjects.size(); i++) {
for (int j = 1; j < mapOfObjects.size() - i; j++) {
if (mapOfObjects.get(list.get(i)).getId() ==
mapOfObjects.get(list.get(j)).getId()) {
sum += (mapOfObjects.get(list.get(i)).getAmount() +
mapOfObjects.get(list.get(j)).getAmount());
}
mapOfSum.put(mapOfObjects.get(list.get(i)).getId(), sum);
}
}
System.out.println(mapOfSum);
It gives me output:
{1=8000, 2=8000, 3=0}
but i want something like this
id=1 amount =3000, id =2 amount = 2000, id =3 amount =1000
How can i remove the object from the map whose summation is already done once while running the first for-loop
Your loop is overengineered.
Try this:
for (Person person : mapOfObjects.values()) //for every person in the map...
{
int newAmount = 0;
if (mapOfSum.containsKey(person.getId())) //if its already in the sumMap
{
newAmount = mapOfSum.get(person.getId()) + person.getAmount(); //update the value
}
else
{
newAmount = person.getAmount(); //else set it as starting value
}
mapOfSum.put(person.getId(), newAmount); //put it in the sum map. If it's already there it will be substituted by new entry.
}
Output:
{1=3000, 2=2000, 3=1000}
EDIT
If you want the output in different format you can do something like this:
StringBuilder stringBuilder = new StringBuilder("");
for (Map.Entry<Integer, Integer> entry : mapOfSum.entrySet())
{
stringBuilder.append("id=").append(entry.getKey()).append(" amount=").append(entry.getValue()).append(", ");
}
System.out.println(stringBuilder.toString());
Output:
id=1 amount=3000, id=2 amount=2000, id=3 amount=1000,
EDIT2:
As suggested in comments by #NickHolt there is more efficient way that shortens execution time by eliminating the containsKey() check.
It can be done like this:
Integer personAmount = mapOfSum.get(person.getId());
if (personAmount == null)
{
personAmount = person.getAmount();
}
else
{
personAmount = person.getAmount() + personAmount;
}
mapOfSum.put(person.getId(), personAmount);
or in shorter form using ternary operator:
Integer personAmmount = mapOfSum.get(person.getId());
personAmmount = personAmmount == null ? person.getAmount() : person.getAmount() + personAmmount;
mapOfSum.put(person.getId(), personAmount);
You can use Google Guava multimap
Multimap<Integer, Integer> PersonMap = ArrayListMultimap.create();
PersonMap.put(1, 1000);
PersonMap.put(1, 1000);
PersonMap.put(1, 1000);
PersonMap.put(2, 1000);
PersonMap.put(2, 1000);
PersonMap.put(3, 1000);
Then you can use something like:
Hashset<Integer, Integer> PersonDictionary = new Hashset<Integer, Integer>();
for (Integer key: PersonMap.keySet())
PersonDictionary.Add(key, Sum(PersonMap.get(key)));
Where sum a function : Is there possibility of sum of ArrayList without looping
Instead of using two for loops, use single, for traversing the value set of the map, and find the summation
Map<String, Person> mapOfObjects = new HashMap<String, Person>();
mapOfObjects.put("map1", new Person(1, 1000));
mapOfObjects.put("map2", new Person(1, 1000));
mapOfObjects.put("map3", new Person(1, 1000));
mapOfObjects.put("map4", new Person(2, 1000));
mapOfObjects.put("map5", new Person(2, 1000));
mapOfObjects.put("map6", new Person(3, 1000));
Map<Integer, Integer> mapOfSum = new HashMap<Integer, Integer>();
for (Person person : mapOfObjects.values()) {
Integer value= mapOfSum.get(person.getId());
if (value == null) {
value = 0;
}
value += person.getAmount();
mapOfSum.put(person.getId(), value);
}
System.out.println(mapOfSum);