对于我输入到程序中的数据,我需要每个数据项的重复值和值,并且我要求数据的迭代顺序默认为插入顺序。
这是我正在使用的数据类型:
y = m * x + c
y = 8, m = 3, x = 2, c = 2
到目前为止,我一直使用 linkedHashMap 来存储我的所有数据,它满足我除了存储重复项之外的所有标准。
因此,如果我有 b + b = c,我将无法让我的程序运行。
最好我想继续使用键和值属性的映射以及存储不同类型数据的能力,即字符串、整数。所以我更喜欢一种让 linkedhashmap 保留重复项的方法。如果没有,请告诉我是否有其他方法可以满足我的所有要求。
问候
最佳答案
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;
}
输入文件
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
输出:
[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
关于java - linkedhashmap 的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14434791/