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
Related
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.
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 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 this following test code:
public static final String[] list = {
"apple","ball","cat","dog","egg","fan","girl","hat","igloo","jerk"
};
...
HashMap<DoubleKey<Integer, Integer>, String> hm = new HashMap<DoubleKey<Integer, Integer>, String>();
Set<DoubleKey<Integer, Integer>> s = new TreeSet<DoubleKey<Integer, Integer>>();
Random g = new Random();
for(int i=0; i<10; i++){
int first = g.nextInt(9999) + 1000;
int second = g.nextInt(9999) + 1000;
DoubleKey<Integer, Integer> k1 = new DoubleKey<Integer, Integer>(first, second);
DoubleKey<Integer, Integer> k2 = new DoubleKey<Integer, Integer>(first, second);
s.add(k1);
hm.put(k2, list[i]);
}
Set<DoubleKey<Integer, Integer>> ts = hm.keySet();
Iterator<DoubleKey<Integer, Integer>> itr = ts.iterator();
while(itr.hasNext()){
DoubleKey<Integer, Integer> k = itr.next();
System.out.println(k.getFirstKey().toString() + " + " + k.getSecondKey().toString() + " -> " + hm.get(k).toString());
}
System.out.println("----");
Iterator<DoubleKey<Integer, Integer>> sItr = s.iterator();
while(sItr.hasNext()){
DoubleKey<Integer, Integer> k = sItr.next();
String currStr = hm.get(k);
System.out.println(k.getFirstKey().toString() + " + " + k.getSecondKey().toString() + " -> " + currStr);
}
What I did is to create a Custom Generic Class DoubleKey<K, J> to contain a key having two parts. As you can see, the Set s and the keys of HashMap hm are have the same components, but was instantiated differently (k1 = k2). When I try to get a value using the keys on s to hm, it returns null, though at the first printing it shows the correct mapping.
Sample Output:
3922 + 2544 -> girl
9267 + 3750 -> hat
3107 + 10929 -> apple
5162 + 8834 -> fan
8786 + 1125 -> cat
10650 + 4078 -> egg
3808 + 7363 -> jerk
1364 + 7657 -> dog
1364 + 4412 -> ball
1583 + 1460 -> igloo
----
10650 + 4078 -> null
1364 + 4412 -> null
1364 + 7657 -> null
1583 + 1460 -> null
3107 + 10929 -> null
3808 + 7363 -> null
3922 + 2544 -> null
5162 + 8834 -> null
8786 + 1125 -> null
9267 + 3750 -> null
This is my DoubleKey implemention:
public class DoubleKey<K extends Comparable<K>,J extends Comparable<J>> implements Comparable<DoubleKey<K,J>>{
private K key1;
private J key2;
public DoubleKey(K key1, J key2){
this.key1 = key1;
this.key2 = key2;
}
public K getFirstKey(){
return this.key1;
}
public J getSecondKey(){
return this.key2;
}
// need for Comparable interface
public int compareTo(DoubleKey<K,J> aThat){
// NOTE: check for nulls
return (this.key1.toString() + this.key2.toString()).compareTo(aThat.key1.toString() + aThat.key2.toString());
}
public boolean equals(DoubleKey<K,J> aThat){
return (this.key1.toString() + this.key2.toString()).equals(aThat.key1.toString() + aThat.key2.toString());
}
}
How did it happened? Can two objecst (in this case from a custom generic) be different eve3n if they have instantiated with 2 same values? How can I correct this? I hope someone can help me here. Thanks!
Additionally to .hashCode(), you should have an implementation of equals(Object), not (only) equals(DoubleKey<...>), since otherwise you'll have two independent methods here (and only the first one is actually called by the HashMap). Here is a proposal:
public boolean equals(Object other) {
if(this == other)
return true;
if(!(other instanceof DoubleKey))
return false;
DoubleKey that = (DoubleKey)other;
return (this.key1 == null ? that.key1 == null : this.key1.equals(that.key1)) &&
(this.key2 == null ? that.key2 == null : this.key2.equals(that.key2));
}
The hashCode method should be made to fit this, too, for example like this:
public int hashCode() {
return key1.hashCode() * 3 + key2.hashCode() * 5;
}
Your key1.toString()+key2.toString() comparison is a bit dangerous, as it lets (1, 21).equals((12,1)) be true, which is usually not intended. The same is true for your compareTo method - compare the components using their compareTo method, not the concatenated String.
Learn this lesson now: If you override the equals method (as you have done), then you MUST override the hashcode method too. That method is used for various things, including looking up items in HashMaps.
Where is DoubleKey class's hashCode method override? I don't think that it will work as a proper key unless you implement this because otherwise your two objects will be considered different.