Java 对多个键值使用Hashmap,为什么?
我被困在这里: 我的输入文件:Java 对多个键值使用Hashmap,为什么?,java,map,collections,Java,Map,Collections,我被困在这里: 我的输入文件: 123 456 789 872 727 282 123 838 831 818 833 939 现在,我需要将数据保存在hashmap、2d数组或任何最好的备选方案中,如: key value 123 -> 456, 789, 838, 831 872 -> 727, 282 818 -> 833, 939 实现这一点的最佳方法(简单且优化)是什么?使用什么 我正在尝试maprawdata=newhashmap()但没有成功 我是jav
123 456 789
872 727 282
123 838 831
818 833 939
现在,我需要将数据保存在hashmap、2d数组或任何最好的备选方案中,如:
key value
123 -> 456, 789, 838, 831
872 -> 727, 282
818 -> 833, 939
实现这一点的最佳方法(简单且优化)是什么?使用什么
我正在尝试maprawdata=newhashmap()代码>但没有成功
我是java新手。:) Map data=newhashmap();
void addValue(字符串键、字符串值){
如果(!data.contains(键)){
data.put(key,newlinkedlist());
}
data.get(key)、add(value);
}
我不太确定java方法的确切名称,但主要应该是这样。每个HashMap键指向一个包含选项的链接列表。Map data=new HashMap();
void addValue(字符串键、字符串值){
如果(!data.contains(键)){
data.put(key,newlinkedlist());
}
data.get(key)、add(value);
}
我不太确定java方法的确切名称,但主要应该是这样。每个HashMap关键点指向一个包含您的选项的链接列表。现在要结合您的语法和ruibm的语法,以防您需要另一个透视图:
String key = "123";
String value = "456";
Map<String, ArrayList> rawData = new HashMap<String, ArrayList>();
if(!rawData.containsKey(key)){
rawData.put(key, new ArrayList());
}
rawData.get(key).add(value);
String key=“123”;
字符串值=“456”;
Map rawData=new HashMap();
如果(!rawData.containsKey(键)){
put(key,newarraylist());
}
获取(键)、添加(值);
现在结合您的语法和ruibm的语法,以防您需要另一个透视图:
String key = "123";
String value = "456";
Map<String, ArrayList> rawData = new HashMap<String, ArrayList>();
if(!rawData.containsKey(key)){
rawData.put(key, new ArrayList());
}
rawData.get(key).add(value);
String key=“123”;
字符串值=“456”;
Map rawData=new HashMap();
如果(!rawData.containsKey(键)){
put(key,newarraylist());
}
获取(键)、添加(值);
这里有一个相当严格的版本,要求:
- 每个键和每个值都是三位数
- 每行必须定义一个键和至少一个值
- 为同一个键重复的值不会合并到值列表中
- 每三次后允许有任意大小的空间
Matcher
的重复分配可以优化为使用Matcher#reset()
,但在清晰度上有所损失
private static <T extends Appendable> T collectLineInto(Reader source, T sink)
throws IOException
{
for (int read = source.read();
-1 != read && '\n' != read;
read = source.read())
{
sink.append((char)read);
}
return sink;
}
static Map<Integer, List<Integer>> read(Reader reader)
throws IOException
{
final Pattern head = Pattern.compile("(\\d{3}) +(\\d{3})(?: +|$)");
final Pattern tail = Pattern.compile("\\G(\\d{3})(?: +|$)");
final Map<Integer, List<Integer>> result =
new HashMap<Integer, List<Integer>>();
for (final StringBuilder buf = new StringBuilder(11);
0 != collectLineInto(reader, buf).length();
buf.setLength(0))
{
final Matcher m = head.matcher(buf);
if (!m.lookingAt())
throw new IOException("Encountered invalid entry");
final Integer key = new Integer(m.group(1));
List<Integer> values = result.get(key);
if (null == values)
{
values = new LinkedList<Integer>();
result.put(key, values);
}
values.add(Integer.parseInt(m.group(2)));
m.usePattern(tail);
while (!m.hitEnd())
{
if (m.find())
values.add(Integer.parseInt(m.group(1)));
else
throw new IOException("Encountered invalid triple");
}
}
return result;
}
static Map<Integer, List<Integer>> read(InputStream is, Charset cs)
throws IOException
{
return read(new InputStreamReader(is, cs));
}
专用静态T collectLineInto(读卡器源、T接收器)
抛出IOException
{
for(int read=source.read();
-1!=读取&&'\n'!=读取;
read=source.read())
{
sink.append((char)read);
}
回流槽;
}
静态地图读取(读卡器)
抛出IOException
{
final Pattern head=Pattern.compile(“(\\d{3})+”(\\d{3})(?:+)”;
final Pattern tail=Pattern.compile(\\G(\\d{3})(?:+|$)”;
最终地图结果=
新的HashMap();
对于(最终StringBuilder buf=新StringBuilder(11);
0!=collectLineInto(读取器,buf).length();
buf.设定长度(0))
{
最终匹配器m=头部匹配器(buf);
如果(!m.lookingAt())
抛出新IOException(“遇到无效条目”);
最终整数键=新整数(m.group(1));
列表值=result.get(键);
if(null==值)
{
值=新的LinkedList();
结果。输入(键、值);
}
add(Integer.parseInt(m.group(2));
m、 使用模式(尾部);
而(!m.hitEnd())
{
if(m.find())
add(Integer.parseInt(m.group(1));
其他的
抛出新IOException(“遇到无效三元组”);
}
}
返回结果;
}
静态映射读取(InputStream为,字符集cs)
抛出IOException
{
返回读取(新的InputStreamReader(is,cs));
}
这里有一个相当严格的版本,要求:
- 每个键和每个值都是三位数
- 每行必须定义一个键和至少一个值
- 为同一个键重复的值不会合并到值列表中
- 每三次后允许有任意大小的空间
Matcher
的重复分配可以优化为使用Matcher#reset()
,但在清晰度上有所损失
private static <T extends Appendable> T collectLineInto(Reader source, T sink)
throws IOException
{
for (int read = source.read();
-1 != read && '\n' != read;
read = source.read())
{
sink.append((char)read);
}
return sink;
}
static Map<Integer, List<Integer>> read(Reader reader)
throws IOException
{
final Pattern head = Pattern.compile("(\\d{3}) +(\\d{3})(?: +|$)");
final Pattern tail = Pattern.compile("\\G(\\d{3})(?: +|$)");
final Map<Integer, List<Integer>> result =
new HashMap<Integer, List<Integer>>();
for (final StringBuilder buf = new StringBuilder(11);
0 != collectLineInto(reader, buf).length();
buf.setLength(0))
{
final Matcher m = head.matcher(buf);
if (!m.lookingAt())
throw new IOException("Encountered invalid entry");
final Integer key = new Integer(m.group(1));
List<Integer> values = result.get(key);
if (null == values)
{
values = new LinkedList<Integer>();
result.put(key, values);
}
values.add(Integer.parseInt(m.group(2)));
m.usePattern(tail);
while (!m.hitEnd())
{
if (m.find())
values.add(Integer.parseInt(m.group(1)));
else
throw new IOException("Encountered invalid triple");
}
}
return result;
}
static Map<Integer, List<Integer>> read(InputStream is, Charset cs)
throws IOException
{
return read(new InputStreamReader(is, cs));
}
专用静态T collectLineInto(读卡器源、T接收器)
抛出IOException
{
for(int read=source.read();
-1!=读取&&'\n'!=读取;
read=source.read())
{
sink.append((char)read);
}
回流槽;
}
静态地图读取(读卡器)
抛出IOException
{
final Pattern head=Pattern.compile(“(\\d{3})+”(\\d{3})(?:+)”;
final Pattern tail=Pattern.compile(\\G(\\d{3})(?:+|$)”;
最终地图结果=
新的HashMap();
对于(最终StringBuilder buf=新StringBuilder(11);
0!=collectLineInto(读取器,buf).length();
buf.设定长度(0))
{
最终匹配器m=头部匹配器(buf);
如果(!m.lookingAt())
抛出新IOException(“遇到无效条目”);
最终整数键=新整数(m.group(1));
列表值=result.get(键);
if(null==值)
{
值=新的LinkedList();
结果。输入(键、值);
}
add(Integer.parseInt(m.group(2));
m、 使用模式(尾部);
而(!m.hitEnd())
{
if(m.find())
add(Integer.parseInt(m.group(1));
其他的
抛出新IOException(“遇到无效三元组”);
}
}
返回结果;
}
静态映射读取(InputStream为,字符集cs)
抛出IOException
{
返回读取(新的InputStreamReader(is,cs));
}
试试看
试用
“简单且优化”-不错的要求。可能是DEK在闲逛。所有的键/值都是数字吗?如果是,0123与123是否不同?此外,如果它们都是数字,它们可以有多大/多小?右侧三元组中的任何重复项是否需要合并?这个列表真的是一个有序的集合,还是它必须容忍重复的集合?“简单且优化”——很好的需求。可能是DEK在闲逛。所有的键/值都是数字吗?如果是,0123与123是否不同?此外,如果它们都是数字,它们可以有多大/多小?右侧三元组中的任何重复项是否需要合并?列表真的是有序集合,还是必须容忍重复?我会考虑使用集合。