Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.foo;
- import java.time.Month;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Random;
- import java.util.stream.Collectors;
- import com.example.foo.SomeClass.Foo.Type;
- public class SomeClass {
- public static void main(final String[] args) {
- final FooContainer foos = new FooContainer();
- final Random rng = new Random();
- final Type[] types = Foo.Type.values();
- // This just adds random elements:
- for (int i = 0; i < 100; i++)
- foos.add(new Foo(Month.of(rng.nextInt(12) + 1).name(), types[rng.nextInt(types.length)]));
- final List<Foo> elements = foos.sortedByType();
- for (final Foo foo : elements)
- System.out.println(foo.getName());
- }
- public static class FooContainer {
- private final List<Foo> list = new ArrayList<>();
- public void add(final Foo foo) {
- this.list.add(foo);
- }
- public List<Foo> sortedByType() {
- return this.list.stream().sorted(Comparator.comparing(Foo::getType))
- .collect(Collectors.toList());
- }
- }
- public static class Foo {
- public static enum Type {
- A, B, C;
- }
- private Type type;
- private String name;
- public Foo(final String name, final Type type) {
- this.name = name;
- this.type = type;
- }
- public String getName() {
- return this.name;
- }
- public void setName(final String name) {
- this.name = name;
- }
- public Type getType() {
- return this.type;
- }
- public void setType(final Type type) {
- this.type = type;
- }
- }
- }
Advertisement