Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Easy Learn Java: Programming Articles, Examples and Tips - Page 454


Previous 1060 Stories (530 Pages, 2 Per Page) Next

Which Collection should I use in my Java program? Part 2.

Go to all tips in

Which Collection should I use in my Java program? Hashtable vs hashmap? vectors vs hashtable? Hashmap vs hashset? Hashtable Vs Two Dimensional Array? ArrayList vs. Vector? Again hashmap vs hashtable? hashmap vs arraylist? More? I compared all the collections in my table below! Oooh...




All the these questions I asked myself at least 10 times, if not even more. And I have not found any good Java Collections overview page on the Internet, where I could pick one collection and compare with another in one page, without going to multiple places.





This Comprehensive Java Collections overview page was the result of my half-day work. I think that the info I have now in two tables below can be good enough to make fast and right choice when you do not know which Java collection can be used in your application.

I hope that I have not done serious mistakes, if you find them, please inform me and I will correct the page. At the bottom of tables you can find Java code examples for all the collections described below.

General-purpose Implementations
Interfaces Implementations
  Hash table Resizable array Tree Linked list Hash table + Linked list
Set HashSet   TreeSet   LinkedHashSet

 

List   ArrayList

Vector

  LinkedList  
Queue          
Map HashMap

Hashtable

  TreeMap   LinkedHashMap

 

http://JavaFAQ.nu

Set: A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

Map: cannot contain duplicate keys; each key can map to at most one value.

The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.

46 Java Free lessons course on our site!

All Java Lessons contents page | Java Lesson 1 | Java Lesson 2 | Java Lesson 3 | Java Lesson 4 | Java Lesson 5 | Java Lesson 6 | Java Lesson 7 | Java Lesson 8 | Java Lesson 9 | Java Lesson 10 | Java Lesson 11 | Java Lesson 12 | Java Lesson 13 | Java Lesson 14 | Java Lesson 15 | Java Lesson 16 | Java Lesson 17 | Java Lesson 18 | Java Lesson 19 | Java Lesson 20 | Java Lesson 21 | Java Lesson 22 | Java Lesson 23 | Java Lesson 24 | Java Lesson 25 | Java Lesson 26 | Java Lesson 27 | Java Lesson 28 | Java Lesson 29 | Java Lesson 30 | Java Lesson 31 | Java Lesson 32 | Java Lesson 33 | Java Lesson 34 | Java Lesson 35 | Java Lesson 36 | Java Lesson 37 | Java Lesson 38 | Java Lesson 39 | Java Lesson 40 | Java Lesson 41 | Java Lesson 42 | Java Lesson 43 | Java Lesson 44 | Java Lesson 45 | Java Lesson 46

Important! Although Array  is not class in Java API, I have included it into the table below, because it is similar to collections in terms of storage. It can be used as a simplest "collection type" Smile.

That's why some functions and properties are not applicable to Array in the table below (marked as N/A).

Java Collections Comprehensive Overview Table, Part 1

  HashSet HashMap ArrayList TreeSet TreeMap LinkedList LinkedHashSet LinkedHashMap
Random
access
no yes yes no yes yes no yes
Serializable yes yes yes yes yes yes yes yes
Synchronized no no no no no no no no
guarantees to the iteration order no no yes yes, yes yes yes yes
constant order over time no no yes,
see note 6
yes,
see note 7
yes,
see note 7
yes yes yes
permits null element yes yes,

null key,
null value

yes no no yes yes yes
is iteration
time proportional to size?****
yes note 1 yes almost,
see note 6
no,
log(n)
no,
log(n)
2n,
see note 8
yes,
just slightly below that of HashSet
yes
Is fail-fast? note 2 yes yes yes yes yes yes yes yes
is resizable? yes yes yes yes yes yes yes  
Only unique elements
(no duplicates)?
 
