List<ExamSubjects> unique = strings.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ExamSubjects::getId))), ArrayList::new) );