Collection 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: declaring collection java

import java.util.Collection;
import java.util.ArrayList;
Collection<Integer> fibonacci = new Arraylist<Integer>();

Example 3: how to define a collection in java

List<String> list = Arrays.asList("Lars", "Simon");

Example 4: fusion vecteur ordonner java

public static int[] fusion2 ( int[] tab1, int[]tab2 ){
        int[] result= new int[tab1.length+tab2.length];
        int index1=0;
        int index2=0;
        int i=0;
        while (i<tab1.length+tab2.length && index1!=tab1.length && index2 != tab2.length){//on vérifie qu'on sort d'aucun des 3 tableaux sinon sa te fournit une erreur
            if (tab1[index1]<=tab2[index2]){ //si le tableau 1 est <= au tableau 2
                result[i]=tab1[index1];
                index1++;
            }
            else if(tab1[index1]>tab2[index2]){ //si le tableau 1 est > au tableau 2
                result[i]=tab2[index2];
                index2++;
            }
            i++;
        }
        //dans le cas ou les deux tableaux ne font pas la même taille
        while (index1 < tab1.length){ //le premier tableau est plus grand que le second
            result[i]=tab1[index1];
            i++;
            index1++;
        }
        while (index2 < tab2.length){ //la c'est le second 
            result[i]=tab2[index2];
            i++;
            index2++;
        }
        return result;
    }

Example 5: collection api

Postman is a Client toolto work with APIs to send
requests get responseOrganize the requestsCollection
in Postman is a top level folder
to store your requests so we can run any time

Example 6: what is collection fromework

The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures.

Tags:

Java Example