0

I'm trying finish my method 'priority' which should return the priority of the Task. and make it return null if the specified name does not exist (as shown in the main). I've tried iterating through the ArrayList, but i don't think this is the way to do it. Is there anyone who can help me out?

class Task
{
    public static final ArrayList<Task> ALL_TASKS = new ArrayList<>();

    private String name;
    private Integer priority;

    public Task(String name, Integer priority)
    {
        this.name = name;
        this.priority = priority;
        ALL_TASKS.add(this);
    }

    @Override public String toString()
    {
        return "name: " + this.name + "\tpriority: " + this.priority;
    }
}

class Main
{
    public static void main(String[] arguments)
    {
        new Task("Clean", 5);
        new Task("Dishwash", 4);
        new Task("Study", 1);
        System.out.println(Task.priority("Dishwash"));
        System.out.println(Task.priority("Vacuumclean"));
    }
}
3
  • Why did you remove a part of the code that is essential to understand the main? Commented Feb 25, 2020 at 16:39
  • Duplicated? See stackoverflow.com/questions/17526608/… Commented Feb 25, 2020 at 17:22
  • You should not return null here, Optional would be better choice Commented Feb 25, 2020 at 19:43

4 Answers 4

0
public static Integer priority(String name) {
        for(int i = 0; i < ALL_TASKS.size(); i++){
          if(ALL_TASKS.get(i).getName().equals(name))
            return ALL_TASKS.get(i).getPriority();
          else
            return null; // but if using Java 8 Option<Task> would be better
        }
    }

This is the example which I would use. It is not tested but it give you idea what create and the way of thinking.

Also in my solution I assume you also create getName() and getPriority() methods for your Task

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

public static Integer priority(String name) {
    for(Task task : ALL_TASKS) {
        if(task.name.equals(name)) {
            return task.priority;
        }
    }
    return null;
}

Or using Stream-API:

public static Integer priority(String name) {
    return ALL_TASKS.stream()
            .filter(task -> task.name.equals(name))
            .map(task -> task.priority)
            .findFirst()
            .orElse(null);
}

2 Comments

that will not work, cause name for Task is private
@noname the method is a member in the Task class, so private doesn't matter.
0

The Java Stream API provides a more convenient and efficient approach to iterating and processing elements of a collection.

class Task
{
    public static final ArrayList<Task> ALL_TASKS = new ArrayList<>();

    public static Integer priority(String name)
    {
        List<Task> result = ALL_TASKS.stream().filter(task-> 
                          (task.name.equals(name))).collect(Collectors.toList());
        if(result.isEmpty())
        {
            return null;
        }
        else
        {
            return result.get(0).priority;
        }
    }

    private String name;
    private Integer priority;

    public Task(String name, Integer priority)
    {
        ALL_TASKS.add(this);
        this.name = name;
        this.priority = priority;
    }

    public String toString()
    {
        return "name: " + this.name + "\tpriority: " + this.priority;
    }
}

class Main
{
    public static void main(String[] arguments)
    {
        new Task("Clean", 5);
        new Task("Dishwash", 4);
        new Task("Study", 1);
        System.out.println(Task.priority("Dishwash"));
        System.out.println(Task.priority("Vacuumclean"));
    }

}

Comments

0

An option would be storing your List as a Map instead, using the tasks name as the key

HashMap<String, Task> ALL_TASKS = new HashMap<>();
Task task = ALL_TASKS.get(codeIsIn);
if(task!=null)
{
    Integer priority = task.priority;
}

If a Task consists only of name and priority, it gets even simpler

HashMap<String, Integer> ALL_TASKS = new HashMap<>();
Integer priority = ALL_TASKS.get(codeIsIn);
//null if nonexistent

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.