Write a Program to check if an element is part of a collection or not.

We can use contains() function to check if an element is part of any collection apart from Map. For Map type collections, we need to containsKey() to find if a key is present in the map and containsValue() to find if the value exists in the map.

Example

package test;

import java.util.*;

public class Main {

public static void main(String[] args) {

List<String> list = new ArrayList<>();
list.add("Rahul");
list.add("Rohan");
list.add("Megha");

// Verify if list contains a specific element or not
System.out.println(list.contains("Suraj"));
System.out.println(list.contains("Megha"));

Map<Integer, String> hashmap = new HashMap<>();
hashmap.put(1, "Arsenal");
hashmap.put(2, "ManU");
hashmap.put(3, "ManCity");
// Verify if map contains a specific key/value or not
System.out.println(hashmap.containsKey(4));
System.out.println(hashmap.containsKey(1));
System.out.println(hashmap.containsValue("Real Madrid"));
System.out.println(hashmap.containsValue("Arsenal"));

}

}

Output

false
true
false
true
false
true

Write a program to reverse an ArrayList.

We can reverse the ArrayList using reverse() method of Collections class.

Example

import java.util.*;

public class Main {

public static void main(String[] args) {
//Creating an ArrayList object
ArrayList < String > arrlist = new ArrayList < String > ();

//Adding elements to ArrayList object
arrlist.add("Nokia");
arrlist.add("Samsung");
arrlist.add("iPhone");
arrlist.add("One+");

//Displaying ArrayList Before Reverse
System.out.println("Before Reverse ArrayList:");
System.out.println(arrlist);

/*Reversing the list using
Collections.reverse() method*/
Collections.reverse(arrlist);

//Displaying list after reverse
System.out.println("After Reverse ArrayList:");
System.out.println(arrlist);
}

}

Output

Before Reverse ArrayList: [Nokia, Samsung, iPhone, One + ]
After Reverse ArrayList: [One + , iPhone, Samsung, Nokia]

Write a program to convert ArrayList to LinkedList and vice versa.

Using ArrayList & LinkedList constructor which takes a collection as an argument, we can convert ArrayList to LinkedList & vice versa.

import java.util.*;

public class Main {

public static void main(String[] args) {
LinkedList < String > linkedlist = new LinkedList < String > ();
linkedlist.add("Mango");
linkedlist.add("Banana");
linkedlist.add("Pear");
linkedlist.add("Apple");
linkedlist.add("Orange");
// Converting LinkedList to ArrayList
ArrayList < String > list = new ArrayList < String > (linkedlist);

System.out.println(list);

// Converting ArrayList to LinkedList
List < String > list1 = new LinkedList < String > (list);
System.out.println(list);
}
}

Output

[Mango, Banana, Pear, Apple, Orange]
[Mango, Banana, Pear, Apple, Orange]

How to get the highest and lowest values of a List?

There are 2 ways we can achieve that. First, sorting the collection and getting the first & last elements of the collection. Second, using max() & min() methods of Collections class, we can get the highest and lowest values.

Example

import java.util.*;

public class Main {

public static void main(String[] args) {
LinkedList < Integer > list = new LinkedList < Integer > ();
list.add(44);
list.add(11);
list.add(22);
list.add(33);
//Sort the collection and get the first and last element
Collections.sort(list);
System.out.println(list.get(0));
System.out.println(list.get((list.size() - 1)));
//Use max() and min() methods of COllections classs
System.out.println(Collections.max(list));
System.out.println(Collections.min(list));


}
}


Output
11
44
44
11

How to make a collection read-only?

A read-only Collection means a Collection where we cannot perform modifications like add, remove or set. We can only read from the Collection by using get method or by using Iterator.

This kind of Collection is good for a certain requirement where parameters are final and cannot be changed.

 In Java, we can use Collections.unModifiableList() method to create read-only List , Collections.unmodifiableSet() for creating read-only Set like read-only HashSet and similarly creating a read-only Map in Java, as shown in below example. Any modification in the read-only List will result in java.lang.UnSupportedOperationException.

Example

import java.util.*;

public class Main {

public static void main(String[] args) {

{
List<String> stuff = Arrays.asList(new String[] { "a", "b" });
List<String> list = new ArrayList<>(stuff);
list = Collections.unmodifiableList(list);
Set<String> set = new HashSet<>(stuff);
set = Collections.unmodifiableSet(set);
Map<String, String> map = new HashMap<>();
map = Collections.unmodifiableMap(map);
System.out.println("Collections are read-only now.");
}

}
}

What is the difference between Iterator & Enumerator?

Compare different Collection classes with respect to their properties.

The below table summarizes the properties of different collection classes and their properties with respect to duplicate values, null value allowed/not allowed, sorting and preserving the insertion order.

List


What are the differences between List & Set?


What are the differences between Array and ArrayList?
What are the differences between Arraylist & LinkedlList?

Set


What are the differences between Set & Map?

What are the differences between HashSet & TreeSet?

Map


What are the differences between HashSet & HashMap?
What are the differences between HashMap & TreeMap?


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}