String Flashcards

1
Q

Arrays.asList();

A
  • The asList() method of java.util.Arrays class
  • Return a fixed-size list backed by the specified array.
  • This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.toArray().
  • The returned list is serializable and implements RandomAccess.
  • This runs in** O(1)** time.
  • List<String> list = Arrays.asList(a);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to convert this array to ArrayList?

String a[]
                = new String[] { "A", "B", "C", "D" };
A

Arrays.asList(a);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can we add a new string to followoing list?

List<String> list = Arrays.asList(a);
A
  • No. bcz Arrays.asList() returns a fixed size ArrayList.
  • This list may not be structurally modified. That means that it is not possible to add elements to the list or remove elements from the list. The reason simply is that the list is still backed by the array, and the size of the array may not change.

When we need a fixed size mutable collection we go for Array

And that’s the key point here: An array is not a Collection. The Arrays.asList method mainly serves as a “bridge” between the “arrays world” and the “collections world”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between ArrayList al1 and al2?

String[] geeks = {“Rahul”, “Utkarsh”, “Shubham”, “Neelam”};

List<String> al1 = new ArrayList<String>(Arrays.asList(geeks));</String></String>

List<String> al2 = Arrays.asList(geeks);</String>

A
  • We can’t add new element in al2 but we can add new element in al1
  • Creation of al1 is O(n) and al2 is O(1);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Collections.addAll();

A
  • public static boolean addAll(Collection c, T… a)
  • Adds all of the specified elements to the specified collection.
  • Elements to be added may be specified individually or as an array.
  • Return type is boolean type. It returns true if the collection changed as a result of the call.
    It throws UnsupportedOperationException if collection c does not support add method and throws IllegalArgumentException if some aspect of a value in elements(or elements of array) prevents it from being added to collection c.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to split words of string?

A

String str = “geekss@for@geekss”;
String[] arrOfStr = str.split(“@”, 2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Split it into array of strings. The array should not contain any sapace

Hello Vivek How Are you ?

A

str.split(“//s+”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Convert ArrayList<String> to String with a dilimter.</String>

A
  • Using String.join(“ “, list);
  • Using StringJoiner class
  • Using StringBuilder
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to remove leading and trailing spaces in a String?

A

str.trim();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly