import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;public class Test { public static void main(String[] args) {
int[] aa={1,2,3,4,5,6,7,8,9};
Set set=new HashSet();
for(int a:aa){
set.add(a);
}
// 把set放入到List中
List n=new ArrayList();
n.addAll(set);
Map map=new HashMap();
Iterator it=n.iterator();
while(it.hasNext()){
map.put(UUID.randomUUID(), it.next());
}
// 输出结果
Iterator keys=map.keySet().iterator();
while(keys.hasNext()){
Object key=keys.next();
Object value=map.get(key);
System.out.println(key+":"+value);
} }}
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;public class Test { public static void main(String[] args) {
int[] aa={1,2,3,4,5,6,7,8,9};
Set set=new HashSet();
for(int a:aa){
set.add(a);
}
// 把set放入到List中
List n=new ArrayList();
n.addAll(set);
Map map=new HashMap();
Iterator it=n.iterator();
while(it.hasNext()){
map.put(UUID.randomUUID(), it.next());
}
// 输出结果
Iterator keys=map.keySet().iterator();
while(keys.hasNext()){
Object key=keys.next();
Object value=map.get(key);
System.out.println(key+":"+value);
} }}
解决方案 »
- mysql插入图片文件问题
- 【求助】weblogic各版本之间的差异
- 关于java值传递方面的疑问,请高人分析一下内存中的变化,为什么2个结果的值不同
- 十万火急!!用C/C++写了个动态链接库加密库,用JNI调用,LoadLibrary报 " undefined symbol: _ZNSt8ios_base4InitC1Ev"异常 ??
- 两个Action之间如何通过HTTP进行通信?
- 做了个浏览器,但不能使用,大家给我看看。
- 轻松拿分!(关于TAB不起作用的问题)
- 我很笨的!!不过请你们耐心点,给我说说。。。
- 请问哪为为可以给我些有关scjp1.4中ASSERTIONS和GARBAGE COLLECTION的教程和习题!
- 关于applet,求救!!! 在线等!!!
- 关于Process中的getOutputStream
- Java网络编程udp接收端收不到数据的问题
int[] a={1,2,3,4,5,6,7,8,9};
Set<Integer> set = new HashSet<Integer>();
for (int atemp : a) {
set.add(atemp);
}
List<Integer> list = new ArrayList<Integer>();
list.addAll(set);
Map<String, Integer> map = new HashMap<String, Integer>();
for (Integer num : list) {
map.put(UUID.randomUUID().toString(), num);
}
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for (Map.Entry<String, Integer> entry : entrySet) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
private static final Object PRESENT = new Object(); /**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<>();
}ArrayList 源码,ArrayList 是基于数组实现的private transient Object[] elementData; /**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size; /**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
private static final Object PRESENT = new Object(); /**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<>();
}ArrayList 源码,ArrayList 是基于数组实现的private transient Object[] elementData; /**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size; /**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
请问你这些源码在哪里看到的,介绍下,谢谢
private static final Object PRESENT = new Object(); /**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<>();
}ArrayList 源码,ArrayList 是基于数组实现的private transient Object[] elementData; /**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size; /**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
请问你这些源码在哪里看到的,介绍下,谢谢
按着Ctrl点你想看的地方
通过key 去获取value的方法 public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key); return null == entry ? null : entry.getValue();
}也是先获取entry,然后再从entry中获取getValue的,所以直接遍历entry集合,能快一些
可以看看 Map.Entry 的api介绍,或直接看map的接口定义
获取map的两种方式:
1、KeySet 得到key 然后在get出来Value
2、另一个就是map.entrySet()
得到的是一个存有Enty对象的Set集合,可以迭代该集合得到一个个的Enty对象,然后这个Enty对象里面存有一个map对象的key 和value 可直接获取这两个东西
获取map的两种方式:
1、KeySet 得到key 然后在get出来Value
2、另一个就是map.entrySet()
得到的是一个存有Enty对象的Set集合,可以迭代该集合得到一个个的Enty对象,然后这个Enty对象里面存有一个map对象的key 和value 可直接获取这两个东西
这个就是C#里面的Dictionary