0

Need to sort array object based on other string array and remove unmatch object

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


public class Test {
    String name;
    int no;
    public String getName() {
        return name;
    }
    public int getNo() {
        return no;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setNo(int no) {
        this.no = no;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", no=" + no +
                '}';
    }

    public static Test createStudent(String name, int no) {
        Test student = new Test();
        student.setName(name);
        student.setNo(no);
        return student;
    }


    public static void sortStudentInArrayList(List<String> scppriority) {
        List<Test> students = new ArrayList<>();
        Test student1 = createStudent("SCP1", 3);
        students.add(student1);
        Test student2 = createStudent("SCP2", 1);
        students.add(student2);
        Test student3 =  createStudent("SCP3", 5);
        students.add(student3);
        Test student4 = createStudent("SCP4", 2);
        students.add(student4);
        
       
        //students.retainAll(scppriority);
        System.out.println("Original students list: " + students);
        
       
        Collections.sort(students, Comparator.comparing(s -> scppriority.indexOf(s.getName())));

    }

    public static void main(String[] args) {
        
        List<String> string2 = new ArrayList<String>();
        string2.add("SCP2");
        string2.add("SCP3");

               

        sortStudentInArrayList(string2);
}
}

<** output Original students list: [Student{name='SCP1', no=3}, Student{name='SCP4', no=2}, Student{name='SCP2', no=1}, Student{name='SCP3', no=5}] need to remove unmatch from student list, Thanks in Advance**>

4
  • Can you add the expected set of students in output?? Commented Jun 28, 2020 at 5:07
  • Thanks @SabareeshMuralidharan. the expected output should be Student{Student{name='SCP2', no=1}, Student{name='SCP3', no=5}] based on string2 defined order Commented Jun 28, 2020 at 5:19
  • And you don't want to use retainAll? Commented Jun 28, 2020 at 5:41
  • yes retainAll, I tried but got not able to success as I have object array and string array . any suggestion will be helpful Commented Jun 28, 2020 at 5:44

1 Answer 1

1

Try this,

List<Test> result = students.stream()
        .filter(student -> scppriority.contains(student.getName()))
        .sorted(Comparator.comparing(s -> scppriority.indexOf(s.getName())))
        .collect(Collectors.toList());
System.out.println("Original students list: " + result);

Output:

Original students list: [Student{name='SCP2', no=1}, Student{name='SCP3', no=5}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.