yes yes no,
allow duplicate elements
yes yes no yes yes
Notes   The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. This class is roughly equivalent to Vector, except that it is unsynchronized.     Implements all optional list operations.
Implements the Queue interface, providing first-in-first-out queue operations for add, poll, etc.
- FIFO semantics
LinkedHashSet: Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. LinkedHashMap: Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.
Since Java
version
1.2 1.2 1.2 1.2 1.2 1.2 1.4 1.4
Java Code Examples code examples for HashSet code examples for HashMap code examples for ArrayList code examples for TreeSet code examples for TreeMap code examples for LinkedList code examples for LinkedHashSet code examples for LinkedHashMap
Always visit http://JavaFAQ.nu when you need more info about Java!

To be continued

Part 2. Java Collections Comprehensive Overview Table (continuation).

  Vector Hashtable PriorityQueue WeakHashMap Array
Random
access
yes yes no no yes
Serializable yes yes yes no!!! note 3
Synchronized yes yes no no N/A
guarantees to the iteration order yes yes yes,

look at note 5 below

no yes
constant order over time yes yes yes no yes
permits null element   no no yes, both keys and values not
is iteration
time proportional to size? note 4
yes yes depends on operation,
see note 5
yes yes
Is fail-fast? note 2 yes yes ?? yes  
is resizable? yes yes yes yes no
Only unique elements
(no duplicates)?
yes yes

keys must be unique, values not

yes yes no
Notes     PriorityQueue orders its elements according to their values. A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently than other Map implementations.

This class is intended primarily for use with key objects whose equals methods test for object identity using the == operator. Once such a key is discarded it can never be recreated, so it is impossible to do a lookup of that key in a WeakHashMap at some later time and be surprised that its entry has been removed. This class will work perfectly well with key objects whose equals methods are not based upon object identity, such as String instances. With such recreatable key objects, however, the automatic removal of WeakHashMap entries whose keys have been discarded may prove to be confusing.

 
Since Java
version
1.0 1.0 1.5 1.2 1.0
Java Code Examples code examples for Vector code examples for Hashtable code examples for PriorityQueue code examples for WeakHashMap code examples forArray
http://JavaFAQ.nu

 

 

In general, it is good API design practice not to make users pay for a feature they don't use. Furthermore, unnecessary synchronization can result in deadlock under certain circumstances.

Note 1 - Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

Note 2 - Look at the fail-fast tip

Note 3 An array is Serializable if component type is Serializable

Note 4 If iteration performance is important and if iterating requires time proportional to the sum of a collection instance's size, it's very important not to set the initial capacity too high (or the load factor too low).

Note 5 (PriorityQueue): It orders elements according to an order specified at construction time, which is specified either according to their natural order (see Comparable), or according to a Comparator, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

Implementation note: this implementation provides O(log(n)) time for the insertion methods (offer, poll, remove() and add) methods; linear time for the remove(Object) and contains(Object) methods; and constant time for the retrieval methods (peek, element, and size).

Note 6: The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking).

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

Note 7: TreeSet class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.

Note 8: All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

Copyright by JavaFAQ.nu, Alexandre Patchine, 2006


23716 bytes more | 1 comment | Printer Friendly Page  Send to a Friend | Score: 4
Posted by Javaaddict on Thursday, May 18, 2006 (01:00:00) (23425 reads)

IntelliJ IDEA vs. Eclipse vs. NetBeans: my personal experience...

Go to all tips in Java Tools

This article is about best IDE which you can find for the money - IntelliJ IDEA. But before I will describe the goodies of the latest version and explain why people pay for it, I would like to say a few words about programming with Notepad...


Last century was popular to write programs with Notepad or similar simple text editors. On Internet you could find hundreds of sites which were very proudly informing you: "This site is designed with Notepad". Many people were proud that they could keep in the head all small details of HTML language. I used to design FrontPage, but then I discovered that it was quite buggy - produced very bad html code. So, I found other powerful tools that saved me hundreds of hours.

I never could understand why people would like to spend much time on very routine operations. Even more: they were proud that they did it, it was difficult and almost impossible, but they succeeded and now you see it - my site is here!


Very similar situation was with Java and C++ programming. A lot of people struggled with notepad and unfriendly command line Java compilers: error messages that they produced sometime were very difficult to understand.

Fortunately this time is passed, you can not find anymore those sites with Notepad logo.

The reason is simple and always was clear for me: they did not stand out a competition!!! They simply become extinct. How can you keep, for example my site http://JavaFAQ.nu consistent and up to date? Do it daily with just notepad? No way!

