http://www.csis.pace.edu/~bergin/patterns/ppoop.html
Sunday, January 23, 2011
Sunday, January 16, 2011
ceilingKey
1- public K ceilingKey(K key) {}
2-return key .
3- Gets the entry corresponding to the specified key;
4-if no such entry exists, returns the entry for the least key greater than the specified key;
5-if no such entry exists (i.e., the greatest key in the Tree is less than the specified key), returns null
Example:
package com.example;
import java.util.TreeMap;
public class Test2 {
public static void main(String[] args) {
TreeMap treeExample=new TreeMap();
treeExample.put(1, "AAA");
treeExample.put(2, "BBB");
treeExample.put(4, "DDD");
System.out.println(treeExample.ceilingKey(1));
System.out.println(treeExample.ceilingKey(3));
System.out.println(treeExample.ceilingKey(6));
}
}
Out Put:
1
4
null.
2-return key .
3- Gets the entry corresponding to the specified key;
4-if no such entry exists, returns the entry for the least key greater than the specified key;
5-if no such entry exists (i.e., the greatest key in the Tree is less than the specified key), returns null
Example:
package com.example;
import java.util.TreeMap;
public class Test2 {
public static void main(String[] args) {
TreeMap
treeExample.put(1, "AAA");
treeExample.put(2, "BBB");
treeExample.put(4, "DDD");
System.out.println(treeExample.ceilingKey(1));
System.out.println(treeExample.ceilingKey(3));
System.out.println(treeExample.ceilingKey(6));
}
}
Out Put:
1
4
null.
higherKey
1- public K higherKey(K key) {}
2-the method should return higher key
2-it is method which return the key .and returning key should be satisfy one within following three condition
1- if the higher key exist then it return higher key .
2-if the higher key does not exist then returns the entry for the least key greater than the specified key
example :key 3 if 4 is not exist but 5 is there it's return 5
3-if it not satisfy upper two condition then it should be return null
Example:
package com.example;
import java.util.TreeMap;
public class Test1 {
public static void main(String[] args) {
TreeMap treeExample=new TreeMap();
treeExample.put(1, "AAA");
treeExample.put(2, "BBB");
treeExample.put(3, "CCC");
treeExample.put(4, "DDD");
treeExample.put(6, "FFF");
System.out.println(treeExample.higherKey(3));
System.out.println(treeExample.higherKey(4));
System.out.println(treeExample.higherKey(6));
}
}
Out Put:
4
6
null
2-the method should return higher key
2-it is method which return the key .and returning key should be satisfy one within following three condition
1- if the higher key exist then it return higher key .
2-if the higher key does not exist then returns the entry for the least key greater than the specified key
example :key 3 if 4 is not exist but 5 is there it's return 5
3-if it not satisfy upper two condition then it should be return null
Example:
package com.example;
import java.util.TreeMap;
public class Test1 {
public static void main(String[] args) {
TreeMap
treeExample.put(1, "AAA");
treeExample.put(2, "BBB");
treeExample.put(3, "CCC");
treeExample.put(4, "DDD");
treeExample.put(6, "FFF");
System.out.println(treeExample.higherKey(3));
System.out.println(treeExample.higherKey(4));
System.out.println(treeExample.higherKey(6));
}
}
Out Put:
4
6
null
subMap
1- SortedMap subMap(K fromKey, K toKey);
2- public NavigableMap subMap(K fromKey, boolean fromInclusive,K toKey, boolean toInclusive) {}
------------------------------------------------------------------------------------------------------------
1- SortedMap subMap(K fromKey, K toKey);
1-Returns a view of the portion of this map whose keys range from fromKey inclusive, to toKey , exclusive.
2-If fromKey and toKey are equal, the returned map is empty.
3-The returned map is backed by this map, so changes
------------------------------------------------------------------------------------------------------------
2- public NavigableMap subMap(K fromKey, boolean fromInclusive,K toKey, boolean toInclusive) {}
This method is same as upper method but one main difference is that it provide extra facility to inclusive and exclusive key as user demand
---------------------------------------------------------------------------------------------------------------------------------
Example:
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
public class Test {
public static void main(String[] args) {
TreeMap setmap=new TreeMap();
setmap.put(1, "AAA");
setmap.put(2, "BBB");
setmap.put(3, "CCC");
setmap.put(4, "DDD");
setmap.put(5, "EEE");
SortedMap< Integer, String> simple=setmap.subMap(2, 4);
System.out.println(simple);
SortedMap< Integer, String> simple1=setmap.subMap(2,false, 4,true);
System.out.println(simple1);
}
}
OUTPUT :
{2=BBB, 3=CCC}
{3=CCC, 4=DDD}
2- public NavigableMap
------------------------------------------------------------------------------------------------------------
1- SortedMap
1-Returns a view of the portion of this map whose keys range from fromKey inclusive, to toKey , exclusive.
2-If fromKey and toKey are equal, the returned map is empty.
3-The returned map is backed by this map, so changes
------------------------------------------------------------------------------------------------------------
2- public NavigableMap
This method is same as upper method but one main difference is that it provide extra facility to inclusive and exclusive key as user demand
---------------------------------------------------------------------------------------------------------------------------------
Example:
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
public class Test {
public static void main(String[] args) {
TreeMap
setmap.put(1, "AAA");
setmap.put(2, "BBB");
setmap.put(3, "CCC");
setmap.put(4, "DDD");
setmap.put(5, "EEE");
SortedMap< Integer, String> simple=setmap.subMap(2, 4);
System.out.println(simple);
SortedMap< Integer, String> simple1=setmap.subMap(2,false, 4,true);
System.out.println(simple1);
}
}
OUTPUT :
{2=BBB, 3=CCC}
{3=CCC, 4=DDD}
tailMap
SortedMap tailMap(K fromKey);
1-It is method in the TreeMap .
2-Returns a view of the portion of this map whose keys are greater than or equal to fromKey .
3-This method returns the portion of TreeMap whose keys are grater than or equal to fromKey.
Example :
ublic static void main(String[] args) {
TreeMap mapexample = new TreeMap();
map.put(1, "AAA");
map.put(2, "BBB");
map.put(3, "CCC");
map.put(4, "DDD");
SortedMap smap1 = map.tailMap(3);
System.out.println(smap1);
}
OutPut:{ 3=CCC 4=DDD}
1-It is method in the TreeMap .
2-Returns a view of the portion of this map whose keys are greater than or equal to fromKey .
3-This method returns the portion of TreeMap whose keys are grater than or equal to fromKey.
Example :
ublic static void main(String[] args) {
TreeMap
map.put(1, "AAA");
map.put(2, "BBB");
map.put(3, "CCC");
map.put(4, "DDD");
SortedMap
System.out.println(smap1);
}
OutPut:{ 3=CCC 4=DDD}
HashMap Class
HashMap:
1- deleration of HashMap is following
public class HashMap extends AbstractMap
implements Map, Cloneable, Serializable {}
2-The HashMap gives you an unsorted, unordered Map.
3-HashMap allows one null key in a collection and multiple null values in a collection.
4-This class makes no guarantees as to the order of the map
5- Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
6- HashMap has two parameter that affect its performance
1-initial capacity
2-load factor
initial capacity: capacity at the time the hash table is created .
Load Factor :if the Hash table is full ,then load factor is automatically increased the size of HashMap.
1- deleration of HashMap is following
public class HashMap
implements Map
2-The HashMap gives you an unsorted, unordered Map.
3-HashMap allows one null key in a collection and multiple null values in a collection.
4-This class makes no guarantees as to the order of the map
5- Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
6- HashMap has two parameter that affect its performance
1-initial capacity
2-load factor
initial capacity: capacity at the time the hash table is created .
Load Factor :if the Hash table is full ,then load factor is automatically increased the size of HashMap.
Map interface
1-An object that maps keys to values.
2-A map cannot contain duplicate keys;
3-each key can map to at most one value.
4-both the key and the value are objects.
5-The Map interface is provide three collection view
1-collections of value
2-set of keys
3-set of key -value mappings
6-Four main classes which are implemented the Map interface
1-HashMap
2-Hashtable
3-LinkedHashMap
4-TreeMap
7-All general-purpose map implementation classes should provide two "standard" constructors:
1-a void (no arguments) constructor which creates an empty map
2-a constructor with a single argument of type Map,
3-which creates a new map with the same key-value mappings as its argument.
8-method in this interface
1- boolean containsKey(Object key);
2-boolean containsValue(Object value);
3-V get(Object key);
4-V put(K key, V value);
5-V setValue(V value);
6-V getValue();
7-boolean equals(Object o);
8-int hashCode();
2-A map cannot contain duplicate keys;
3-each key can map to at most one value.
4-both the key and the value are objects.
5-The Map interface is provide three collection view
1-collections of value
2-set of keys
3-set of key -value mappings
6-Four main classes which are implemented the Map interface
1-HashMap
2-Hashtable
3-LinkedHashMap
4-TreeMap
7-All general-purpose map implementation classes should provide two "standard" constructors:
1-a void (no arguments) constructor which creates an empty map
2-a constructor with a single argument of type Map,
3-which creates a new map with the same key-value mappings as its argument.
8-method in this interface
1- boolean containsKey(Object key);
2-boolean containsValue(Object value);
3-V get(Object key);
4-V put(K key, V value);
5-V setValue(V value);
6-V getValue();
7-boolean equals(Object o);
8-int hashCode();
Sunday, January 9, 2011
encapsulation and abstraction ?
Q1: encapsulation is a type of "information hiding" in which you hide data attributes of a class behind getter and setter methods; this allows you to control how data is stored and manipulated. For example you could have a class that holds an IP address (for a networking application). The class might expose the IP address as a String, such as "127.0.0.1" with methods like getIpAddr():String and setIpAddr(String) but internally the ipAddr field could be stored as a Java int.
Whereas, "abstraction" is a concept about modeling: to create an abstraction is to find the "minimal amount of information" about the thing being modeled to solve the requirements of that "thing".
Thursday, January 6, 2011
Subscribe to:
Posts (Atom)