Jul 10

As already explained PriorityQueue can have other priorities than the natural order. Therefore (as for other Collections) Comparable or Comparator is used:

class ObjSort implements Comparator <Integer>{

public int compare(Integer a,Integer b)

return (b.compareTo(a));

}

This will be used while implementing PriorityQueue:

PriorityQueue <Integer> a = new PriorityQueue <Integer>(100, new ObjSort());

In case we are assigning the following values:

a.offer(3);
a.offer(7);
a.offer(1);
a.offer(5);
a.offer(9);

the output from:

int b=a.size();

for (int i =0; i<b; i++)

System.out.println(a.peek() +” ” +a.size()); //Output of the highest Element without deletion

is:

9 5
9 5
9 5
9 5
9 5

and the output from

for (int i =0; i<b; i++)

System.out.println(a.poll() +” ” +a.size()); // Output and deletion of the highest element

is:

9 4
7 3
5 2
3 1
1 0


Tagged with:
May 25

It is quite important for SCJP to know exactly differences and similarities of the single classes. Therefore here an overview about the characteristics of the classes:

class abilities
HashMap Connection with a unique key, null keys and multiple null values are allowed 

good performance (dependent on hashCode() implementation, but no order

HashTable Connection with an unique key 

Thread safe

otherwise analogue HashMap

TreeMap Connection with an unique key 

inserting new elements is more time consuming as eg HashMap, but therefore

permanent sorting and

fast, sorted output

LinkedHashMap Connection with an unique key 

faster at adding and deleting elements than HashMap

slower while iterating trough elements

HashSet Uniqueness of values 

fast sorting and finding of elements, but

no order

TreeSet Uniqueness of values 

adding of new elements more time consuming as eg HashSet, therfore

permanent sorting and

fast, sorted output

LinkedHashSet Uniqueness of values, 

high performance at multiple operations

ordered HashSet

ArrayList Values can appear double, 

faster in iterating through elements

Vector Values can appear double 

Thread safe

otherwise analogue ArrayList

LinkedList Values can appear double 

faster in adding and deleting elements than ArrayList

slower in iterating through elements

PriorityQueue “waiting queue” 

sorting by priority

Tagged with:
May 20

PriorityQueue has the following abilities:

  • Priority in, priority out, in contradiction to FIFO (first in, first out) “the more important the earlier”
  • order represents the relatively priority
  • order is done either due to natural order or in relation to Comparator

responsible class is java.util.PriorityQueue .

 

Definition/ Implementation:

PriorityQueue a;
a = new PriorityQueue();

PriorityQueue b = new PriorityQueue();


Use:

a.offer(“Hello”); // Adding a element in natural order

a.remove(); // deletion of the highest element

a.size(); // returning size

Tagged with:
May 19

There are different kind of Queue, but for SCJP there is only one relevant:

  • PriorityQueue

About Queues:

  • Stack of elements which are taken one by one (typically FIFO – first in, first out)
  • support of all standard Collection Methods
  • additional methods for adding/ removing and to check queue elements
  • A waiting list can take a unlimited amount of elements and returning them back in the order of the insertion. (Similar to supermarkt – those which are first on sales point are served first – first in (there), first out)
  • LinkedList is already expanded with the implementation of queue interface
Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs