Found 9326 Articles for Object Oriented Programming

How to convert a list collection into a dictionary in Java?

Nikitha N
Updated on 30-Jul-2019 22:30:21

1K+ Views

Following is an example to convert a list collection into a dictionary in Java.Example Live Demoimport java.util.ArrayList; import java.util.Dictionary; import java.util.Hashtable; public class CollectionDictionary {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       System.out.println(list);       Dictionary dictionary = new Hashtable();       Hashtable hashTable = new Hashtable();       hashTable.put(1, list.get(0));       hashTable.put(2, list.get(1));       hashTable.put(3, list.get(2));       hashTable.put(4, list.get(3));       System.out.println(hashTable);    } }Output[JavaFx, Java, WebGL, OpenCV] {4=OpenCV, 3=WebGL, 2=Java, 1=JavaFx}

How to copy or clone a Java ArrayList?

Abhinanda Shri
Updated on 25-Feb-2020 09:50:14

1K+ Views

The clone() method of the java.util.ArrayList class returns a shallow copy of this ArrayList instance (i.e the elements themselves are not copied). Using this method, you can copy the contents of one array list to other.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String args[]) {       ArrayList arrlist1 = new ArrayList();       arrlist1.add(new StringBuilder("Learning-"));       ArrayList arrlist2 = (ArrayList) arrlist1.clone();       StringBuilder strbuilder = arrlist1.get(0);       strbuilder.append("list1, list2-both pointing to the same StringBuilder");       System.out.println("The 1st list prints: ");       for (int i = ... Read More

How to compare two ArrayList for equality in Java?

Srinivas Gorla
Updated on 30-Jul-2019 22:30:21

12K+ Views

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.Example Live Demoimport java.util.ArrayList; public class ComparingList {    public static void main(String[] args) {       ArrayList list1 = new ArrayList();       list1.add("JavaFx");       list1.add("Java");       list1.add("WebGL");       list1.add("OpenCV");       ArrayList list2 = new ArrayList();       list2.add("JavaFx");       list2.add("Java");       list2.add("WebGL"); ... Read More

How to use remove(obj), remove(index), and removeAll() methods in Java list collections?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:21

137 Views

remove(int index) − Removes the element at position index from the invoking list and returns the deleted element. The resulting list is compacted. That is, the indexes of subsequent elements are decremented by one. removeRange(int fromIndex, int toIndex) − Removes all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. removeAll(Collection c) − Removes from this list all of its elements that are contained in the specified collection (optional operation).

How to iterate over a Java list?

Abhinaya
Updated on 30-Jul-2019 22:30:21

11K+ Views

Often, you will want to cycle through the elements in a collection. For example, you might want to display each element.The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface.Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list and the modification of elements.Before you can access a collection through an iterator, you must obtain one. Each of the collection classes provides an iterator() method that returns an iterator to the start of the ... Read More

How to find the index of given element of a Java List?

Govinda Sai
Updated on 30-Jul-2019 22:30:21

15K+ Views

The indexOf(Object) method of the java.util.ArrayList class returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Using this method, you can find the index of a given element.Example Live Demoimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add("G");       arrlist.add("E");       arrlist.add("F");       arrlist.add("M");       System.out.println("Size of list: " + arrlist.size());       for (String value : arrlist) {       ... Read More

How to convert a comma separated String into an ArrayList in Java?

Sravani S
Updated on 30-Jul-2019 22:30:21

7K+ Views

To convert a comma separated String into an ArrayListSplit the String into an array of Strings using the split() method.Now, convert the obtained String array to list using the asList() method of the Arrays class.Example Live Demoimport java.util.Arrays; import java.util.List; public class Sample {    public static void main(String[] args) {       String myString = "JavaFx,Java,WebGL,OpenCV";       String[] myArray = myString.split(",");       System.out.println("Contents of the array ::"+Arrays.toString(myArray));       List myList = Arrays.asList(myArray);    } }OutputContents of the array ::[JavaFx, Java, WebGL, OpenCV]

How to replace an element of an ArrayList in Java?

Priya Pallavi
Updated on 30-Jul-2019 22:30:21

8K+ Views

You can replace an element of an ArrayList using the set() method of the Collections class. This method accepts two parameters an integer parameter indicating the index of the element to be replaced and an element to replace with.Example Live Demoimport java.util.ArrayList; public class ArrayListSample {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       System.out.println(list);       list.set(2, "HBase");       System.out.println(list);    } }Output[JavaFx, Java, WebGL, OpenCV] [JavaFx, Java, HBase, OpenCV]

How to sort an ArrayList in Java in descending order?

V Jyothi
Updated on 30-Jul-2019 22:30:21

6K+ Views

To sort the contents of an ArrayList in descending orderCreate an ArrayList.Sort the contents of the ArrayList using the sort() method of the Collections class.Then, reverse array list using the reverse() method of the Collections class.Example:import java.util.ArrayList; import java.util.Collections; public class ArrayListSample {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       Collections.sort(list);       System.out.println(list);       Collections.reverse(list);       System.out.println(list);    } }Output:[Java, JavaFx, OpenCV, WebGL] [WebGL, OpenCV, JavaFx, Java]

How to sort an ArrayList in Java in ascending order?

Priya Pallavi
Updated on 30-Jul-2019 22:30:21

428 Views

You can sort an ArrayList using the sort() method of the Collections class this method accepts a list object as a parameter and sorts the contents of it in ascending order.Example:import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; public class ArrayListSample {    public static void main(String[] args) {       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       Set set = new LinkedHashSet(list);       Collections.sort(list);       System.out.println(list);    } }Output:[Java, JavaFx, OpenCV, WebGL]

Advertisements