1 / 17

Collections The Power of Bulk Operations

Collections The Power of Bulk Operations. OOP Tirgul 9 2006. Short Overview. public interface Collection<E> extends Iterable <E>

amandla
Download Presentation

Collections The Power of Bulk Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CollectionsThe Power of Bulk Operations OOP Tirgul 9 2006

  2. Short Overview public interface Collection<E> extends Iterable<E> 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 Set<E> extends Collection<E> 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.

  3. Short Overview public abstract class AbstractCollection<E> extends Object implements Collection<E> This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface. public interface Map<K,V> An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

  4. Set Basic Operations  boolean addAll(Collection<? extends E> c) Adds all of the elements in the specified collection to this set if they're not already present (optional operation).  boolean containsAll(Collection<?> c) Returns true if this set contains all of the elements of the specified collection.  boolean removeAll(Collection<?> c) Removes from this set all of its elements that are contained in the specified collection (optional operation). boolean retainAll(Collection<?> 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. <T> 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.

  5. Required Fields Optional Fields

  6. Text 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@net password abcabc ICQ-number 123 home-page haim.com hobbies basketball language hebrew Required Fields (attributes) Optional Fields (attributes)

  7. The required Interface If return values are false, functions print to the screen the non-valid Attributes\Values

  8. Using 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" };

  9. TestForm - cont public static void main(String[] args) { try { boolean valid; Form form = new Form(_requiredAttrs, _optionalAttrs); form.readFile(args[0]); valid = form.validateRequiredAttributes(); // Note the order of the following expression valid = form.validatePermitedAttributes() && valid; valid = form.validateAttributeValues(_forbiddenValues)&&valid; 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 " + args[0]); } }

  10. Class Form - Constructor public class Form { Set<String> _requiredAttrs; Set<String> _permitedAttrs; Map<String, String> _attrMap; int _numAttr; public Form(String[] requiredAttrs, String[] optionalAttrs) { _requiredAttrs = new HashSet<String>( Arrays.asList(requiredAttrs)); _permitedAttrs = new HashSet<String>( Arrays.asList(optionalAttrs)); // permitedAttrs is the union of required and optional _permitedAttrs.addAll(_requiredAttrs); _numAttr = _requiredAttrs.size()+ _permitedAttrs.size(); }

  11. Parsing public void readFile(String fileName) throws IOException { String line, attr, value; Scanner lineScanner; _attrMap = new HashMap<String, String>(_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); } } } }

  12. Required Attributes – The Hard Way public boolean validateRequiredAttributes() { Iterator<String> i; // Init Map<String, Boolean> filledRequiredAttr = new HashMap<String, Boolean>(); i = _requiredAttrs.iterator(); while(i.hasNext()) filledRequiredAttr.put(i.next(),Boolean.FALSE); // Iterate over the attributes that has been filled // and update the map of the required attributes Set<String> 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 = numFilled+1; } }

  13. The Hard Way - cont // Check if all required fields has been filled, if // not print those that were not. i = _requiredAttrs.iterator(); if(numFilled < _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; }

  14. This is done because we do not want to Use views in the removeAll operation Checking Required Attributes public boolean validateRequiredAttributes() { Set<String> attrs = _attrMap.keySet(); if(!attrs.containsAll(_requiredAttrs)) { // note the 'new' command Set<String> missing = new HashSet<String>(_requiredAttrs); missing.removeAll(attrs); System.out.println ("Missing attributes: " + missing); return false; } else return true; }

  15. Checking Permitted Attributes public boolean validatePermitedAttributes() { Set<String> attrs = _attrMap.keySet(); if (!_permitedAttrs.containsAll(attrs)) { Set<String> illegal = new HashSet<String>(attrs); illegal.removeAll(_permitedAttrs); System.out.println("Illegal attributes: " + illegal); return false; } else return true; }

  16. Validating Field Values public boolean validateAttributeValues (String[] forbidden) { Set<String> forbiddenSet = new HashSet<String>(Arrays.asList(forbidden)); Set<String> commonValues = new HashSet<String>(_attrMap.values()); commonValues.retainAll(forbiddenSet); if (commonValues.size()>0) { System.out.println ("Illegal values: " + commonValues); return false; } else return true; }

  17. Summary 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

More Related