Jun 30

I already introduced some methods of set bevore:

a.add(11);
a.size();
a.get(0); // CompilerError; working only for List, not for Set
a.remove(11); // deletion of element “11″ (no index!)

Today I would like to go a little bit deeper; let’s have a look on the following example:

System.out.print(a.add(“Mailand”));
System.out.println(” “+a.add(a));
System.out.println(
” “+a.add(new Employee()));
System.out.println(
” “+a.add(“Mailand”));

In case we have defined Set a = new HashSet(); bevore, we get the following output:

true true true false

In case we have definded Set a = new TreeSet(); bevore, we get an CompilerError since TreeSet wants to save the values in an sorted way, which is due to the different objects not possible.

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 13

TreeSet has the following abilities:

  • ordered and sorted
  • elements are sorted in ascending order (1,2,3 … A,B, C… a, b,c) independent from the inserting order
  • optionally TreeSet can use a constructor (Comparable or Comparator) which includes own ideas for sorting (I will explain more in later posts)
  • internally elements are stored in a blanced binary tree; therefore Red-Black-Tree-structure is used
  • inserting new elements is (due to re-organisation of the “Tree”) more time-consuming than HashSet, but therefore permanent sorting
  • fast sorted output

responsible class is java.util.TreeSet.

Definition/ initialization:

TreeSet a;
a = new TreeSet();

TreeSet b = new TreeSet();

TreeSet c = new TreeSet(100); // CompilerError

Using TreeSet is equals using HashSet:
a.add(11);
a.size();

a.get(0); // CompilerError
a.remove(11);// Deletion of element “11″ (no index!)

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