Java集合互转,集合转换

集合之间的互转

确认JDK是否支持如下集合的转换

Collectors

Objects

参考:

List< T>转Map<String, T>

PHP实现

$list = [
    ['name' => 'lisa', 'classRom' => 2, 'age' => 15],
    ['name' => 'tony', 'classRom' => 2, 'age' => 23],
    ['name' => 'maria', 'classRom' => 3, 'age' => 15],  
];

array_combine(array_column($list, 'name'), $list);

Java实现

Map<String, T> array_combine(() -> T::getXXX, List list);

// 列表转 Map,列表中对象的属性作为 key,对象作为 value
public <T, R> Map<R, T> listToMapV1(Function<? super T, ? extends R> mapper, List<T> list) {
    Map<R, T> map = new HashMap<>(list.size());
    for (T t : list) {
        map.put(mapper.apply(t), t);
    }
    return map;
}

List<Student> studentList = new ArrayList<>(3);
Student lisa = Student.builder().name("lisa").classRoom(2).age(15).build();
Student tony = Student.builder().name("tony").classRoom(2).age(23).build();
Student maria = Student.builder().name("maria").classRoom(3).age(15).build();
studentList.add(lisa);
studentList.add(tony);
studentList.add(maria);
//
System.out.println(listToMapV1(Student::getName, studentList));
{tony=Student(name=tony, classRoom=2, age=23), lisa=Student(name=lisa, classRoom=2, age=15), maria=Student(name=maria, classRoom=3, age=15)}

List< T>转Map<String, List< T>>

统计某个 key 的集合。

PHP实现


Java实现

// 按照列表中对象的属性,进行分组
public <T, R> Map<R, List<T>> groupByColumnV1(Function<? super T, ? extends R> mapper, List<T> list) {
    Map<R, List<T>> map = new HashMap<>(list.size());
    for (T t : list) {
        R apply = mapper.apply(t);
        List<T> groupList = map.get(apply);
        if (null == groupList) {
            groupList = new ArrayList<>(list.size());
            map.put(apply, groupList);
        }
        groupList.add(t);
    }
    return map;
}

public <T, R> Map<R, List<T>> groupByColumnV2(Function<? super T, ? extends R> mapper, List<T> list) {
    return list.stream().collect(Collectors.groupingBy(mapper));
}

//
System.out.println(groupByColumnV1(Student::getAge, studentList));
{15=[Student(name=lisa, classRoom=2, age=15), Student(name=maria, classRoom=3, age=15)], 23=[Student(name=tony, classRoom=2, age=23)]}

List< T>转List< String>

PHP实现

// 返回二维数组中的所有的名字组成的一维数组
array_column($list, 'name');

Java实现

类似 array_cloumn,获取集合对象中的某个属性的集合。

// 获取 list 中对象的某个属性的集合,类似 php 的 array_column( array $input, mixed $column_key, mixed $index_key = null) : array
public <T, R> List<R> columnToListV1(Function<? super T, ? extends R> mapper, List<T> list) {
    List<R> propertyList = new ArrayList<>(list.size());
    for (T t : list) {
        propertyList.add(mapper.apply(t));
    }
    return propertyList;
}

public <T, R> List<R> columnToListV2(Function<? super T, ? extends R> mapper, List<T> list) {
    return list.stream().map(mapper).collect(Collectors.toList());
}

//
System.out.println(columnToListV1(Student::getName, studentList));
[lisa, tony, maria]

获取泛型数组里的泛型的某个属性的 List

System.out.println(Arrays.stream(students).map(Student::getName).collect(Collectors.toList()));
[lisa, tony, maria]

List< T>转Map<String, String>

PHP实现

array_combin(array_column($list, 'name'), array_column($list, 'age'));

Java实现

// 列表转哈希表,第一个列表对象的属性作为 key,第二个列表对象的属性作为 value
public <T1, R1, T2, R2> Map<R1, R2> listToMapV1(Function<T1, R1> mapper1, List<T1> list1, Function<T2, R2> mapper2, List<T2> list2) {
    Map<R1, R2> map = new HashMap<>(list1.size());
    for (int i = 0; i < list1.size(); i++) {
        map.put(mapper1.apply(list1.get(i)), mapper2.apply(list2.get(i)));
    }
    return map;
}

//
System.out.println(listToMapV1(Student::getName, studentList, Student::getAge, studentList));
{tony=23, lisa=15, maria=15}

包装类型和集合互转

// 包装数据类型转集合
Integer[] integers = new Integer[]{1, 2};
List<Integer> integerList = Arrays.stream(integers).collect(Collectors.toList());
assertEquals(2, integerList.size());
assertEquals(1, (int) integerList.get(0));
assertEquals(2, (int) integerList.get(1));
// 基本数据类型数组转集合
int[] ints = new int[]{1, 2, 3};
List<Integer> integerList1 = Arrays.stream(ints).boxed().collect(Collectors.toList());
assertEquals(3, integerList1.size());
assertEquals(1, (int) integerList1.get(0));
assertEquals(3, (int) integerList1.get(2));
posted @ 2023-05-25 21:59  沙里  阅读(232)  评论(0)    收藏  举报