javahelp

What is difference between ArrayList and vector?
Ans: )
1) Synchronization – ArrayList is not thread-safe whereas Vector is thread-safe. In Vector class each method like add(), get(int i) is surrounded with a synchronized block and thus making Vector class thread-safe.

2) Data growth – Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

How can Arraylist be synchronized without using Vector?
Ans) Arraylist can be synchronized using:
• Collection.synchronizedList(List list)

Other collections can be synchronized:

• Collection.synchronizedMap(Map map)
• Collection.synchronizedCollection(Collection c)

If an Employee class is present and its objects are added in an arrayList. Now I want the list to be sorted on the basis of the employeeID of Employee class. What are the steps?
Ans) 1) Implement Comparable interface for the Employee class and override the compareTo(Object obj) method in which compare the employeeID

2) Now call Collections.sort() method and pass list as an argument.

Now consider that Employee class is a jar file.

1) Since Comparable interface cannot be implemented, create Comparator and override the compare(Object obj, Object obj1) method .

2) Call Collections.sort() on the list and pass comparator as an argument.

What is difference between HashMap and HashTable?
Ans) Both collections implements Map. Both collections store value as key-value pairs. The key differences between the two are

1. Access to the Hashtable is synchronized on the table while access to the HashMap isn’t. You can add it, but it isn’t there by default.

2. Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn’t. If you change the map while iterating, you’ll know. • Fail-safe – “if the Hashtable is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException”

3. HashMap permits null values and only one null key, while Hashtable doesn’t allow key or value as null.

What is difference between Arrays and ArrayList ?
Ans) Arrays are created of fix size whereas ArrayList is of not fix size. It means that once array is declared as :

int [] intArray= new int[6];
intArray[7]   // will give ArraysOutOfBoundException.
Also the size of array cannot be incremented or decremented. But with arrayList the size is variable.
Once the array is created elements cannot be added or deleted from it. But with ArrayList the elements can be added and deleted at runtime.
List list = new ArrayList();
list.add(1);
list.add(3);
list.remove(0) // will remove the element from the 1st location.

ArrayList is one dimensional but array can be multidimensional.
int[][][] intArray= new int[3][2][1];   // 3 dimensional array

When to use ArrayList or LinkedList ?
Ans)  Adding new elements is pretty fast for either type of list. For the ArrayList, doing  random lookup using “get” is fast, but for LinkedList, it’s slow. It’s slow because there’s no efficient way to index into the middle of a linked list. When removing elements, using ArrayList is slow. This is because all remaining elements in the underlying array of Object instances must be shifted down for each remove operation. But here LinkedList is fast, because deletion can be done simply by changing a couple of links. So an ArrayList works best for cases where you’re doing random access on the list, and a LinkedList works better if you’re doing a lot of editing in the middle of the list.

Consider a scenario. If an ArrayList has to be iterate to read data only, what are the possible ways and which is the fastest?
Ans) It can be done in two ways, using for loop or using iterator of ArrayList. The first option is faster than using iterator. Because value stored in arraylist is indexed access. So while accessing the value is accessed directly from the index.

Now another question with respect to above question is if accessing through iterator is slow then why do we need it and when to use it.

Ans) For loop does not allow the updation in the array(add or remove operation) inside the loop whereas Iterator does. Also Iterator can be used where there is no clue what type of collections will be used because all collections have iterator.

Which design pattern Iterator follows?
Ans) It follows Iterator design pattern. Iterator Pattern is a type of behavioral pattern. The Iterator pattern is one, which allows you to navigate through a collection of data using a common interface without knowing about the underlying implementation. Iterator should be implemented as an interface. This allows the user to implement it anyway its easier for him/her to return data. The benefits of Iterator are about their strength to provide a common interface for iterating through collections without bothering about underlying implementation.

Example of Iteration design pattern – Enumeration The class java.util.Enumeration is an example of the Iterator pattern. It represents and abstract means of iterating over a collection of elements in some sequential order without the client having to know the representation of the collection being iterated over. It can be used to provide a uniform interface for traversing collections of all kinds.

Why is it preferred to declare: List<String> list = new ArrayList<String>(); instead of ArrayList<String> = new ArrayList<String>();
Ans) It is preferred because:
If later on code needs to be changed from ArrayList to Vector then only at the declaration place we can do that.
The most important one – If a function is declared such that it takes list. E.g void showDetails(List list);
When the parameter is declared as List to the function it can be called by passing any subclass of List like ArrayList,Vector,LinkedList making the function more flexible

How to sort list in reverse order?
Ans) To sort the elements of the List in the reverse natural order of the strings, get a reverse Comparator from the Collections class with reverseOrder(). Then, pass the reverse Comparator to the sort() method.

List list = new ArrayList();
Comparator comp = Collections.reverseOrder();
Collections.sort(list, comp)

How to sort list of strings – case insensitive?
Ans) using Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

Can a null element added to a set ?
Ans) A null element can be added only if the set contains one element because when a second element is added then as per set defination a check is made to check duplicate value and comparison with null element will throw NullPointerException.

Advertisements

30 comments on “javahelp

  1. I conceive this web site has got some real wonderful info for everyone :D. “Anybody who watches three games of football in a row should be declared brain dead.” by Erma Bombeck.

  2. What i don’t understood is in reality how you’re no longer really a lot more well-appreciated than you may be now. You are very intelligent. You recognize thus considerably on the subject of this matter, made me for my part consider it from a lot of various angles. Its like men and women aren’t interested except it’s one thing to accomplish with Girl gaga! Your personal stuffs excellent. At all times care for it up!

  3. hi!,I love your writing very a lot! share we communicate extra approximately your article on AOL? I need a specialist on this house to unravel my problem. May be that’s you! Looking ahead to look you.

  4. What i do not realize is actually how you are no longer actually a lot more smartly-appreciated than you may be right now. You are so intelligent. You realize therefore considerably in relation to this matter, produced me in my opinion imagine it from numerous varied angles. Its like women and men are not interested except it’s one thing to do with Girl gaga! Your own stuffs outstanding. All the time care for it up!

  5. Hello there, I do think your blog may be having web browser compatibility issues. Whenever I look at your site in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping issues. I merely wanted to give you a quick heads up! Aside from that, excellent site!

  6. I would like to thnkx for the efforts you’ve put in writing this website. I’m hoping the same high-grade blog post from you in the upcoming also. In fact your creative writing skills has encouraged me to get my own web site now. Actually the blogging is spreading its wings quickly. Your write up is a great example of it.

  7. It’s a shame you don’t have a donate button! I’d definitely donate to this excellent blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to new updates and will talk about this site with my Facebook group. Chat soon!

  8. hi!,I like your writing very so much! share we be in contact extra approximately your article on AOL? I need an expert in this space to solve my problem. May be that’s you! Looking ahead to look you.

  9. Great work! This is the type of info that should be shared around the net. Shame on the search engines for not positioning this post higher! Come on over and visit my website . Thanks =)

  10. Good post. I learn one thing tougher on completely different blogs everyday. It’ll always be stimulating to read content from other writers and practice a little something from their store. I’d desire to use some with the content on my weblog whether you don’t mind. Natually I’ll provide you with a hyperlink on your net blog. Thanks for sharing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s