what are collections in java code example
Example 1: collections in java
LIST: Can store duplicate values,
Keeps the insertion order.
It allows multiple null values,
Also we can read a certain value by index.
- ArrayList not syncronized, array based class
- LinkedList not synchronized, doubly linked
- Vector is synchronized, thread safe
SET: Can only store unique values,
And does not maintain order
- HashSet can have null, order is not guaranteed
- LinkedHashSet can have null and keeps the order
- TreeSet sorts the order and don't accept null
QUQUE : Accepts duplicates,
Doesn't have index num,
First in first our order.
MAP : is a (key-value format)
and keys are always unique,
and value can be duplicated.
- HashTable don't have null key, sychronized(thread-safe)
- LinkedHashMap can have null key, keeps order
- HasHMap can have null key, order is not guaranteed
- TreeMap doesn't have null key and keys are sorted
Example 2: how to define a collection in java
List<String> list = Arrays.asList("Lars", "Simon");
Example 3: what type of collections used
Depending on the data that I am working with, I use
Arrays, Lists, Sets, Maps.
Example 4: what is collection fromework
The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures.
Example 5: What kind of collections you used in java?
Selenium List of elements. List of the collection
We have Set Collection Type in our framework.
Getwindowhandles to handle multiple tabs.
return type is Getwindowhandles methods is going to return Set.
For avoid Duplication in Selenium multiple check box, dropdown box.
We use Set. We get only unique webelements.
Map has not Interface. For key and value.
List and Set has not a key and value data.
API testing we are getting data from json file.
Jason file datas are key and value format.
Data base testing we are using maps.
If you want to get pairs of data from tables.
Example 6: when to use collections in java
When to use List, Set and Map?
If we need to access elements frequently by using index, then List is a way
to go ArrayList provides faster access if we know index.
If we want to store elements and want them to maintain an order,
then go for List again. List is an ordered collection and maintain order.
If we want to create collection of unique elements and don't want
any duplicate than choose any Set implementation. (HashSet... )
If we want store data in form Key and Value than Map is the way to go.
We can choose from HashMap, Hashtable...