The same with Java - if you program for money, not for your own sake you should forget about notepad, until you are Java Guru! If Java programming is your job- you need modern tools, which will increase efficiency and productivity of your daily work with Java programming by hundreds percents!

It is funny to own retro car, but you can not drive it to your job daily, help your family without working with it a few hours a day. Instead of laying on your sofa, you will lay under that retro car, trying to fix all possible problems.

Writing programs with notepad is the same. You will spend your time not on actual Java programming, but on a code maintenance. Modern IDEs compile your code on the flight and highlight immediately all errors and even more - warns you about possible troubles that can arise during run time. There is no way to do it with Notepad. That's why it is no future for Notepad in Java programming.

Today probably just novices have not heard about IDE - Integrated Development Environment.

Most famous IDEs in Java world today are Eclipse, NetBeans and IntelliJ IDEA.

Eclipse is probably most famous one! Extremely flexible and configurable, has thousands plugins and it is free. Although you can find commercial ones. From my experience I could give it 5 ... 5-... Why? Because of complexity to use it from the scratch, finding all necessary plugins and make them working stable. I tried a few times to start to use Eclipse, but always switched back to IntelliJ IDEA. The problem I had was not so specific - I just wanted to use Eclipse for JSP design. Free plugins, which I found could not even close to come to features of IDEA.

Although there was another problem, due to its extremely high popularity Eclipse caused a flood of plugins on The Internet. Every small and big company, programming team and standalone programmer decided to add theirs 5 cents to Eclipse development - developed their very own stuff.

For me it was difficult to navigate through this plugins crowd and I gave up. I believe that exist good places where people will explain every my question and make everything working. But I need IDE to program my JSPs and not fix/search all the problems with Eclipse. Next time I will do that, but now I am busy with another things, like writing this article Smile

From my point of view, the chosen name - Eclipse - slightly reflects the situation: I got blinded (exactly like under Sun eclipse) by wide choice of plugins Smile

NetBeans I have not used much, just tried... One thing I got confused - the name. I directly felt that it is not best name which was chosen. Two words Net and Beans - both said me: it is not for you! You are not writing Net applications and not use beans. My first impression of the name was that this IDE for those who works with J2EE. And there is no room for those who works with just Classical Java. I was not right of course, but this impression is still in my head: it is not for you! Anyway, all features I have seen in this tool looked useful Smile. I can not say more about NetBeans...

And last one, I am stuck to, is IntelliJ IDEA. I use it more than two years. I got advised by one consultant when we worked together and he said that IntelliJ IDEA is solid product. I gave it a chance and still do it. The most remarkable thing that it is not free!

We have seen many IDEs for Java during last years, almost all of them are gone. But this is surviving and growing. The piece of success that it is from new European Union country where labor force is probably cheaper. While Eclipse and NetBeans are completely free, the guys from JetBrains (company which develops IntelliJ IDEA) succeeding to sale it!

