Lecture10
Lecture10
List! Set
! SortedSet!
ArrayList
LinkedList! HashSet
TreeSet!
Collection<E>
Has methods which include:
!boolean add(E e)!
!boolean remove(Object o)!
!boolean contains(Object o)!
!int size()
Doesn’t fully define how they work
Set<E>
• Defines s.add(e) to work as:
– If s.contains(e) was false, it becomes true,
method returns true, s.size() increased by 1
– If s.contains(e) was true, it remains true,
method returns false, s.size() unchanged
• Defines s.remove(e) to work as:
– If s.contains(e) was true, it becomes false,
method returns true, s.size() decreased by 1
– If s.contains(e) was false, it remains false,
method returns false, s.size() unchanged
• This is mathematical set behaviour
List<E>
• Adds methods which work on items being in a
position in the collection:
– E get(i)
– E set(i,e) (returns replaced element)
– int indexOf(e)
• Defines ls.add(e) to work by adding e to end
of list, returns true
• Defines ls.remove(e) to work by removing
lowest indexed occurrence of e, returns false if
no occurrence of e, true otherwise
Overloaded List<E> methods
Overloading is when there are two methods of the
same name in a class or interface, List<E> has two
examples:
• ls.add(i,e) adds e at position i in list, following
elements move up one position (different from
list.add(e) as that has one argument, this has
two)
• ls.remove(i) removes the element at position i
in list, following elements down one position
(different from the general remove method only by
argument type, int, rather than Object)
Copy constructors
Collection classes do not have a constructor which
works as constructors for new arrays do, that is by
creating a collection of a particular size. But they do
have constructors which take another collection
object as their argument and create a new collection
containing that collection’s elements
This orders Strings by length rather than alphabetically
Scrabble score Comparator
class Scrabble implements Comparator<String>!
{!
public static final int[] scores =
{1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};!
!
public static int score(String str) {!
String str1 = str.toUpperCase();!
int score = 0;!
for(int i=0; i<str1.length(); i++)!
score+=scores[str1.charAt(i)-'A'];!
return score;!
}!
!
public int compare(String str1,String str2) {!
return score(str1)-score(str2);!
}!
}
Finding the biggest using a Comparator
public static <T> T !
biggest(Collection<T> coll, Comparator<? super T> comp)!
{!
Iterator<T> it = coll.iterator();!
T biggest = it.next();!
while(it.hasNext())!
{!
T nextItem = it.next();!
if(comp.compare(nextItem,biggest)>0)!
biggest = nextItem;!
}!
return biggest;!
}!
!
You can use this to find the biggest item in a collection according
to the ordering given by the Comparator argument
• If we have ArrayList<String> words
– biggest(words,new LengthComparer())
returns the longest string from words
– biggest(words,new Scrabble())!
returns the string with the highest Scrabble score!
• But rather than write our own biggest, we can use Java’s
method max in class Collections
– Collections.max(words,new LengthComparer())
returns the longest string from words!
— Collections.max(words,new Scrabble())!
! returns the string with the highest Scrabble score
• Comparator<? super T> allows us to use e.g. a
Comparator<Fruit> to find the biggest from a
Basket<Banana>
Sorting using a Comparator
• We have already seen Java’s built-in methods which sort by
“natural order”, so if ls is of type List<E> then the
method call Collections.sort(ls) will sort the list to
which ls refers. The sorting is done “in place”.
• This means the elements of ls must have their own
compareTo method, so E should be declared as
<E extends Comparable<? super E>>!
• As an alternative, Collections.sort(ls,comp) will
sort the list to which ls refers by the order given by a
comparator referred to by comp.
• With this, E can be any type, so does not need a bound, and
comp should be of type Comparator<? super E>!
Anonymous Classes
• Comparator<String> c = new Comparator<String>(){!
! !public int compare(String str1,String str2)!
{!
return str1.length()-str2.length();!
}!
};!
is an alternative to