Reputation: 493
For the data that I am inputting into my program, I require duplicates, values for each item of data and I require the iteration order of the data to by insertion order as a default.
This is the kind of data that I am working with:
y = m * x + c
y = 8, m = 3, x = 2, c = 2
What I have been using up until now is a linkedHashMap to store all my data, which satisfies all my criteria other than storing duplicates.
So if I had b + b = c I wouldn't be able to get my program to work.
Preferably I would like to continue using maps for the key and value property and the ability to store different kinds if data i.e. String, Integer. So I would prefer a way for a linkedhashmap to retain duplicates. If there isn't, please let me know if there is any other way I can meet all my requirements.
Regards
Upvotes: 2
Views: 6216
Reputation: 5103
Use Guava library Multimap with value type of Object. Check their documentation for more info.
Multimap map = ArrayListMultimap.create();
map.put(key, value1);
map.put(key, value2);
List values = map.get(key);
Upvotes: 1
Reputation: 308
You can use Map<Integer,List<Integer>>
. In this datatype , you can store multiple values for the same key.Your put method can be like this ...
public void put(Integer key,Integer val){
List<Integer> arr = map.get(key);
if(arr == null){
arr = new ArrayList<Integer>();
map.put(key,arr);
}
arr.add(val);
}
Upvotes: 1
Reputation: 12743
package com.shashi.mpoole;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.URISyntaxException;
public class LinearEquation {
public static void main(String[] args) throws URISyntaxException {
LinearEquation linearEquation = new LinearEquation();
InputStream in = LinearEquation.class.getResourceAsStream("../../../Inputs.txt");
// Inputs Taken
try {
// Reading Inputs
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
String line=null;
// Separated by Comma m=4,x=2,c=5
while((line=reader.readLine())!=null){
LinearEqInput lei = new LinearEqInput();
String[] inputs = line.split(",");
//Getting array [m=4,x=2,c=5]
for (int i = 0; i < inputs.length; i++) {
String[] keyval = inputs[i].split("=");
//Getting vallue [m,4]
linearEquation.setValue(lei, keyval[0], keyval[1]);
}
System.out.print("[Inputs: m= " + lei.m + " x= " + lei.x + " ,c= " + lei.c + "]");
//result
System.out.println(" Output :" + linearEquation.calculate(lei));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// This method use reflection to store the value of a field in the class LinearEqInput
public void setValue(LinearEqInput lei,String fieldName, String value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
{
Field f = lei.getClass().getDeclaredField(fieldName);
f.setDouble(lei, Double.parseDouble(value));
}
//Calculating the Y
public double calculate(LinearEqInput lei)
{
return lei.m*lei.x + lei.c;
}
}
class LinearEqInput
{
//Inputs
double m,x,c = 0;
}
Inputs File
m=4,x=3,c=1
m=4,x=4,c=6
m=4,x=5,c=5
m=4,x=7,c=4
m=4,x=2,c=3
m=4,x=5,c=2
Output:
[Inputs: m= 4.0 x= 3.0 ,c= 1.0] Output :13.0
[Inputs: m= 4.0 x= 4.0 ,c= 6.0] Output :22.0
[Inputs: m= 4.0 x= 5.0 ,c= 5.0] Output :25.0
[Inputs: m= 4.0 x= 7.0 ,c= 4.0] Output :32.0
[Inputs: m= 4.0 x= 2.0 ,c= 3.0] Output :11.0
[Inputs: m= 4.0 x= 5.0 ,c= 2.0] Output :22.0
Upvotes: 1