Java Collections Framework

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/9

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

10 Terms

1
New cards

Java Collections Framework

Provides 3 things: Interfaces (for abstract data types), Implementations (of the interfaces) and Algorithms for common operations. Everything inherits from Object.

2
New cards

Collection

A general interface for a simple group of objects you can loop over and change. Comes with the add() and remove() methods. You can use addAllto add multiple elements at once. or you can check for membership with contains(). as well as clear() and isEmpty();

3
New cards

Iterable

Anything you can use in an enhanced for loop. Every Collection is Iterable .

when we say “for (Element e: Collection)”

java uses:

Iterator<Element> it = Collection.iterator();
while (it.hasNext()) {
Element e = it.next();
// do something with e
}

4
New cards

List

An ordered Sequence of elements. Everything has an inndex, starts at 0. can use add() or remove() with a specific index, aswell as get() or set() at a specific index. You can also find an elements index using indexOf() (returns -1 if the object isnt' there)

5
New cards

ArrayLists

Made up of Arrays.we start with an array, and if the array gets full, it makes a bigger Array and moves everything to it.

Fast at accessing elements by index

Slow at adding/removing elements at the beginning/middle

6
New cards

LinkedLists

Each Element simply points to the next element. Very slow at accessing elements by index, but fast at adding and removing elements from the beginning or middle of the list. In a linked list, to add an element in the middle, simply create the element and the index it needs, tell the index that it should point to the new element, while the new element points to the next element in the sequence.

7
New cards

E

A type parameter in Java generic collections that can represent any object type.
<? extends E> means anything we use in our collection’s method must be E or a subclass of E

<? super E> means anything we use in this method must be E or a superclass of E.

8
New cards

Set

A collection that does NOT keep duplicate Elements.
Union: a.addAll(b) adds all of set b to set a
Intersection: a.retainAll(b) anything they had in common is kept
Difference: a.removeAll(b) remove anything belonging to set b from a
Contains: a.contains(e) checks if e is a member of a

Set is unordered

9
New cards

The get-put principle

We have our generic type of e.
<? Extends E> is for getting elements (can only get E or a subtype of E)

<? super E> when you only need to put Elementsinto a collection (can only put E or a supertype of E) which helps manage type safety in generics. if you’re using both, use just E. Say you have a collection of bananas. our banana class extends fruit

from the line Collection<? extends Fruit> we know our collection is of fruit objects, but we dont know if they are bananas, strabwerries or anything else. (we dont know if the collection is specifically Collection<banana> or if its Collection<strawberry>) Therefore if we go to get an item, we will know it will be a type of fruit (<? Extends Fruit>). We can’t just add any type of fruit because we might be adding the wrong type of fruit to a bunch of bananas

Collection<? super Banana>. We know we’re working with Bananas or a supertype of Banana, ensuring type safety when adding elements. But we wont be guaranteed it is a banana we are getting from this collection. (Our collection could be Collection<Fruit> which means it could be a mix of strawberries and bananas and trying to get one may not return a banana).

10
New cards