You are on page 1of 20

`

There are 5 legacy classes in java:


Vector Stack Properties Hashtable Dictionary

One legacy interface:


Enumeration

//creates a default vector, which has an initial size of 10. Vector(int size) // creates a vector whose initial capacity is specified by size. Vector(int size, int incr) //creates a vector whose intial capacity is specified by size and increment is specified by incr. Vector(Collection c) //creates a vector that contains the elements of collection c. Vector()

addElement(Object o) //add an element to vector elementAt(index i) // firstElement() lastElement() indexOf( Object o) removeElementAt(index i) capacity() size() elements() contains(Object o)

` `

A stack class represents a collection of objects that are in LIFO(last in first out order). The stack provides operations that allow testing for zero elements, inspection of its top most element, removal of its top most element, and the addition of elements. Methods:
Boolean empty() //tests if stack is empty Object peek() //looks at the object at the top of this stack without removing it from stack. Object pop() //Removes the object at top and returns that object as value of this function. Object push(Object o) //pushes an item onto the top of this stack. int search(Object o) // returns the 1-based position where an object is on this stack.

` ` ` ` `

Dictionary class is an abstract class, which maps keys to values. Can store the value in Dictionary object, as a key value pair. Once the value is stored, you can retrieve it by using its key. Dictionary is classified as obsolete, because it is superseded by Map in Collection and framework. Methods:
put(Object key, Object value) get(Object key) keys() //return enumeration of keys elements() //return enumeration of values size() isEmpty() remove(Object key)

A hashtable is conceptually a contiguous section of memory with a number of addressable elements, in which data can be quickly inserted, deleted and found. Hashtable represent a sacrifice of memory for the sake of speed. They are certainly not most memory efficient means storing data, but they provide very fast lookup times. Constructor:
Hashtable() //default constructor Hastable(int size) //has an initial space specified by size Hashtable(Map m) //iniatilize with element and size of Map.

Methods:
put(String key, Object o) get(String key) keys()

` `

Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key is a string and value is also a string. Constructors
Properties(); //creates properties object that has no default values. Properties(Properties propdefault)// creates object that uses default properties.

Methods:
Object setProperty(String key, String value) String getProperty(String key) String getProperty(String key, String defaultval) //default
property will be returned if no value is associated with certain key

Enumeration propertyNames()//return keys Void load(InputStream in) Void store(OutputStream out, String header)

To create and put values in a properties table


Properties props=new Properties(); Props.setProperty(recursiveSearch, true); Props.setProperty(noCopyPattern, *.$$$); Props.setProperty(maxLevel, 7); Props.setProperty(fileName, c:\temp\work.html);

To store a properties table in a file


OutputStream propOut=new FileOutputStream( new File(props.doc) Props.strore(propOut, macro processor properties);

To load properties from a file


props.load(propIn); boolean recursiveSearch =Boolean.getBoolean(props.getProperty(recusiveSearch));

String noCopyPattern=props.getProperty(noCopyPattern)); int maxLevel=Integer.parseInt(props.getProperty(maxLevel);

Java 1.5 introduces a new class named java.util.Formatter that allows to do string formatting similar to printf function in C. format(String format, Object..args)

Regular expression:
My name is Will and I live in williamstown.

how could you find occurrence of will, regardless the case of w. ` With regular expression we candescribe the requirement by composing a pattern made from series of matchers and literals. [Ww]ill Above regular expression will actually match 2 occurrences of will We may only wanted to search for will.

\b[Ww]ill\b

\b: describes word boundary.which can be space, tabs, beginning and end points of line.

(\w+)@(\w+\.)(\w+)(\.\w+)* \w+ : looks for word characters, as denoted by \w. the + indicates one or more word characters must appear. And it must followed by the literal @ character. billy@ joe@ francisford@ \w+\. : \is a regex matcher(so will be skipped) looks for wildcard character literals like * . _; billy@webworld. joe@optus.

(\w+)@(\w+\.)(\w+)(\.\w+)* \w+ : billy@webworld.com joe@optus.net (\.\w+)* : looks for the period followed by one or more word characters, * denotes that preceding matcher, literal can occur zero or more times. fred@vianet.com barney@comcorp.net.au wilma@mjnteractive.iinet.net.au

Pattern class ` This class let you compile your regular epression.
String emailRegEx=(\\w+)@(\\W+\\.)(\\w+)(\\.\\w+)*; Pattern pattern=Pattern.compile(emailRegEx); Variales: CASE_INSENSITIVE : tells regex engine to match Ascii characters regardless of case. MULTILINE : you will sometimes want to tell the regex engine that your target string is not a single line of code.
Pattern.compile(myRegEx,Pattern.CASE_INSENSITIVE|Pattern.MULTILINE)

Matcher:

if you have created pattern object, you will use it to create a Matcher. This is done by calling the matcher() factory method defined by pattern Matcher matcher(CharSequence str) boolean matches() To determine if sequence of input sequence matches the pattern

use find() each call to find() begins where previous one left off. group() //obtain a string containing last matches, start() //return the index of current match.

End() // return last index of current match String targetString=you can email me at g_andy@example.net to get more more info Matcher match = pattern.matcher(targetString): While(matcher.find()) {
System.out.println(found a match:+matcher.group()); System.out.println(start position:+matcher.start()); System.out.println(end position:+matcher.end());

Found match: g_andy@example.com Start position: 20 End position:38 Found match: andy@example.net Start position: 42 End position:58 found

You might also like