Converting Array to List
Array can be converted to List by using asList static function of Arrays class.
Integer[] integerArray = {1,2,3,4};
List<Integer> integerList = Arrays.asList(integerArray);
Arrays.asList Returns a fixed-size list backed by the specified array.
public static List asList(T... a)
recall that argument list of variable length is implemented by packing the arguments into an array and passing that. It can be understood by the following example
List<Long> a = Arrays.asList(1L,2L,3L);
// is equivalent to
List<Long> b = Arrays.asList(new Long[]{1L,2L,3L});
java.lang.UnsupportedClassVersionError: Bad version number in .class file
java.lang.UnsupportedClassVersionError: Bad version number in .class file at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:620) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124) at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1853) at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:875)
Why is this exception coming?
String.intern in Java
String.intern returns a canonical representation for the string
object. A pool of strings, initially empty, is maintained privately by
the class String. All literal strings and string-valued constant
expressions are stored in this pool.
When the intern method is invoked, if the pool already contains a
string equal to this String object as determined by the equals(Object)
method, then the string from the pool is returned. Otherwise, this
String object is added to the pool and a reference to this String
object is returned.
It follows that for any two strings s and t, s.intern() ==
t.intern() is true if and only if s.equals(t) is true. By using intern
one can get better performance as compared to equals method in String
class. equals does the character by character that you probably want.
There are some disadvantages of intern. If we start using intern
with every string then pool size will increase and could lead to memory
problem. Secondly in case of bigger pool, finding a string could be
more costly that having character by character comparison.
What will be output of the following program?
public static void main(String[] args) {
String s1 = new String("Hello");
change(s1);
System.out.println(s1);
}
public static void change(String s1) {
s1 += " Guest";
}
The output of the program will be “Hello” only. ” Guest” won’t be appended to the original string.
This is due to immutable nature of string. In the change method, appending string to s1 won’t change previous string, rather it will create a new string which will be “Hello Guest” and will be pointing to some other memory location and not to the String location which is passed to the ‘change’ method.
Which one of the following is more efficient?
String str1 = "Hello " + "Guest"; // 1
String str2 = (new StringBuffer().append("Hello ")).append("Guest").toString(); //2
str1 is resolved at compile time, while str2 is resolved at runtime time with an extra StringBuffer and String. The version that can be resolved at compile time is more efficient. It avoids the overhead of creating a String and an extra StringBuffer, as well as avoiding the runtime cost of several method calls.
differences between the == operator and the equals() method in Java
Equals method compare values of the receiver and sender object. It returns true if the values are same. While ‘==’ operator is a fundamental operator, it compares reference of the sender and receiver object. E.g.
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3);// false
System.out.println(s1.equals(s2));// true
System.out.println(s1.equals(s3));// true
From the example, we can see that reference and value for s1 & s2 are same. At the same time s3 is a newly created instance which has same value as of s1 and s2 but reference is different.
String in Java?
Strings are immutable. A string can’t be altered once created. Applying a method on string won’t change string’s value; rather it will create a new string. String is also final, and so can’t be sub classed. When you assign one String variable to another, no copy is made. Even when you take a substring there is no new String created.
Creating String : String can be created by assigning string literals as shown below. The text between double quotes is a string literal. By assigning a string literal, you can avoid using the new keyword. In fact, this special shortcut syntax was designed to improve String performance. Each JVM only keeps one copy of each string literal. In the following code firstStr and secondStr points to same string reference. thirdStr creates a new String in String pool.
String firstStr = "I am a String Literal";
String secondStr = "I am a String Literal";
// Points to the same object as firstString
String thirdStr = new String("I am a String Literal");
// By using constructor, It will create a new Object in JVM
Note: You almost always want to initialize String references with literals to avoid creating unnecessary objects in the JVM. This can really add up if you are creating hundreds of identical Strings.
Difference between HashTable and HashMap
The HashTable is almost equal to HashMap except following points.
1. HashTable is synchronized, while HashMap is not.
2. HashTable doesn’t permit null as key while in HashMap it is allowed.
3. HashTable guarantee that order of the table will remain constant over time while in HashMap it is not.
Differences between ArrayList and LinkedList
Both classes implement List interface. Order is the most important feature of a List; it promises to maintain elements in a particular sequence. Following are the main differences
1. An ArrayList implemented with Array, allowed fast random access to an element, while LinkedList are optimal for sequential access and are relatively slow for random access.
2. ArrayList is slow when inserting or removing element from middle of the List while LinkedList provide inexpensive insertion and deletion from the middle of the List.
Differences between Vector and ArrayList
The two classes are very similar, however there are few differences, which can give one a clear idea about their usages.Vector is synchronized. Content in vector is thread-safe. ArrayList is not synchronized hence its content is not thread-safe. Synchronization is not at free of cost. It affects vector’s performance. So if we don’t require thread-safe data, we should use ArrayList.
Internally, both the ArrayList and vector hold data in Array. When we add a new element into ArrayList or Vector, and the object will need to expand its internal array if it runs out of memory. Vector doubles the size of Array by default, while the ArrayList increases its size by 50 percent. Vector does have slight advantage since they allow to reset increment size.Addition and deletion operation in both classes takes same time.
Addition at the end of container can be performed in constant time O(1). Deletion and Addition are more expansive when the element is added/removed from middle, it takes O(n-i). Here n is number of elements in the container and i is index of the element.