Skip to main content

Posts

What does it mean by Vector being synchronized?

So you found that there is a very famous (and hence commonly found on all the question sites and hence not asked anywhere anymore) interview question: What is difference between Vector and ArrayList? And the answer is simple: Vector is synchronized but ArrayList is not. Fine. But what does this mean? If you understand how synchronized works I will tell you the simplest way to understand the difference. Just look at the implementation of Vector ( here ) and ArrayList ( here ). In the implementation of Vector you will find that many of the methods are synchronized. size(), indexOf(), isEmpty(), set() etc. all are synchronized and hence all these methods can be accessed by one thread at a time. So the state of the Vector can be modified/accessed by only one thread at a time. On the other hand, none of the methods in ArrayList are synchronized. This is what Java doc says: Note that this implementation is not synchronized. If multiple threads access an ArrayList instance co...

Hands On Review : AOL's Alto Mail

I am a sucker for new online services, specially emails and when AOL announced its new offering Alto Mail, I was really excited as anything coming on the heels of Microsoft's Outlook.com with a premise of making mail arrangement a more visual affair had to be definitely worth a try. So does it really deliver the goods? First of all let's get this straight: Alto Mail is NOT a new Email service, it is just a new web based email client which you can connect to view your Gmail/Yahoo/AOL/iCloud content. (Only Yahoo.com accounts will work, no ".co.in" or ".co.uk" or any of the other Yahoo local domain names work with Alto). Once you login to Alto with any of your above mentioned accounts, give it a few hours (well it will depend on the size of your inbox and folders) to pull in all the emails/contacts from your account. But even before the import is completed, any emails sent from Alto will reflect in your 'Sent' folder of your linked mail account. ...

Javascript : How to setfocus on an element created at runtime?

In my web page, I created a new row in my data table at runtime. The new row consisted of two text boxes and I needed to set focus on first text box in the row. I got the id of the text box element by inspecting the HTML code and tried to do the following: In java code: inputId = ; executeJavaScript("selectDPTypeRow('"+inputId+"')" );   ('executeJavaScript' is a custom method to execute JS code from Java code) In JS code: function selectNewRow(textBoxElementId){        var textBox = document.getElementById(textBoxElementId);        textBox.setFocus(); } But this never worked as 'textBox' always gave null. I was baffled as to why should the element be not found in the page DOM since inputId was always correct. Later I realized that by the time the above code is called, the new elements have not yet been rendered on the page and hence the elements are not found. To overcome this I introduced a timelag of half a second...

Quick Java : How to remove duplicate elements from an ArrayList or Vector

There are various algorithms to do so and if you want to implement any of those you are free to do so, but if you are using java this can be done in two lines! As you know Sets don't allow duplicate elements, so why not use this property. Convert your ArrayList (or Vector or any other Collection for that matter) to Set and convert it back to your Collection. In below code snippet, I have an ArrayList of integers named 'listOfNumbers'. HashSet uniqueNumbers = new HashSet (listOfNumbers);   Using HashSet will mess up the order of your elements, so if you are concerned about the order, use LinkedHashSet. After conversion to set the duplicates are removed, convert it back to Collection object. ArrayList listOfUniqueNumbers = new ArrayList (uniqueNumbers); And you are done! How cool is that :)

ADF: How to remove components at run time!

In an earlier post we saw h ow to add ADF components in a page at runtime . It is easy enough. After some time I got a bit confused as to how to remove components, either predefined or these dynamically created components at runtime. This is easy too and I cursed myself for getting stuck over such a trifle issue. Let's assume that you have a panel group layout whose child components you want to remove and you have bound this panel group layout in your backing bean. In the event handler method of the action on which you want the components to be removed, do this: getMyPanelGroupLayout().getChildren().clear(); Simple isn't it? Since ADF exposes children of any component in form of a List, adding and removing any component is as easy as adding or removing to a list. Here I have removed all the child components of the panel group layout using 'clear()'. If you want to selectively remove components, based on id or name or any other property, you can traverse the l...

ADF: Using Input Range Slider (RichInputRangeSlider)

Input Range Slider is one of the coolest components of ADF which is not only very intuitive for users but also gives a 'cool' look to the page :) So how do we get minimum and maximum values selected on the slider? Among other properties of this component are 'minimum' and a 'maximum' and then there is 'value'. This causes some confusion as to  how can we get the values selected on the slider. This is not half as complex as it seems! Let's focus first on the 'value'. The value of this component is returned in form of a oracle.adf.view.rich.model.NumberRange object. oracle.adf.view.rich.model.NumberRange minMax = (oracle.adf.view.rich.model.NumberRange)getInputRangeSlider().getValue(); You can extract both the minimum and maximum values from this object like this:         int startValue = minMax.getMinimum().intValue();         int endValue = minMax.getMaximum().intValue(); and that's it. You have both the values ...

What is reordering and how does Volatile help?

Following excerpt is from  JSR 133 (Java Memory Model) FAQ What is meant by reordering of instructions? There are a number of cases in which accesses to program variables (object instance fields, class static fields, and array elements) may appear to execute in a different order than was specified by the program. The compiler is free to take liberties with the ordering of instructions in the name of optimization. Processors may execute instructions out of order under certain circumstances. Data may be moved between registers, processor caches, and main memory in different order than specified by the program. For example, if a thread writes to field  a  and then to field  b , and the value of  b  does not depend on the value of  a , then the compiler is free to reorder these operations, and the cache is free to flush  b  to main memory before  a . There are a number of potential sources of reordering, such as the compiler, the...