例如:
hashMap a = new hashMap();
a.put( "a", "1 "); 然后我再从新运行程序
a.put( "a", "2 ");然后我再从新运行程序
a.put( "a", "3 ");
程序我分3次运行put后能不能返回结果得到 :
a 1
a 2
a 3
如果map不能做到的话请问大家还有其它方法能实现我想要的结果吗??如果有请写出例子来谢谢!!
hashMap a = new hashMap();
a.put( "a", "1 "); 然后我再从新运行程序
a.put( "a", "2 ");然后我再从新运行程序
a.put( "a", "3 ");
程序我分3次运行put后能不能返回结果得到 :
a 1
a 2
a 3
如果map不能做到的话请问大家还有其它方法能实现我想要的结果吗??如果有请写出例子来谢谢!!
可以把map放到list中,变通达到你的要求
List list = new ArrayList();
hashMap a = new hashMap();
a.put( "a", "1 ");
list.add(a);然后我再从新运行程序
a = new HashMap();
a.put( "a", "2 ");
list.add(a);
然后我再从新运行程序
a = new HashMap();
a.put( "a", "3 ");
list.add(a);
可以反过来啊:hashMap a = new hashMap();
a.put( "1", "a");
a.put( "2", "a");
a.put( "3", "a");
HashMap a = new HashMap();
a.put("1", "a");
a.put("2", "a");
a.put("3", "a");
Iterator it = a.keySet().iterator();
while(it.hasNext()){
String key = (String) it.next();
System.out.println(a.get(key)+ " " + key);
}
import java.util.LinkedList;
import java.util.List;
import java.util.Map;public class Test { public static void main(String[] args) {
MultiValueHashMap<String, String> map = new MultiValueHashMap<String, String>();
String key = "a";
String key1 = "b";
map.put(key, "1");
map.put(key, "2");
map.put(key, "3");
map.put(key, "2");
map.put(key1, "4");
map.put(key1, "5");
map.put(key1, "6");
map.put(key1, "7");
String[] s = map.getAll(key, new String[0]);
System.out.println(s.length);
for(int i = 0, k = map.getSize(key); i < k; i++) {
System.out.println(key + " " + map.getValue(key, i));
}
System.out.println();
for(int i = 0, k = map.getSize(key1); i < k; i++) {
System.out.println(key1 + " " + map.getValue(key1, i));
}
}
}class MultiValueHashMap<K, V> extends HashMap<K, V> { private static final long serialVersionUID = 1L;
private Map<K, List<V>> map = new HashMap<K, List<V>>(); /**
* 这里仅仅重写了三个方法,应该需要重写所有的方法
*/
@Override
public V get(Object key) {
List<V> list = map.get(key);
return list.get(list.size()-1);
}
@Override
public V put(K key, V value) {
List<V> list = map.get(key);
if(list == null) {
list = new LinkedList<V>();
}
list.add(value);
map.put(key, list);
return null;
} @Override
public V remove(Object key) {
map.remove(key);
return null;
}
/**
* 移除键中所有的相同的值
* @param key
* @param value
* @return 被移除的值
*/
public V removeAllValue(K key, V value) {
List<V> list = map.get(key);
if(list == null) {
return null;
}
int index = 0;
for(int i = 0, k = list.size(); i < k; i++) {
V element = list.get(i);
if(element.equals(value)) {
index++;
}
}
for(int i = 0; i < index; i++) {
list.remove(value);
}
return value;
} /**
* 获得键所有值的数组
* @param key
* @param v
* @return
*/
@SuppressWarnings("unchecked")
public V[] getAll(K key, V[] v) {
return map.get(key).toArray(v);
}
/**
* 根据存入的索引,取出键的值
* @param key
* @param index
* @return
* @author Gao Baowen
*/
public V getValue(K key, int index) {
int size = getSize(key);
if(index <0 || index >= size) {
throw new IllegalArgumentException("index is too many, the max index is " + (size - 1) + ".");
}
return map.get(key).get(index);
}
/**
* 获得键中有多少值
* @param key
* @return
* @author Gao Baowen
*/
public int getSize(K key) {
return map.get(key).size();
}
}
a.put("a", "1");
a.put("a", "2");
a.put("a", "3");
这样是可以满足返回结果:
a 1
a 2
a 3 可是我想要的方法是单独运行程序可以保存前三次的值,之后返回结果
a 1
a 2
a 3
a 2
a 3
-------------------
你这样返回有什么意义呢?是返回三组字符串,还是一个字符串?