Sort objects within a Set by String value containing all objects

Ok, this is complicated. I have a list of sets. I would like to sort the objects in sets in order.

Imagine each set as a repressive class at school. Each set contains human objects. The person object has a String value for the name. I would like to organize Persons in a set by name before I go over and write them.

Is it possible to use Collections.sort();or something similar to achieve this?

for (Set<Person> s : listOfAllChildren) {       
      for (Person p : s) {
        if(p.getClass().equalsIgnoreCase("Jones")){
          System.out.println(p.getName());
          }
         else if...//carry on through other classes 
        }                              
      }        

I know that 2+ children in a class can have the same name, but please ignore this

+3
source share
7 answers

A Set , , , .

SortedSet, TreeSet, . Comparator, Person Comparable.

+10

Comparable (Person ..).

:

:

import java.util.*;

class Person implements Comparable<Person> {
    private String firstName, lastName;

    public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName;}
    public String getFirstName() {return firstName;}
    public String getLastName() {return lastName;}
    public String getName() {return firstName + " " + lastName;}

    public int compareTo(Person p) {
        return lastName.compareTo(p.lastName);
    }
}

class FirstNameComparator implements Comparator<Person> {
    public int compare(Person p1, Person p2){
            return p1.getFirstName().compareTo(p2.getFirstName());
    }
}

class Test {
  public static void log(String s) {
        System.out.println(s);
    }

  public static void main(String[] args) {
        Set<Person> people = new HashSet<Person>();
        people.add(new Person("Bob", "Jones"));
        people.add(new Person("Alice", "Yetti"));

        log("Sorted list:");
        List<Person> peopleList = new LinkedList<Person>();
        peopleList.addAll(people);
        Collections.<Person>sort(peopleList);
        for (Person p : peopleList) {
            log(p.getName());
        }

        log("TreeSet:");
        TreeSet<Person> treeSet = new TreeSet<Person>();
        treeSet.addAll(people);
        for (Person p : treeSet) {
            log(p.getName());
        }

        log("TreeSet (custom sort):");
        TreeSet<Person> treeSet2 = new TreeSet<Person>(new FirstNameComparator());
        treeSet2.addAll(people);
        for (Person p : treeSet2) {
            log(p.getName());
        }
      }
};
+6

Java 8 Set List , .

List<Person> personList = personSet.stream().sorted((e1, e2) -> e1.getName().compareTo(e2.getName())).collect(Collectors.toList());
+3

TreeSet . TreeSet Person. Collection.sort, AFAIR .

+1

Person Comparable, , .

0

! Collection.sort(). (, TreeSet). , , Set to a List.

Person Comparable, Collections.sort(), , . - :

public class Person implements Comparable<Person> {
  ...
  @Override
  public int compareTo(Person p) {
    return this.name.compareTo(p.name);
  }
}

TreeSet, . , , Collections.sort( l) .

0

, SortedSet, , TreeSet. Comparator, Person.

0

Source: https://habr.com/ru/post/1773804/


All Articles