> For the complete documentation index, see [llms.txt](https://cs61b-2.gitbook.io/cs61b-textbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cs61b-2.gitbook.io/cs61b-textbook/12.-inheritance-iv-iterators-object-methods/12.6-exercises.md).

# 12.6 Exercises

### Factual

1. What methods are required for a class that is Iterable?
2. Which of the following is true about the `java.util.Se`t and the `java.util.List` interfaces?
   * [ ] If we add `String[][]` objects to a `Set` and a `List`, the size of the set will always be less than or equal to the size of the list.
   * [ ] The `java.util.ArrayList` class is an implementation of the `java.util.List` interface.
   * [ ] The `Set` and `List` interfaces extend the `Iterator` interface.
   * [ ] The `Set` and `List` interfaces extend the `Iterable` interface.
3. Suppose we have a class that implements `Iterator`. What methods must it override in order to compile?

<details>

<summary>Problem 1</summary>

An `Iterable` is required to have the `iterator()` method, which returns an `Iterator`.

</details>

<details>

<summary>Problem 2</summary>

* **If we add `String[][]` objects to a `Set` and a `List`, the size of the set will always be less than or equal to the size of the list.** Sets only have unique items, while lists can have duplicates, so if we add the same elements to both the list will always have at least as many elements as the set.
* **The `java.util.ArrayList` class is an implementation of the `java.util.List` interface.** One implementation of the `List` interface in Java is the ArrayList class.
* **The `Set` and `List` interfaces extend the `Iterable` interface.** Sets and Lists in Java can be used in enhanced for loops, which means that they are `Iterable`.

</details>

<details>

<summary>Problem 3</summary>

An `Iterator` must override `hasNext()`, which returns a boolean indicating whether there are more elements in the `Iterator`, and `next()`, which returns the next item.

</details>

### Conceptual

1. Why do we want to override the `.equals` method?

<details>

<summary>Problem 1</summary>

The `.equals()` method inherited from `Object` only checks if two items have the same memory address. This is undesireable behavior for many user-written classes in Java.

</details>

## Metacognitive

1. In lecture, you built the `ArraySetIterator` class. Modify the lecture class to take in a `Comparator<T>` and an item of generic type `T` called `ref` in the constructor. This new iterator should only return items greater than `T`. For reference, the code for `ArraySetIterator` is included below.

```java
private class ArraySetIterator implements Iterator<T> {
	private int pos;
	
	public ArraySetIterator() { 
		pos = 0; 
	}
	
	public boolean hasNext() { 
		return pos < size; 
	}
	
	public T next() {
	 	T returnItem = items[pos];
	 	pos += 1;
	 	return returnItem;
	}
}
```

2. Problem 7 from the Spring 2018 Midterm 2

<details>

<summary>Problem 1</summary>

```java
public class ArraySetGreaterIterator implements Iterator<T> {
    private int pos;
    private T ref;
        
    private Comparator<T> comp;

    public ArraySetGreaterIterator(T ref, Comparator<T> comp) {
        this.ref = ref;
        this.comp = comp;
    }

    @Override
    public boolean hasNext() {
        return pos < size;
    }

    @Override
    public T next() {
        T returnItem = items[pos];
        while (comp.compare(returnItem, ref) <= 0) {
            pos += 1;
            returnItem = items[pos];
        }
        return returnItem;
    }
}
```

</details>

<details>

<summary>Problem 2</summary>

[Solutions](https://drive.google.com/file/d/1LIyFXwHYCWXNqIgKTsTyKiOYnB79_ykk/view) and [walkthrough](https://www.youtube.com/watch?v=nMZn4EV0gGw) are linked here and on the course website.

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cs61b-2.gitbook.io/cs61b-textbook/12.-inheritance-iv-iterators-object-methods/12.6-exercises.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
