140 likes | 250 Views
Regular EXpressions & Hash-Tables and Hash-Maps. Regular Expressions (DEFINITION). Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation. Regular Expressions (cont..). Mainly used in Pattern Matching with strings
E N D
Regular Expressions (DEFINITION) • Sequence of characters • Generalized form • Expresses Pattern of strings in a Generalized notation
Regular Expressions (cont..) • Mainly used in • Pattern Matching with strings • In Theoretical Computer Science to represent Atomatons and Regular Languages • Can be implemented using Java and many other languages. • Eg: “boolean b = Pattern.matches("a*b", "aaaaab");” in Java. • Eg: “regex(?n, “Chicago”, “i”)” in SPARQL.
Regular Expressions (meaning) • a*b => aaaaa……….b OR b [1 or 0 a’s followed by 1 b] • a+b => aaaa……….b OR ab [at least 1 a followed by 1 b] • (a|b) => Either a OR b. • (a|b)*bb => aaaa…….bb OR bbb………bb
Implementation in JAVA Important APIs and Interfaces • java.util.regex • java.util.regex.Pattern • Serializable [Interface]
EXAMPLE CODE public final class Pattern extends Object implements Serializable _____________________________________________________________________________ Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches(); ~ boolean b = Pattern.matches("a*b", "aaaaab");
Example code (cont..) Here's an example, MatcherDemo.java, that counts the number of times the word "dog" appears in the input string. import java.util.regex.Pattern; import java.util.regex.Matcher; public class MatcherDemo { private static final String REGEX = "\\bdog\\b"; private static final String INPUT = "dog dogdog doggie dogg"; public static void main(String[] args) { Pattern p = Pattern.compile(REGEX); // get a matcher object Matcher m = p.matcher(INPUT); int count = 0; while(m.find()) { count++; System.out.println("Match number " + count); System.out.println("start(): " + m.start()); System.out.println("end(): " + m.end()); } }
Hash Table • Hashtablewas part of the original java.util and is a concrete implementation of a Dictionary. • Hashtablestores key/value pairs in a hash table. • When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table. • Hash Table Class
Hash Table constructors • Hashtable( ) //default constructor • Hashtable(intsize) //creates a hash table that has an initial size specified by size • Hashtable(intsize, float fillRatio) //creates a hash table that has an initial size specified by size and a fill ratio specified by fillRatio. • Hashtable(Map m) //creates a hash table that is initialized with the elements in m.
Important methods • booleancontains(Object value) //Returns true if some value equal to value exists within the hash table. Returns false if the value isn't found. • booleancontainsKey(Object key) //Returns true if some key equal to key exists within the hash table. Returns false if the key isn't found. • booleancontainsValue(Object value) //Returns true if some value equal to value exists within the hash table. Returns false if the value isn't found. • Enumeration elements( ) //Returns an enumeration of the values contained in the hash table. • Object get(Object key) //Returns the object that contains the value associated with key. If key is not in the hash table, a null object is returned. • booleanisEmpty( ) //Returns true if the hash table is empty; returns false if it contains at least one key. • Enumeration keys( ) //Returns an enumeration of the keys contained in the hash table. • Object put(Object key, Object value) //Inserts a key and a value into the hash table. Returns null if key isn't already in the hash table; returns the previous value associated with key if key is already in the hash table. • void rehash( ) //Increases the size of the hash table and rehashes all of its keys. • Object remove(Object key) //Removes key and its value. Returns the value associated with key. If key is not in the hash table, a null object is returned.
HASH MAP • HashMap class uses a hashtable to implement the Map interface. • This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets.
Constructors • HashMap( ) //default hash map • HashMap(Map m) //initializes the hash map by using the elements of m • HashMap(intcapacity) //initializes the capacity of the hash map to capacity • HashMap(intcapacity, float fillRatio) //initializes both the capacity and fill ratio of the hash map by using its arguments
Important methods • booleancontainsKey(Object key) //Returns true if this map contains a mapping for the specified key. • booleancontainsValue(Object value) //Returns true if this map maps one or more keys to the specified value. • Set entrySet() //Returns a collection view of the mappings contained in this map. • Object get(Object key) //Returns the value to which the specified key is mapped in this identity hash map, or null if the map contains no mapping for this key. • booleanisEmpty() //Returns true if this map contains no key-value mappings. Basically same as Hash Table………………..
Sources • http://docs.oracle.com/javase/tutorial/essential/regex/matcher.html • http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html • http://www.tutorialspoint.com/java/java_hashtable_class.htm