Collections

Things to remember about collections:

Deque interface ( implementations LinkedList and any Deque e.g. ArrayDeque )

my code see CollectionsPlay.deQueAsStack() , deQueAsQueue()

Stack methods of Deque:

  • push(E)
  • pop(E)
  • peek() // common to Stack and Queue (peek at head of stack or head of Queue ( both are the same ) )

 

HEAD of stack = top of stack (element at top of stack)

e.g. for this stack [8,6,4,2] 8 = last onto the stack (top) 8 = HEAD

 

HEAD of queue = the first element in the Queue

for this Queue (where 8 was added first into the Q ) [8,6,4,2]   8 = HEAD of Q (the 1st in the Q )

 

Queue methods (mostly all the others)

offer(E) – add to end of Queue

offerFirst(E) – add to front of Queue

offerLast(E) – add to end of Queue

 

Comparator

		String longer = "I am the longer String !!";
		String ickle = "I'm ickle";
		
		List<String> list = new ArrayList<>(); 
		list.add(0,longer); 
		list.add(1, ickle); 
				
		Comparator<String> byLength = (s1, s2) -> s1.length() - s2.length() ;
				
		// { longer, ickle }
		
		Collections.sort(list, byLength); 
		// after sort { ickle , longer }

Comparator called with ( longer , ickle )

longer.len – ickle.len = ( 23 – 7 = 16 )

a plus number so longer goes after in the ordering ( had it been a minus number it would go before).