Conversion between java collections: efficient and complete

Keywords: Attribute

Catalog

1. Array --"List

2. List --"Array

3,List —> Set / Set —> List

4,List -> Map

5,Map  —> List / Set

6,List —> List / Set

1. Array --"List

To convert an array into a collection, the array must be a reference data type. If an array of basic data types is converted into a collection, the whole array will be converted as an object.

Arrays.asList() returns a fixed-size list supported by a specified array. So you can't do Add, Remove, etc.

private static void arrToList(){
        // Converting an array of basic data types into a collection takes the entire array as an object transformation
        // Output: [[I@3830f1c0]
        int[] arr = {1, 2, 3, 4, 5};
        List<int[]> list = Arrays.asList(arr);
        System.out.println(list);

        // To convert an array into a collection, the array must be a reference data type
        // Output: [1, 2, 3, 4, 5]
        Integer[] arr2 = {1, 2, 3, 4, 5};
        // Arrays.asList() returns a fixed-size list supported by a specified array. So you can't do Add, Remove, etc.
        List<Integer> list2 = Arrays.asList(arr2);
        
        // Just create a new List again.
        List<Integer> list3 = new ArrayList<>(list2);
        list3.add(7);
        System.out.println(list2);
        System.out.println(list3);
    }

2. List --"Array

 private static void arrToList(){
        // Arrays must be reference data types
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            list.add("1-"+i);
        }
        String[] arr = new String[list.size()];
        list.toArray(arr);
        System.out.println(Arrays.toString(arr));
    }

3,List —> Set / Set —> List

Converting a List set to a Set set can be directly de-duplicated.

 private static void listToSet(){
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            list.add("1-"+i);
        }
        // Direct a new object, just put the list in
        Set<String> set = new HashSet<>(list);
        System.out.println(set.toString());
    }

Similar to a List collection, a set collection can be transferred directly to a List object and placed into it.

 4,List -> Map

If our List stores an object, we want to convert it into a Map collection, with key as an attribute of the object. (Implemented using Stream streams)

Entity class:

/**
 * @author MouFangCai
 * @date 2019/8/2 10:41
 */
public class TestEntity {
    private Integer id;
    private String name;

    @Override
    public String toString() {
        return "TestEntity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
 private static void listToMap(){
        List<TestEntity> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            TestEntity testEntity = new TestEntity();
            testEntity.setId(i);
            testEntity.setName("name-"+i);
            list.add(testEntity);
        }
        System.out.println(list.toString());
        // Using stream stream stream to achieve high efficiency
        // Here the key of the map is the id of TestEntity
        Map<Integer, TestEntity> map = list.stream().collect
                (Collectors.toMap(TestEntity::getId, Function.identity(), (key1, key2) -> key2));
        System.out.println(map.toString());
    }

5,Map  —> List / Set

 private static void mapToList(){
        Map<Integer, TestEntity> map = new HashMap<>();
        for (int i = 0; i < 4; i++) {
            TestEntity testEntity = new TestEntity();
            testEntity.setId(i);
            testEntity.setName("name-"+i);
            map.put(i, testEntity);
        }
        // Get the set set set of map key s directly
        Set<Integer> set1 = map.keySet();
        // Converting map values to Set sets
        Set<TestEntity> set = new HashSet<>(map.values());
        // Converting map values to List collections
        List<TestEntity> list = new ArrayList<>(map.values());

        System.out.println(set.toString());
        System.out.println(list.toString());
    }

6,List —> List / Set

Requirement: List1 collection store is an object, and we want to get a List2 collection of an object's attributes directly from the collection (implemented using Stream stream)

 private static void listToList(){
        List<TestEntity> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            TestEntity testEntity = new TestEntity();
            testEntity.setId(i);
            testEntity.setName("name-"+i);
            list.add(testEntity);
        }
        // Get a list collection of name s
        List<String> list1 = list.stream().map(TestEntity::getName).collect(Collectors.toList());
        // Get the set set set set for name
        Set<String> set = list.stream().map(TestEntity::getName).collect(Collectors.toSet());
        System.out.println(list1.toString());
    }

 

Posted by htcilt on Sat, 12 Oct 2019 11:36:12 -0700