Sort numeric line spacing in java

I have a Person class with some Person, and there are details, like the name, age group.
The time range is {"0-5", "6-10", "11-30", "31-45", "46-50", "50-100", "100-110"};

I have a Person class with name, ageBandString spacing, and it parameterizes the constructor, getters, seters.

class Person {
    String name;
    String ageBand; //say it is string "0-50" which i pass in constructor while creating a person.
    //getters
    //setters
}

class TestAgeBand {
    public static void main(String args[]) {
        ArrayList<Person> person = new ArrayList<Person>();

        Person p1 = new Person("Mike1", "0-5");   
        Person p2 = new Person("Mike2", "6-10");
        Person p3 = new Person("Mike3", "11-30");   
        Person p4 = new Person("Mike4", "31-45");   
        Person p5 = new Person("Mike5", "50-100");   
        Person p6 = new Person("Mike6", "46-50"); 
        Person p7 = new Person("Mike7", "100-110");

        person.add(p1);
        //adding all persons to list.
    }
}

Here is what I do with my code to sort the interval. I need to sort people according to increasing intervals. I use Treemap to sort intervals.

Map<String, Person> ageBandMap = new TreeMap<String, Person>(){
    for(Person p: person) {
        ageBandMap.put(p.ageBand, p.name);
    }
}

When I print an interval key set, I get

Output:

[0-5, 100-110, 11-30, 31-45, 46-50, 50-100, 6-10]

What I do not need. I need intervals sorted as follows:

[0-5, 6-10, 11-30, 31-45, 46-50, 50-100, 100-110]

+4
5

ageBand Integer, .

person.stream().sorted(Comparator.comparing(element -> Integer.parseInt(element.getAgeBand().split("-")[0])))
            .collect(Collectors.toList());

Java 8, Collections.sort().

 Collections.sort(person, new Comparator<Person>() {
        @Override
        public int compare(Person o1, Person o2) {
            return Integer.parseInt(o1.getAgeBand().split("-")[0]) - Integer.parseInt(o2.getAgeBand().split("-")[0]);
        }
    });
+3

TreeMap, . , , , Person, ( ) . , :

public class Person {
    String name;
    Integer lower;
    Integer upper;
}

SortedSet<Person> set =
    new TreeSet<Person>(new Comparator<Person>()
    {
        public int compare(Person p1, Person p2) {
            if (p1 == null && p2 == null) return 0;
            if (p1 == null) return -1;
            if (p2 == null) return 1;
            return p1.getLower().compareTo(p2.getLower());
        }
    });
+2

, , LinkedHashMap - .

0

:

interface Interval extends Comparable {

     int left();
     int right();
}

public final class IntervalImpl implements Interval {

     private final int left;
     private final int right;


     public IntervalImpl(int left, int right) {
          this.left = left;
          this.right = right;
     }

     public IntervalImpl(String interval) {
          this.left = Integer.parseInt(interval.split("-")[0]);
          this.right = Integer.parseInt(interval.split("-")[1]);
     }

     @Override
     public int left() { return left; }

     @Override 
     int right() { return right; }

     @Override
     int compareTo(Interval other) {
         return left.compareTo(other.left());
     }

 }

:

 public final class Person {
     private final Interval interval;
     private final String name;

     public Person (String name, String interval) {
          this.name = name;
          this.interval = new Interval(interval);
     }

     public Interval getInterval() {
          return interval;
     }
 }

:

Map<Interval, Person> map = new TreeMap<>();

Enum. Interval.

0

, :

    Map<String,Person> ageBandMap = new LinkedHashMap<>();
    Map<Integer, Person> ageBandIntMap = new TreeMap<>();

    for(Person p: person)
        ageBandIntMap.put(Integer.parseInt(p.ageBand.split("-")[0]), p);

    for(Entry<Integer, Person> entry : ageBandIntMap.entrySet())
        ageBandMap.put(entry.getValue().ageBand, entry.getValue());

    for(Entry<String, Person> entry : ageBandMap.entrySet())
        System.out.format("\nageBand : %7s\t Person name : %S", entry.getKey(), entry.getValue().name);
0
source

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


All Articles