The one of key to success I believe is the quality of Czech beer (JetBrain's headquarter is in Prague)! R&D is done and continues in St. Petersburg, Russia and you know them, Russians - they can hack everything in the world.

Let's look at the IntelliJ IDEA, latest version. Currently it is 5.1.

In this article I will mostly describe new features, since older IDEA 4.x was quite similar to other IDEs. Most important improvements for me I marked with word New.

JSP support New

HTML and XHTML support New

CSS support

I18N support New

Code Inspections New

Debugger  New

JavaScript Support New

Quick definition lookup New

Enhancements in J2EE support New

J2ME support

Refactorings and Search

Code Analysis

Enhanced Open API

Plugin development support New

Version Control Support

Desktop

Project Views

Editor

Code Assistance

Formatter and Code Style

Project and Settings

XML support

This is just very my short overview of IntelliJ IDEA and detailed features description I will give you in Part 2 of this article.

To be continued...

Disclaimer: English is not native language for me and I appreciate if you correct my errors in a friendly way Smile


6714 bytes more | comments? | Printer Friendly Page  Send to a Friend | Score: 3.5
Posted by Javaaddict on Monday, May 15, 2006 (01:30:00) (34379 reads)

Previous 1060 Stories (530 Pages, 2 Per Page) Next

530| 529| 528| 527| 526| 525| 524| 523| 522| 521| 520| 519| 518| 517| 516| 515| 514| 513| 512| 511| 510| 509| 508| 507| 506| 505| 504| 503| 502| 501| 500| 499| 498| 497| 496| 495| 494| 493| 492| 491| 490| 489| 488| 487| 486| 485| 484| 483| 482| 481| 480| 479| 478| 477| 476| 475| 474| 473| 472| 471| 470| 469| 468| 467| 466| 465| 464| 463| 462| 461| 460| 459| 458| 457| 456| 455|
454
| 453| 452| 451| 450| 449| 448| 447| 446| 445| 444| 443| 442| 441| 440| 439| 438| 437| 436| 435| 434| 433| 432| 431| 430| 429| 428| 427| 426| 425| 424| 423| 422| 421| 420| 419| 418| 417| 416| 415| 414| 413| 412| 411| 410| 409| 408| 407| 406| 405| 404| 403| 402| 401| 400| 399| 398| 397| 396| 395| 394| 393| 392| 391| 390| 389| 388| 387| 386| 385| 384| 383| 382| 381| 380| 379| 378| 377| 376| 375| 374| 373| 372| 371| 370| 369| 368| 367| 366| 365| 364| 363| 362| 361| 360| 359| 358| 357| 356| 355| 354| 353| 352| 351| 350| 349| 348| 347| 346| 345| 344| 343| 342| 341| 340| 339| 338| 337| 336| 335| 334| 333| 332| 331| 330| 329| 328| 327| 326| 325| 324| 323| 322| 321| 320| 319| 318| 317| 316| 315| 314| 313| 312| 311| 310| 309| 308| 307| 306| 305| 304| 303| 302| 301| 300| 299| 298| 297| 296| 295| 294| 293| 292| 291| 290| 289| 288| 287| 286| 285| 284| 283| 282| 281| 280| 279| 278| 277| 276| 275| 274| 273| 272| 271| 270| 269| 268| 267| 266| 265| 264| 263| 262| 261| 260| 259| 258| 257| 256| 255| 254| 253| 252| 251| 250| 249| 248| 247| 246| 245| 244| 243| 242| 241| 240| 239| 238| 237| 236| 235| 234| 233| 232| 231| 230| 229| 228| 227| 226| 225| 224| 223| 222| 221| 220| 219| 218| 217| 216| 215| 214| 213| 212| 211| 210| 209| 208| 207| 206| 205| 204| 203| 202| 201| 200| 199| 198| 197| 196| 195| 194| 193| 192| 191| 190| 189| 188| 187| 186| 185| 184| 183| 182| 181| 180| 179| 178| 177| 176| 175| 174| 173| 172| 171| 170| 169| 168| 167| 166| 165| 164| 163| 162| 161| 160| 159| 158| 157| 156| 155| 154| 153| 152| 151| 150| 149| 148| 147| 146| 145| 144| 143| 142| 141| 140| 139| 138| 137| 136| 135| 134| 133| 132| 131| 130| 129| 128| 127| 126| 125| 124| 123| 122| 121| 120| 119| 118| 117| 116| 115| 114| 113| 112| 111| 110| 109| 108| 107| 106| 105| 104| 103| 102| 101| 100| 99| 98| 97| 96| 95| 94| 93| 92| 91| 90| 89| 88| 87| 86| 85| 84| 83| 82| 81| 80| 79| 78| 77| 76| 75| 74| 73| 72| 71| 70| 69| 68| 67| 66| 65| 64| 63| 62| 61| 60| 59| 58| 57| 56| 55| 54| 53| 52| 51| 50| 49| 48| 47| 46| 45| 44| 43| 42| 41| 40| 39| 38| 37| 36| 35| 34| 33| 32| 31| 30| 29| 28| 27| 26| 25| 24| 23| 22| 21| 20| 19| 18| 17| 16| 15| 14| 13| 12| 11| 10| 9| 8| 7| 6| 5| 4| 3| 2| 1|


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy