Java 如何将函数插入目录/哈希表
我用c#编写了这本词典,我需要用Java重写它Java 如何将函数插入目录/哈希表,java,c#,Java,C#,我用c#编写了这本词典,我需要用Java重写它 public static IReadOnlyDictionary<Type, Func<object, byte[]>> FromTypeDict = new Dictionary<Type, Func<object, byte[]>> { { typeof(byte), input => new byte[] { (byte)input } }, {
public static IReadOnlyDictionary<Type, Func<object, byte[]>> FromTypeDict = new Dictionary<Type, Func<object, byte[]>> {
{ typeof(byte), input => new byte[] { (byte)input } },
{ typeof(float), input => new byte[] { (byte)(((float)input) * 10) } },
{ typeof(ushort), input => IntToByte((ushort)input, 2) },
{ typeof(uint), input => Enumerable.Range(0, 4).Select(i => (byte)((((uint)input) >> (i * 8)) & 0xFF)).ToArray() },
{ typeof(string), input => (input as string)?.Select(c => (byte)c).ToArray() },
{ typeof(PatientSetting), input => new byte[] { (byte)(PatientSetting)input } },
{ typeof(MacAddressCollection), input => {
var coll = input as MacAddressCollection;
return new byte[] { (byte)coll.Count }.Concat(coll.SelectMany(i => (byte[])i)).ToArray();
} }
};
public static IReadOnlyDictionary FromTypeDict=new Dictionary{
{typeof(byte),input=>新字节[]{(byte)input},
{typeof(float),input=>新字节[]{(byte)((float)input)*10},
{typeof(ushort),input=>IntToByte((ushort)input,2)},
{typeof(uint),input=>Enumerable.Range(0,4)。选择(i=>(byte)(((uint)input)>>(i*8))&0xFF)).ToArray(),
{typeof(string),input=>(作为字符串输入)?。选择(c=>(byte)c.ToArray(),
{typeof(PatientSetting),input=>新字节[]{(byte)(PatientSetting)input},
{typeof(MacAddressCollection),输入=>{
var coll=作为MacAddressCollection的输入;
返回新字节[]{(字节)coll.Count}.Concat(coll.SelectMany(i=>(字节[])i)).ToArray();
} }
};
我创建一个哈希表
public static Hashtable<Type, Function<Object, Byte>> my_dict = new Hashtable<Type, Function<Object, Byte>>();
publicstatichashtable my_dict=newhashtable();
但我不知道如何在其中插入相同的功能
感谢您的帮助这或多或少是您要求的翻译
public class FunctionHashtable {
public static Hashtable<Class<?>, Function<Object, Byte>> my_dict = new Hashtable<>();
static {
// Duplicate below line as many times as needed
my_dict.put(Float.class, (object) -> Byte.valueOf(object.toString())); // Replace Float.class with the class (type in c#), replace the lambada with you custom logic
}
}
公共类函数哈希表{
公共静态哈希表
public class FunctionMap {
public static Map<Class<?>, Function<Object, byte[]>> myDict = new Hashtable<>();
static {
// Duplicate below line as many times as needed
myDict.put(Float.class, (object) -> object.toString().getBytes()); // Replace Float.class with the class (type in c#), replace the lambada with you custom logic
}
}