×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: ture ture
Added: Apr 8, 2018 10:16 PM
Views: 3015
Tags: no tags
  1. ARRAYLIST
  2. // Java program to demonstrate working of ArrayList in Java
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. class arrayli
  7. {
  8.     public static void main(String[] args)
  9.                        throws IOException
  10.     {
  11.         // size of ArrayList
  12.         int n = 5;
  13.  
  14.         //declaring ArrayList with initial size n
  15.         ArrayList<Integer> arrli = new ArrayList<Integer>(n);
  16.  
  17.         // Appending the new element at the end of the list
  18.         for (int i=1; i<=n; i++)
  19.             arrli.add(i);
  20.  
  21.         // Printing elements
  22.         System.out.println(arrli);
  23.  
  24.         // Remove element at index 3
  25.         arrli.remove(3);
  26.  
  27.         // Displaying ArrayList after deletion
  28.         System.out.println(arrli);
  29.  
  30.         // Printing elements one by one
  31.         for (int i=0; i<arrli.size(); i++)
  32.             System.out.print(arrli.get(i)+" ");
  33.     }
  34. }
  35.  
  36. LinkedLIST
  37. Questa classe comprende i seguenti metodi:
  38.  
  39. 1. addizione booleana (elemento Object) : aggiunge l'elemento alla fine dell'elenco.
  40.  
  41. 2. void add (int index, Object object) : inserisce l'elemento nella posizione 'index' nella lista.
  42.  
  43. 3. void addFirst (oggetto Object) : inserisce l'elemento all'inizio della lista.
  44.  
  45. 4. addLast (elemento Object) void : aggiunge l'elemento alla fine dell'elenco.
  46.  
  47. 5. contiene boolean (elemento Object) : restituisce true se l'elemento è presente nell'elenco.
  48.  
  49. 6. Oggetto get (indice int): Restituisce l'elemento nella posizione 'indice' nell'elenco. Genera "IndexOutOfBoundsException" se l'indice non rientra nell'intervallo della lista.
  50.  
  51. 7. int indexOf (oggetto Object) : se viene trovato un elemento, restituisce l'indice della prima occorrenza dell'elemento. Altrimenti restituisce -1.
  52.  
  53. 8. Object remove (int index) : rimuove l'elemento nella posizione 'index' in questo elenco. Genera "NoSuchElementException" se l'elenco è vuoto.
  54.  
  55. 9. int size () : restituisce il numero di elementi in questo elenco.
  56.  
  57. 10. void clear () : rimuove tutti gli elementi dall'elenco.
  58.  
  59.  
  60.  
  61. // Java code for Linked List implementation
  62.  
  63. import java.util.*;
  64.  
  65. public class Test
  66. {
  67.     public static void main(String args[])
  68.     {
  69.         // Creating object of class linked list
  70.         LinkedList<String> object = new LinkedList<String>();
  71.  
  72.         // Adding elements to the linked list
  73.         object.add("A");
  74.         object.add("B");
  75.         object.addLast("C");
  76.         object.addFirst("D");
  77.         object.add(2, "E");
  78.         object.add("F");
  79.         object.add("G");
  80.         System.out.println("Linked list : " + object);
  81.  
  82.         // Removing elements from the linked list
  83.         object.remove("B");
  84.         object.remove(3);
  85.         object.removeFirst();
  86.         object.removeLast();
  87.         System.out.println("Linked list after deletion: " + object);
  88.  
  89.         // Finding elements in the linked list
  90.         boolean status = object.contains("E");
  91.  
  92.         if(status)
  93.             System.out.println("List contains the element 'E' ");
  94.         else
  95.             System.out.println("List doesn't contain the element 'E'");
  96.  
  97.         // Number of elements in the linked list
  98.         int size = object.size();
  99.         System.out.println("Size of linked list = " + size);
  100.  
  101.         // Get and set elements from linked list
  102.         Object element = object.get(2);
  103.         System.out.println("Element returned by get() : " + element);
  104.         object.set(2, "Y");
  105.         System.out.println("Linked list after change : " + object);
  106.     }
  107. }
  108.  
  109.  
  110.  
  111. HashSET
  112. Metodi importanti in HashSet:
  113.  
  114. boolean add (E e) : aggiungi l'elemento specificato se non è presente, se è presente restituisce false.
  115. void clear () : rimuove tutti gli elementi dal set.
  116. boolean contiene (Object o) : restituisce true se l'elemento è presente nel set.
  117. boolean remove (Object o) : rimuove l'elemento se è presente nel set.
  118. Iterator iterator () : restituisce un iteratore sull'elemento nel set.
  119. Programma di esempio:
  120.  
  121. // Java program to demonstrate working of HashSet
  122. import java.util.*;
  123.  
  124. class Test
  125. {
  126.     public static void main(String[]args)
  127.     {
  128.         HashSet<String> h = new HashSet<String>();
  129.  
  130.         // adding into HashSet
  131.         h.add("India");
  132.         h.add("Australia");
  133.         h.add("South Africa");
  134.         h.add("India");// adding duplicate elements
  135.  
  136.         // printing HashSet
  137.         System.out.println(h);
  138.         System.out.println("List contains India or not:" +
  139.                            h.contains("India"));
  140.  
  141.         // Removing an item
  142.         h.remove("Australia");
  143.         System.out.println("List after removing Australia:"+h);
  144.  
  145.         // Iterating over hash set items
  146.         System.out.println("Iterating over list:");
  147.         Iterator<String> i = h.iterator();
  148.         while (i.hasNext())
  149.             System.out.println(i.next());
  150.     }
  151. }
  152.  
  153.  
  154.  
  155. HashMap è una struttura di dati che utilizza una funzione di hash per mappare i valori di identificazione, noti come chiavi, ai loro valori associati. Contiene coppie "valore-chiave" e consente il recupero del valore per chiave.
  156.  
  157. La caratteristica più impressionante è la rapida ricerca di elementi specialmente per i grandi no. di elementi. Di default non è sincronizzato, ma possiamo farlo chiamando
  158.  
  159.  Mappa myhash = Collections.synchronizedMap (hashMap);
  160. al momento della creazione, per impedire l'accesso non sincronizzato accidentale alla mappa.
  161. Questi sono vari metodi di classe hashmap importanti. Questo post spiega: put (), get (), isEmpty () e size ()
  162.  
  163. put (): java.util.HashMap.put () svolge un ruolo nell'associare il valore specificato con la chiave specificata in questa mappa. Se la mappa in precedenza conteneva un mapping per la chiave, il vecchio valore viene sostituito.
  164. Sintassi:
  165.  
  166. pubblico V put (chiave K, valore V)
  167.  Parametri:
  168. chiave - chiave con cui deve essere associato il valore specificato
  169. valore - valore da associare alla chiave specificata
  170. Ritorno: il valore precedente associato a
  171. chiave, o null se non ci fosse alcuna mappatura per la chiave.
  172. get (): il metodo java.util.HashMap.get () restituisce il valore a cui è mappata la chiave specificata, oppure null se questa mappa non contiene alcuna mappatura per la chiave.
  173. Sintassi:
  174.  
  175. Parametri V get pubblici (chiave dell'oggetto)
  176. :
  177. chiave: la chiave il cui valore associato deve essere restituito
  178. Ritorno: il valore a cui è specificato
  179. la chiave è mappata, o null se questa mappa non contiene alcuna mappatura
  180. il tasto.
  181. isEmpty (): il metodo java.util.HashMap.isEmpty () restituisce true se la mappa non contiene mapping di valori-chiave.
  182. Sintassi:
  183.  
  184. public boolean isEmpty ()
  185. Return: true se questa mappa non contiene mapping di valori-chiave
  186. size (): java.util.HashMap.size () restituisce il numero di associazioni di valori-chiave in questa mappa.
  187. Sintassi:
  188.  
  189. public int size ()
  190. Return: il numero di mappature chiave-valore in questa mappa.
  191. Implementazione per illustrare i metodi sopra
  192.  
  193.  
  194.  
  195.  
  196.  
  197. // Java program illustrating use of HashMap methods -
  198. // put(), get(), isEmpty() and size()
  199. import java.util.*;
  200. public class NewClass
  201. {
  202.    public static void main(String args[])
  203.    {
  204.        // Creation of HashMap
  205.        HashMap<String, String> Geeks = new HashMap<>();
  206.  
  207.        // Adding values to HashMap as ("keys", "values")
  208.        Geeks.put("Language", "Java");
  209.        Geeks.put("Platform", "Geeks For geeks");
  210.        Geeks.put("Code", "HashMap");
  211.        Geeks.put("Learn", "More");
  212.  
  213.        System.out.println("Testing .isEmpty() method");
  214.  
  215.        // Checks whether the HashMap is empty or not
  216.        // Not empty so printing the values
  217.        if (!Geeks.isEmpty())
  218.        {
  219.            System.out.println("HashMap Geeks is notempty");
  220.  
  221.            // Accessing the contents of HashMap through Keys
  222.            System.out.println("GEEKS : " + Geeks.get("Language"));
  223.            System.out.println("GEEKS : " + Geeks.get("Platform"));
  224.            System.out.println("GEEKS : " + Geeks.get("Code"));
  225.            System.out.println("GEEKS : " + Geeks.get("Learn"));
  226.  
  227.            // size() method prints the size of HashMap.
  228.            System.out.println("Size Of HashMap : " + Geeks.size());
  229.        }
  230.    }
  231. }
  232.  
  233. Altre interfacce
  234. https://www.geeksforgeeks.org/java/