Docsity
Docsity

Prepara i tuoi esami
Prepara i tuoi esami

Studia grazie alle numerose risorse presenti su Docsity


Ottieni i punti per scaricare
Ottieni i punti per scaricare

Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium


Guide e consigli
Guide e consigli


Cheatsheet del liguaggio Java, Schemi e mappe concettuali di Programmazione Java

Comprende schematizzato tutte le principali regole del linguaggio Java (Cheatsheet)

Tipologia: Schemi e mappe concettuali

2023/2024

Caricato il 21/05/2025

mattia1719
mattia1719 🇮🇹

16 documenti

1 / 3

Toggle sidebar

Questa pagina non è visibile nell’anteprima

Non perderti parti importanti!

bg1
Java Cheatsheet v1.4 LDB 2024
Classi e interfacce
class: definibili solo metodi concreti
static class: definibile solo internamente a un’altra classe e utilizzabile senza istanziare un oggetto della classe esterna
final class: non estendibile con sottoclassi
abstract class: definibili intestazioni (pubbliche) di metodi astratti e/o metodi concreti (pubblici e privati)
interface: definibili solo intestazioni di metodi pubblici
Lettura e stampa
try(Scanner sc = new Scanner(System.in))
sc.nextInt();
System.out.println(String what)
Caratteri e stringhe
char charAt(int index)
int compareTo(String other)
int compareToIgnoreCase(String other)
boolean equals(Object other)
String String.format(String format, Object args)
int indexOf(String what)
String substring(int start, int end)
String toLowerCase()
String toUpperCase()
String trim()
String isEmpty()
String isBlank()
Controllare se una stringa sè composta da soli caratteri alfabetici: s.chars().allMatch(Character::isAlphabetic)
Controllare se una stringa sè composta da soli caratteri minuscoli: s.chars().allMatch(Character::isLowerCase)
Formattare una stringa scon numero di caratteri prefissato e allineamento a destra: String.format("%10s", s)
Formattare un intero icon un numero di cifre prefissato: String.format("%05d", i)
Formattare un decimale fcon un numero di cifre prefissato: String.format("%.2f", f)
Random
Random random = new Random()
int nextInt(int min, int max)
boolean nextBoolean()
double nextDouble()
long nextLong()
void setSeed(long seed)
Enumerazioni
enum Name {
FIRST,
SECOND,
...
}
E[] values()
E valueOf(String name)
int compareTo(E other)
int ordinal()
Array
E[] Arrays.sort(E[] arr)
E[] Arrays.sort(E[] arr, Comparator<E> c)
E[] Arrays.fill(E[] arr, E val)
boolean Arrays.equals(E[] arr1, E[] arr2)
List<T> Arrays.asList(T[] arr)
Stream<T> Arrays.stream(T[] arr)
int Arrays.hashCode(T[] arr)
String Arrays.toString(T[] arr)
Collezioni
boolean add(E element)
boolean addAll(Collection<E> other)
boolean contains(Object element)
boolean containsAll(Collection<E> other)
boolean equals(Collection<E> other)
boolean isEmpty()
boolean remove(Object element)
boolean removeAll(Collection<E> other)
boolean retainAll(Collection<E> other)
int size()
T[] toArray(new T[])
Stream stream()
T Collections.binarySearch(List<T> list, T key)
Collection Collections.sort(List<T> list)
void Collections.copy(List<T> dest, List<T> src)
int Collections.frequency(Collection<E> c, Obj o)
T Collections.min(Collection<E> c)
T Collections.max(Collection<E> c)
Liste
Implementazioni: ArrayList,LinkedList,Vector,Stack
ArrayList(Collection<? extends E> parent)
LinkedList(Collection<? extends E> parent)
boolean add(E element)
void add(int index, E element)
E get(int index)
int indexOf(Object element)
boolean remove(Object element)
E remove(int index)
E set(int index, E element)
List<E> List.of(E e1, E e2, ...) 1/3
pf3

Anteprima parziale del testo

Scarica Cheatsheet del liguaggio Java e più Schemi e mappe concettuali in PDF di Programmazione Java solo su Docsity!

Java Cheatsheet v1.4^ LDB • 2024

Classi e interfacce

class: definibili solo metodi concreti static class: definibile solo internamente a un’altra classe e utilizzabile senza istanziare un oggetto della classe esterna final class: non estendibile con sottoclassi abstract class: definibili intestazioni (pubbliche) di metodi astratti e/o metodi concreti (pubblici e privati) interface: definibili solo intestazioni di metodi pubblici

Lettura e stampa

try(Scanner sc = new Scanner(System.in)) sc.nextInt();

System.out.println(String what)

Caratteri e stringhe

char charAt(int index) int compareTo(String other) int compareToIgnoreCase(String other) boolean equals(Object other) String String.format(String format, Object args) int indexOf(String what)

String substring(int start, int end) String toLowerCase() String toUpperCase() String trim() String isEmpty() String isBlank()

Controllare se una stringa s è composta da soli caratteri alfabetici: s.chars().allMatch(Character::isAlphabetic)

Controllare se una stringa s è composta da soli caratteri minuscoli: s.chars().allMatch(Character::isLowerCase)

Formattare una stringa s con numero di caratteri prefissato e allineamento a destra: String.format("%10s", s)

Formattare un intero i con un numero di cifre prefissato: String.format("%05d", i)

Formattare un decimale f con un numero di cifre prefissato: String.format("%.2f", f)

Random

Random random = new Random() int nextInt(int min, int max) boolean nextBoolean()

double nextDouble() long nextLong() void setSeed(long seed)

Enumerazioni

enum Name { FIRST, SECOND, ... }

E[] values() E valueOf(String name) int compareTo(E other) int ordinal()

Array

E[] Arrays.sort(E[] arr) E[] Arrays.sort(E[] arr, Comparator c) E[] Arrays.fill(E[] arr, E val) boolean Arrays.equals(E[] arr1, E[] arr2)

List Arrays.asList(T[] arr) Stream Arrays.stream(T[] arr) int Arrays.hashCode(T[] arr) String Arrays.toString(T[] arr)

Collezioni

boolean add(E element) boolean addAll(Collection other) boolean contains(Object element) boolean containsAll(Collection other) boolean equals(Collection other) boolean isEmpty() boolean remove(Object element) boolean removeAll(Collection other) boolean retainAll(Collection other)

int size() T[] toArray(new T[]) Stream stream() T Collections.binarySearch(List list, T key) Collection Collections.sort(List list) void Collections.copy(List dest, List src) int Collections.frequency(Collection c, Obj o) T Collections.min(Collection c) T Collections.max(Collection c)

Liste

Implementazioni: ArrayList, LinkedList, Vector, Stack

ArrayList(Collection<? extends E> parent) LinkedList(Collection<? extends E> parent) boolean add(E element) void add(int index, E element) E get(int index)

int indexOf(Object element) boolean remove(Object element) E remove(int index) E set(int index, E element) List List.of(E e1, E e2, ...)

Insiemi

Implementazioni: HashSet (elementi non ordinati), TreeSet (elementi ordinati)

HashSet(Collection<? extends E> parent) TreeSet(Collection<? extends E> parent)

E first() (solo TreeSet) E last() (solo TreeSet)

Mappe

Implementazioni: HashMap (chiavi non ordinate), TreeMap (chiavi ordinate)

HashMap(Map<? extends K,? extends V> parent) TreeMap(Map<? extends K,? extends V> parent) boolean containsKey(Object key) boolean containsValue(Object value) V get(Object key) V getOrDefault(Object key, V defaultValue) boolean isEmpty() Set keySet() Set<Map.Entry<K, V>> entrySet()

K getKey() V getValue() Collection values() V put(K key, V value) V putIfAbsent(K key, V value) V replace(K key, V value) V remove(Object key) K firstKey() (solo TreeMap) K lastKey() (solo TreeMap)

Istanziare un oggetto (valore) e ottenerlo se la chiave è assente, oppure ottenere il valore inserito se la chiave è presente:

ThisClass value = map.computeIfAbsent(key, k -> new ThisClass());

Confronti tra oggetti

Rendere una classe comparabile: ThisClass implements Comparable { public int compareTo... }

Specificare un ordinamento diverso da quello stabilito dal compareTo alle strutture dati ordinate:

Set s = new TreeSet(new Comparator(){ public int compare(Class o1, Class o2) { int diff = o1.field1 - o2.field1; if(diff != 0) return diff;

...

return o1.fieldN.compareTo(o2.fieldN); } });

Oppure: Set s = new TreeSet(this::compare);

private int compare(Class o1, Class o2) { ... }

Oppure: Set s = new TreeSet((o1, o2) -> { ... });

Gestione dei file

Lettura da file testuale: try(BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line;

while((line = reader.readLine()) != null) { ... } }

Oppure: try(Scanner reader = new Scanner(new FileReader(fileName))) { while(reader.hasNextLine()) { String line = reader.nextLine();

... } }

Scrittura su file testuale: try(BufferedWriter writer = new BufferedWriter(new PrintWriter(fileName))) { writer.println("..."); writer.printf("...", arg1, arg2, ..., argN); } Lettura e scrittura da file binario: try (InputStream reader = new BufferedInputStream( new FileInputStream("...")); OutputStream writer = new BufferedOutputStream( new FileOutputStream("..."))) { int c;

while ((c = reader.read()) != -1) { writer.write(c); } }

Iterazioni sugli oggetti

Per ottenere gli elementi di una struttura interna iterando sull’oggetto: class ThisClass implements Iterable { Collection c = new Collection();

public Iterator iterator() { return c.iterator(); } }