How to add multiple items to the ArrayList

There are various ways to initialize an ArrayList with multiple elements and add elements to it. 1. using add() method repeatedly : ArrayList<String> str=new ArrayList<>();str.add(“banana”);str.add(“Apple”);System.out.println(str); 2. Using ArrayList.asList() method : ArrayList<String> list=new ArrayList<>(Arrays.asList(“America”,”Europe”,”Asia”));System.out.println(list); 3.Using Collections.addAll() : ArrayList<String> new_list =new ArrayList<>();Collections.addAll(new_list, “Car”,”Bike”);System.out.println(new_list); 4. Using Java Streams : ArrayList<String> another_list=Stream.of(“Lenovo”,”Dell”,”Apple”).collect(Collectors.toCollection(ArrayList::new));System.out.println(another_list) Outputs:[banana, Apple][America, Europe, Asia][Car, Bike][Lenovo, Dell, Apple]

Java : Cursor : ListIterator

List-iterators are applicable for all the list interfaces. It allows to traverse in backward and forward directions both. Output : [A, B, C, D, E, F][Z, B, C, E, U, F]The previous value of F is situated at index 4The previous value of U is situated at index 3The previous value of E is situated…

Java : Cursor : Iterator

Iterators are used to traverse through the collection element. Its applicable for all collection classes. Example : Supported methods : .hasnext()-Checks if there is any element present in the collection. Return type is boolean ..next() -Returns the next element in the collection. Return type is object ..remove()- remove the element from the collection. Does not…

Java: Cursors: Enumeration

Cursors are used to travers through the elements one by one. enumeration is one type of cursor in Java. Its mainly used in case of legacy classes like Vector. Example: Methods used with enumeration are- hasMoreElements()nextElement()

Static and instance variables in java

Static variable: Static variables are declared using the keyword static.Every instances of the particular class shares the same static variable , that means if the changes are made to the static variable, then all the instances of that class will get affected. In the following example ‘color’ is a static variable within the class Ball.We…