简体   繁体   中英

sorting objects into an array of elements and an array with several elements

I have to sort a list of students, depending on the specialty they applied to.

My variabile for array in object is String [] specializations;

The objects I introduced look like this:

x.add (new Students ("Tudorache1", "Marinel", new String [] {"Programmer", "WEB"}, 12, 24, 2020));

x.add (new Students ("Tudorache7", "Marinel2", new String [] {"Operator", "WEB", "Developer"}, 12, 24, 2021));

x.add (new Students ("Tudorache3", "Marinel3", new String [] {"Constructor", "Accountant", "Secretary"}, 12, 24, 2018));

How can I sort by iteration to get:

WEB Tudorache1 Tudorache7

Programmer Tudorache1

Operator Tudorache7

Developer Tudorache7

Constructor Tudorache3

Accountant Tudorache3

Secretary Tudorache3

The given list of Students should be converted into a map of specializations to the list of the first field of Students class (let's assume this field is lastName ). Then the map may be converted into array of string arrays.

This can be resolved using Java Stream API:

public static Map<String, List<String>> mapSpecToNames(List<Students> students) {
    return students.stream()
            .flatMap(student -> Arrays.stream(student.getSpecializations())
                                      .map(spec -> Arrays.asList(spec, student.getLastName()))
            ) // Stream<List<String>>
            .collect(Collectors.groupingBy(
                pair -> pair.get(0), // specialization is a key
                LinkedHashMap::new,  // keep insertion order
                Collectors.mapping(
                    pair -> pair.get(1), Collectors.toList() // List<String> of names
                )
    ));
}

public String[][] mapToArray(List<Students> students) {
    return mapSpecToNames(students)
            .entrySet().stream()
            .map(e -> Stream.concat(
                    Stream.of(e.getKey()), 
                    e.getValue().stream()
                )
                .toArray(String[]::new)
            )
            .toArray(String[][]::new);
}

Test

List<Students> students = Arrays.asList(
    new Students ("Tudorache1", "Marinel", new String [] {"WEB", "Programmer"}, 12, 24, 2020),
    new Students ("Tudorache7", "Marine2", new String [] {"Operator", "WEB", "Developer"}, 12, 24, 2020),
    new Students ("Tudorache3", "Marine3", new String [] {"Constructor", "Accountant", "Secretary"}, 12, 24, 2020)
);

String[][] result = mapToArray(students);
        
Arrays.stream(result)
      .map(Arrays::toString)
      .forEach(System.out::println);

Output:

[WEB, Tudorache1, Tudorache7]
[Programmer, Tudorache1]
[Operator, Tudorache7]
[Developer, Tudorache7]
[Constructor, Tudorache3]
[Accountant, Tudorache3]
[Secretary, Tudorache3]

List of Students is not good data structure for such sorting. For example you can iterate thru specializations. Print specialization, then run thru x and print student's name if his specialization contain specialization. It is not optimal way but possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM