Conversion between List and Set and Map

When looking at the source code of collections, I found an interesting thing: the transformation between collections in Java

First, take a look at the source code of ArrayList Collection, and find that ArrayList has a default parameter of Collection construction method.

 /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        if ((size = elementData.length) != 0) {
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // replace with empty array.
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }

Then lo ok at other assembly sets. It is found that there are parameter constructions similar to this one. Therefore, the transformation between assemblies directly adds the assembly as a parameter when creating objects.
For example: HashSet to ArrayList:

public static void main(String[] args) {

        HashSet<Object> set = new HashSet<>();
        set.add("123");
        
        ArrayList<Object> list = new ArrayList<>(set);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).toString());
        }

    }

In addition to this, there is another method addAll. Check the source code:

/**
     * Appends all of the elements in the specified collection to the end of
     * this list, in the order that they are returned by the
     * specified collection's Iterator.  The behavior of this operation is
     * undefined if the specified collection is modified while the operation
     * is in progress.  (This implies that the behavior of this call is
     * undefined if the specified collection is this list, and this
     * list is nonempty.)
     *
     * @param c collection containing elements to be added to this list
     * @return <tt>true</tt> if this list changed as a result of the call
     * @throws NullPointerException if the specified collection is null
     */
    public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }

Code:

public static void main(String[] args) {

        HashSet<Object> set = new HashSet<>();

        set.add("123");

        ArrayList<Object> list = new ArrayList<>();
        list.addAll(set);  //This is the trip.

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).toString());
        }

    }

Recently, the value of HashMap encountered in the project has been converted to ArrayList, and the following code is pasted by the way:

 private void set(){
        Map map = new HashMap();
        map.put("aa","11");
        map.put("bb","22");
        map.put("cc","33");

        ArrayList list = new ArrayList();
        list.add(map.values());    //This is to add the value value of the map to a location in the list
        list.addAll(map.values()); //This is to add the value value of map to multiple locations in the list.
    }

As for why map.values() can be added to the list, look at the type of map.values() method:

    Collection<V> values();

Tags: Java

Posted on Fri, 01 Nov 2019 21:33:23 -0700 by nitch