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.List;
- import java.util.Random;
- public class SomeClass {
- public static void main(final String[] args) {
- final FooContainer foos = new FooContainer();
- final Random rng = new Random();
- // This just adds random elements:
- for (int i = 0; i < 100; i++)
- foos.add(new Foo(Month.of(rng.nextInt(12) + 1).name(), rng.nextInt(3) + 1));
- final List<Foo> elements = foos.blub();
- 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> blub() {
- final List<Foo> result = new ArrayList<>();
- for (int i = Foo.TYPE_A; i <= Foo.TYPE_C; i++)
- for (final Foo f : this.list)
- if (i == f.getType())
- result.add(f);
- return result;
- }
- }
- public static class Foo {
- public static final int TYPE_A = 1;
- public static final int TYPE_B = 2;
- public static final int TYPE_C = 3;
- private int type;
- private String name;
- public Foo(final String name, final int type) {
- this.name = name;
- this.type = type;
- }
- public String getName() {
- return this.name;
- }
- public void setName(final String name) {
- this.name = name;
- }
- public int getType() {
- return this.type;
- }
- public void setType(final int type) {
- this.type = type;
- }
- }
- }
Advertisement