Title: Collections The Power of Bulk Operations
1CollectionsThe Power of Bulk Operations
2Short Overview
public interface CollectionltEgt extends
IterableltEgt The root interface in the collection
hierarchy. A collection represents a group of
objects, known as its elements. Some collections
allow duplicate elements and others do not. Some
are ordered and others unordered. The JDK does
not provide any direct implementations of this
interface.
public interface SetltEgt extends CollectionltEgt A
collection that contains no duplicate elements.
More formally, sets contain no pair of elements
e1 and e2 such that e1.equals(e2), and at most
one null element. As implied by its name, this
interface models the mathematical set abstraction.
3Short Overview
public abstract class AbstractCollectionltEgt extend
s Object implements CollectionltEgt This class
provides a skeletal implementation of the
Collection interface, to minimize the effort
required to implement this interface.
public interface MapltK,Vgt An object that maps
keys to values. A map cannot contain duplicate
keys each key can map to at most one value.
4Set Basic Operations
boolean addAll(Collectionlt? extends Egt c) Adds
all of the elements in the specified collection
to this set if they're not already present
(optional operation).
boolean containsAll(Collectionlt?gt c) Returns
true if this set contains all of the elements of
the specified collection.
boolean removeAll(Collectionlt?gt c) Removes from
this set all of its elements that are contained
in the specified collection (optional operation).
boolean retainAll(Collectionlt?gt c) Retains only
the elements in this set that are contained in
the specified collection (optional operation).
Object toArray() Returns an array
containing all of the elements in this set. ltTgt
T toArray(T a) Returns an array
containing all of the elements in this set the
runtime type of the returned array is that of the
specified array.
5Required Fields
Optional Fields
6Text Input
- Suppose that a server receives the form as a text
file - The task is to verify that
- All required fields have been filled
- All the fields are valid
- There are no forbidden values
name haim email haim_at_net password
abcabc ICQ-number 123 home-page
haim.com hobbies basketball language hebrew
Required Fields (attributes)
Optional Fields (attributes)
7The required Interface
If return values are false, functions print to
the screen the non-valid Attributes\Values
8Using The Class Form
First we define the required attributes, optional
attributes and forbidden values
public class TestForm private static final
String _requiredAttrs "name",
"email", "password" private static
final String _optionalAttrs
"ICQ-number", "home-page", "hobbies",
"language" private static final String
_forbiddenValues "Pini-Gershon",
"Shimon-Mizrahi", "Eli-Ohana"
9TestForm - cont
public static void main(String args) try
boolean valid Form form new
Form(_requiredAttrs, _optionalAttrs)
form.readFile(args0) valid
form.validateRequiredAttributes() // Note
the order of the following expression
valid form.validatePermitedAttributes()
valid valid
form.validateAttributeValues(_forbiddenValues)va
lid if (valid)
System.out.println("Form is valid") else
System.out.println("Form is not
valid") catch (IOException e)
System.err.println("Error while reading "
args0)
10Class Form - Constructor
public class Form SetltStringgt
_requiredAttrs SetltStringgt _permitedAttrs
MapltString, Stringgt _attrMap int
_numAttr public Form(String
requiredAttrs, String optionalAttrs)
_requiredAttrs new HashSetltStringgt(
Arrays.asList(requiredAttrs))
_permitedAttrs new HashSetltStringgt(
Arrays.asList(optionalAttrs)) //
permitedAttrs is the union of required and
optional _permitedAttrs.addAll(_requiredAt
trs) _numAttr _requiredAttrs.size()
_permitedAttrs.size()
11Parsing
public void readFile(String fileName)
throws IOException String line, attr,
value Scanner lineScanner _attrMap
new HashMapltString, Stringgt(_numAttr)
BufferedReader inputReader new
BufferedReader(new FileReader(fileName))
while ((line inputReader.readLine()) ! null)
lineScanner new Scanner(line)
if (lineScanner.hasNext()) attr
lineScanner.next() if
(lineScanner.hasNext()) value
lineScanner.next()
_attrMap.put(attr, value)
12Required Attributes The Hard Way
public boolean validateRequiredAttributes()
IteratorltStringgt i // Init MapltString,
Booleangt filledRequiredAttr new
HashMapltString, Booleangt() i
_requiredAttrs.iterator() while(i.hasNext())
filledRequiredAttr.put(i.next(),Boolean.FA
LSE) // Iterate over the attributes that
has been filled // and update the map of the
required attributes SetltStringgt userAttrs
_attrMap.keySet() Boolean isFilled
String key int numFilled 0 i
userAttrs.iterator() while(i.hasNext())
key i.next() isFilled
filledRequiredAttr.get(key) if (isFilled
! null isFilled Boolean.FALSE)
filledRequiredAttr.put(key, Boolean.TRUE)
numFilled numFilled1
13The Hard Way - cont
// Check if all required fields has been filled,
if // not print those that were not. i
_requiredAttrs.iterator() if(numFilled lt
_requiredAttrs.size())
System.out.print("Missing attributes ")
while (i.hasNext()) key
i.next() isFilled
filledRequiredAttr.get(key) if
(isFilled ! null isFilled Boolean.FALSE)
System.out.print( key " " )
System.out.println("")
return false else return
true
14Checking Required Attributes
public boolean validateRequiredAttributes()
SetltStringgt attrs _attrMap.keySet()
if(!attrs.containsAll(_requiredAttrs))
// note the 'new' command SetltStringgt
missing new
HashSetltStringgt(_requiredAttrs)
missing.removeAll(attrs)
System.out.println ("Missing
attributes " missing) return false
else return true
15Checking Permitted Attributes
public boolean validatePermitedAttributes()
SetltStringgt attrs _attrMap.keySet() if
(!_permitedAttrs.containsAll(attrs))
SetltStringgt illegal
new HashSetltStringgt(attrs)
illegal.removeAll(_permitedAttrs)
System.out.println ("Illegal
attributes " illegal) return false
else return true
16Validating Field Values
public boolean validateAttributeValues
(String forbidden) SetltStringgt
forbiddenSet new HashSetltStringgt(Arrays.a
sList(forbidden)) SetltStringgt commonValues
new HashSetltStringgt(_attrMap.values())
commonValues.retainAll(forbiddenSet) if
(commonValues.size()gt0)
System.out.println ("Illegal
values " commonValues) return false
else return true
17Summary of Bulk Operations
- Transforming an array to a collection
Arrays.asList - Set operations
- Union, intersection and set-minus using addAll,
retainAll, and removeAll - Contains relation using containsAll
- Map Collection Views
- keySet , values and entrySet
- More info and more operations
- java API documentation
- Java Collection Tutorialhttp//java.sun.com/docs/
books/tutorial/collections/index.html