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);
} }}

解决方案 »

  1.   


            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());
            }
      

  2.   

    HashSet 源码,HashSet 是基于HashMap 实现的private transient HashMap<E,Object> map;    // Dummy value to associate with an Object in the backing Map
        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];
        }
      

  3.   

    HashSet 源码,HashSet 是基于HashMap 实现的private transient HashMap<E,Object> map;    // Dummy value to associate with an Object in the backing Map
        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];
        }
    请问你这些源码在哪里看到的,介绍下,谢谢
      

  4.   

    HashSet 源码,HashSet 是基于HashMap 实现的private transient HashMap<E,Object> map;    // Dummy value to associate with an Object in the backing Map
        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点你想看的地方
      

  5.   

    这句是什么意思?Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
      

  6.   

    获取map中的entry集合,map中的数据都是在entry中存放的,entry包含key 和 value 
    通过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的接口定义
      

  7.   

    这句是什么意思?Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
    获取map的两种方式:
    1、KeySet 得到key 然后在get出来Value
    2、另一个就是map.entrySet()
    得到的是一个存有Enty对象的Set集合,可以迭代该集合得到一个个的Enty对象,然后这个Enty对象里面存有一个map对象的key  和value  可直接获取这两个东西
      

  8.   

    这句是什么意思?Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
    获取map的两种方式:
    1、KeySet 得到key 然后在get出来Value
    2、另一个就是map.entrySet()
    得到的是一个存有Enty对象的Set集合,可以迭代该集合得到一个个的Enty对象,然后这个Enty对象里面存有一个map对象的key  和value  可直接获取这两个东西
    这个就是C#里面的Dictionary