Java Genericos POO
Java Genericos POO
Guillermo Cámara-Chávez
1/120
Métodos Genéricos
3/120
Métodos Genéricos (cont.)
4/120
Métodos Genéricos (cont.)
5/120
Métodos Genéricos (cont.)
p u b l i c c l a s s GenericMethodTest {
//metodo generico
public s t a t i c < E > void printArray ( E [ ] inputArray
){
// exibe os elementos do vetor
f o r ( E element : inputArray )
System . o u t . p r i n t f ( " %s " , e l e m e n t ) ;
System . o u t . p r i n t l n ( ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
// cria vetores dos tipos Integer, Double e Character
Integer [ ] integerArray = {1 ,2 ,3 ,4 ,5 ,6};
Double [ ] d o u b l e A r r a y = { 1 . 1 , 2 . 2 , 3 . 3 , 4 . 4 , 5 . 5 , 6 . 6 } ;
C h a r a c t e r [ ] c h a r a c t e r A r r a y ={’H ’ , ’E ’ , ’L ’ , ’L ’ , ’O ’ } ;
6/120
Métodos Genéricos (cont.)
7/120
Métodos Genéricos (cont.)
integerArray contains
1 2 3 4 5 6
doubleArray contains
1.1 2.2 3.3 4.4 5.5 6.6
characterArray contains
H E L L O
8/120
Métodos Genéricos (cont.)
9/120
Métodos Genéricos (cont.)
10/120
Métodos Genéricos (cont.)
System . o u t . p r i n t l n ( ) ;
}
11/120
Métodos Genéricos (cont.)
12/120
Classes Genéricas
14/120
Classes Genéricas (cont.)
I A conversão é implı́cita;
15/120
Classes Genéricas (cont.)
16/120
Classes Genéricas (cont.)
17/120
[Link]
p u b l i c c l a s s EmptyStackException extends
RuntimeException {
p u b l i c EmptyStackException () {
t h i s ( " Stack is empty " ) ;
}
p u b l i c E m p t y S t a c k E x c e p t i o n ( S t r i n g msg ) {
s u p e r ( msg ) ;
}
}
18/120
[Link]
public FullStackException () {
t h i s ( " Stack is full " ) ;
}
p u b l i c F u l l S t a c k E x c e p t i o n ( S t r i n g msg ) {
s u p e r ( msg ) ;
}
}
19/120
[Link]
p u b l i c c l a s s Stack < E >
{
p r i v a t e f i n a l i n t s i z e ; // numero de elementos da pilha
p r i v a t e i n t t o p ; // indice do topo
p r i v a t e E [ ] e l e m e n t s ; // vetor para armazenar os elementos
// o tamanho padrao e 10
p u b l i c Stack ()
{
t h i s ( 10 ) ;
}
e l e m e n t s [ ++t o p ] = p u s h V a l u e ;
}
p u b l i c E pop ( )
{
i f ( t o p == −1 )
t h r o w new E m p t y S t a c k E x c e p t i o n ( " Stack is empty
, cannot pop " ) ;
r e t u r n e l e m e n t s [ top−− ] ;
}
}
21/120
Classes Genéricas
22/120
Classes Genéricas (cont.)
23/120
Classes Genéricas (cont.)
24/120
[Link]
p u b l i c c l a s s StackTest {
p r i v a t e d o u b l e [ ] d o u b l e E l e m e n t s ={1.1 , 2 . 2 , 3 . 3 , 4 . 4 ,
5.5 , 6.6};
private int [ ] integerElements = { 1 , 2 , 3 , 4 , 5 , 6 ,
7 , 8 , 9 , 1 0 , 11 } ;
f o r ( T element : elements ) {
System . o u t . p r i n t f ( " %s " , e l e m e n t ) ;
s t a c k . push ( e l e m e n t ) ;
}
}
catch ( FullStackException fullStackException )
{
System . o u t . p r i n t l n ( ) ;
fullStackException . printStackTrace () ;
}
}
26/120
[Link] (cont.)
// metodo generico que testa o metodo pop da classe generica
p u b l i c < T > v o i d t e s t P o p ( S t r i n g name , Stack < T >
stack ) {
try {
System . o u t . p r i n t f ( "\ nPopping elements from %s
\n" , name ) ;
T popValue ;
while ( true ) {
p o p V a l u e = s t a c k . pop ( ) ;
System . o u t . p r i n t f ( " %s " , p o p V a l u e ) ;
}
}
catch ( EmptyStackException emptyStackException ) {
System . o u t . p r i n t l n ( ) ;
emptyStackException . printStackTrace () ;
}
}
27/120
[Link] (cont.)
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
S t a c k T e s t a p p l i c a t i o n = new S t a c k T e s t ( ) ;
application . testStacks () ;
}
}
28/120
[Link] (cont.)
29/120
[Link] (cont.)
Popping e l e m e n t s from d o u b l e S t a c k
5.5 4.4 3.3 2.2 1.1
j a v a s t a c k g e n e r i c o . E m p t y S t a c k E x c e p t i o n : S t a c k i s empty ,
c a n n o t pop
a t j a v a s t a c k g e n e r i c o . S t a c k . pop ( S t a c k . j a v a : 2 6 )
at j a v a s t a c k g e n e r i c o . JavaStackGenerico . testPop (
JavaStackGenerico . java :56)
at j a v a s t a c k g e n e r i c o . JavaStackGenerico . t e s t S t a c k s (
JavaStackGenerico . java :22)
a t j a v a s t a c k g e n e r i c o . J a v a S t a c k G e n e r i c o . main (
JavaStackGenerico . java :69)
30/120
[Link] (cont.)
31/120
[Link] (cont.)
Popping e l e m e n t s from i n t e g e r S t a c k
10 9 8 7 6 5 4 3 2 1
j a v a s t a c k g e n e r i c o . E m p t y S t a c k E x c e p t i o n : S t a c k i s empty ,
c a n n o t pop
a t j a v a s t a c k g e n e r i c o . S t a c k . pop ( S t a c k . j a v a : 2 6 )
at j a v a s t a c k g e n e r i c o . JavaStackGenerico . testPop (
JavaStackGenerico . java :56)
at j a v a s t a c k g e n e r i c o . JavaStackGenerico . t e s t S t a c k s (
JavaStackGenerico . java :24)
a t j a v a s t a c k g e n e r i c o . J a v a S t a c k G e n e r i c o . main (
JavaStackGenerico . java :69)
32/120
Tipos “Crus”
34/120
Coringas em Métodos Genéricos
35/120
Coringas em Métodos Genéricos (cont.)
p u b l i c s t a t i c d o u b l e sum ( A r r a y L i s t < ? e x t e n d s
Number > l i s t )
{
double t o t a l = 0;
f o r ( Number e l e m e n t : l i s t )
t o t a l += e l e m e n t . d o u b l e V a l u e ( ) ;
return total ;
}
36/120
Coringas em Métodos Genéricos (cont.)
37/120
Genéricos e Herança
38/120
Coleções
I Pacote [Link]
39/120
Coleções (cont.)
40/120
Classe Arrays
41/120
[Link]
import java . u t i l . Arrays ;
public c l a s s UsingArrays {
private int intArray [ ] = { 1 , 2 , 3 , 4 , 5 , 6 };
p r i v a t e double doubleArray [ ] = { 8.4 , 9.3 , 0.2 , 7.9 ,
3.4 };
p r i v a t e i n t f i l l e d I n t A r r a y [ ] , intArrayCopy [ ] ;
public UsingArrays () {
f i l l e d I n t A r r a y = new i n t [ 10 ] ;
i n t A r r a y C o p y = new i n t [ i n t A r r a y . l e n g t h ] ;
A r r a y s . f i l l ( f i l l e d I n t A r r a y , 7 ) ; // preenche com 7s
A r r a y s . s o r t ( d o u b l e A r r a y ) ; // ordena crescentemente
// preenche os vetores
System . a r r a y c o p y ( i n t A r r a y , 0 , i n t A r r a y C o p y , 0 ,
intArray . length ) ;
}
42/120
[Link] (cont.)
public void printArrays () {
System . o u t . p r i n t ( " doubleArray : " ) ;
f o r ( double doubleValue : doubleArray )
System . o u t . p r i n t f ( " %.1f " , d o u b l e V a l u e ) ;
System . o u t . p r i n t l n ( "\n" ) ;
}
43/120
[Link] (cont.)
// pesquisa um valor no vetor
public int searchForInt ( int value )
{
return Arrays . binarySearch ( intArray , value ) ;
}
44/120
[Link] (cont.)
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
U s i n g A r r a y s u s i n g A r r a y s = new U s i n g A r r a y s ( ) ;
usingArrays . printArrays () ;
usingArrays . printEquality () ;
int location = usingArrays . searchForInt ( 5 ) ;
i f ( l o c a t i o n >= 0 )
System . o u t . p r i n t f ( " Found 5 at element %d in
intArray \n" , l o c a t i o n ) ;
else
System . o u t . p r i n t l n ( "5 not found in intArray " ) ;
l o c a t i o n = u s i n g A r r a y s . s e a r c h F o r I n t ( 8763 ) ;
i f ( l o c a t i o n >= 0 )
System . o u t . p r i n t f ( " Found 8763 at element %d
in intArray \n" , l o c a t i o n ) ;
else
System . o u t . p r i n t l n ( " 8763 not found in
intArray " ) ;
}
}
45/120
[Link] (cont.)
i n t A r r a y == i n t A r r a y C o p y
i n t A r r a y != f i l l e d I n t A r r a y
Found 5 a t e l e m e n t 4 i n i n t A r r a y
8763 n o t f o u n d i n i n t A r r a y
46/120
Interface Collection e Classe Collections
I Adicionar elementos;
I Esvaziar;
I Comparar.
47/120
Interface Collection e Classe Collections (cont.)
48/120
Interface Collection e Classe Collections (cont.)
49/120
Listas
50/120
Listas (cont.)
I Mais rápidos.
51/120
ArrayList e Iterator
52/120
ArrayList e Iterator (cont.)
import java . util . List ;
import java . util . ArrayList ;
import java . util . Collection ;
import java . util . Iterator ;
// remove elementos
removeColors ( l i s t , removeList ) ;
54/120
ArrayList e Iterator (cont.)
// remove elementos especificados em collection2 de collection1
p r i v a t e void removeColors (
Collection < String > collection1 ,
Collection < String > collection2 ) {
// retorna o iterador
Iterator < String > iterator = collection1 .
i t e r a t o r () ;
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
new C o l l e c t i o n T e s t ( ) ;
}
}
55/120
ArrayList e Iterator (cont.)
ArrayList :
MAGENTA RED WHITE BLUE CYAN
ArrayList a f t e r c a l l i n g removeColors :
MAGENTA
56/120
ArrayList e Iterator (cont.)
57/120
ArrayList e Iterator (cont.)
58/120
ArrayList e Iterator (cont.)
A r r a y L i s t <S t r i n g > a r r = new A r r a y L i s t <S t r i n g >() ;
I t e r a t o r i t = arr . i t e r a t o r () ;
a r r . add ( " bom " ) ;
a r r . add ( " dia " ) ;
a r r . add ( " hoje " ) ;
w h i l e ( i t . hasNext ( ) ) {
System . o u t . p r i n t l n ( i t . n e x t ( ) ) ;
}
I t e r a t o r i t = arr . i t e r a t o r () ;
w h i l e ( i t . hasNext ( ) ) {
System . o u t . p r i n t l n ( i t . n e x t ( ) ) ;
}
59/120
LinkedList
60/120
LinkedList (cont.)
import java . u t i l . L i s t ;
import java . u t i l . LinkedList ;
import java . u t i l . L i s t I t e r a t o r ;
// adiciona elementos
for ( String color : colors )
l i s t 1 . add ( c o l o r ) ;
61/120
LinkedList (cont.)
// adiciona elementos
for ( String color : colors2 )
l i s t 2 . add ( c o l o r ) ;
l i s t 1 . a d d A l l ( l i s t 2 ) ; // concatena as listas
l i s t 2 = n u l l ; // libera
p r i n t L i s t ( l i s t 1 ) ; // exibe os elementos
// converte para maiusculas
convertToUppercaseStrings ( l i s t 1 ) ;
p r i n t L i s t ( l i s t 1 ) ; // exibe os elementos
62/120
LinkedList (cont.)
System . o u t . p r i n t l n ( ) ;
}
63/120
LinkedList (cont.)
// converte para maiusculas
private void convertToUppercaseStrings (
List < String > l i s t ) {
ListIterator < String > iterator =
l i s t . l i s t I t e r a t o r () ;
w h i l e ( i t e r a t o r . hasNext ( ) ) {
S t r i n g c o l o r = i t e r a t o r . n e x t ( ) ; // retorna o item
i t e r a t o r . s e t ( c o l o r . t o U p p e r C a s e ( ) ) ; // converte
}
}
64/120
LinkedList (cont.)
// imprime a lista invertida
private void printReversedList ( List < String > l i s t
) {
ListIterator < String > iterator =
l i s t . l i s t I t e r a t o r ( l i s t . size () ) ;
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
new L i s t T e s t ( ) ;
}
}
65/120
LinkedList (cont.)
list :
black yellow green blue v i o l e t s i l v e r g o l d w h i t e brown
blue gray s i l v e r
list :
BLACK YELLOW GREEN BLUE VIOLET SILVER GOLD WHITE BROWN
BLUE GRAY SILVER
D e l e t i n g elements 4 to 7 . . .
list :
BLACK YELLOW GREEN BLUE WHITE BROWN BLUE GRAY SILVER
Reversed L i s t :
SILVER GRAY BLUE BROWN WHITE BLUE GREEN YELLOW BLACK
66/120
LinkedList (cont.)
67/120
LinkedList (cont.)
I Os iteradores também possuem métodos hasPrevious e
previous
69/120
[Link]
p u b l i c c l a s s UsingToArray
{
// cria uma LinkedList, adiciona elementos e converte para um vetor
p u b l i c UsingToArray ( )
{
S t r i n g c o l o r s [ ] = { " black " , " blue " , " yellow " } ;
L i n k e d L i s t <S t r i n g > l i n k s =
new L i n k e d L i s t < S t r i n g >( A r r a y s . a s L i s t ( c o l o r s ) )
;
70/120
[Link] (cont.)
// converte para um vetor
c o l o r s = l i n k s . t o A r r a y ( new S t r i n g [ l i n k s . s i z e ( ) ] ) ;
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
new U s i n g T o A r r a y ( ) ;
}
}
71/120
[Link] (cont.)
colors :
cyan b l a c k b l u e y e l l o w green red pink
links
cyan b l a c k b l u e y e l l o w green red pink
72/120
[Link] (cont.)
I Uma vez que obtemos uma List criada pelo método asList
(sem alocar memória), o único método de modificação que
podemos utilizar é o set
L i s t <S t r i n g > l i n k s 2 = A r r a y s . a s L i s t ( c o l o r s ) ;
l i n k s 2 . s e t ( 2 , " gray " ) ;
73/120
[Link] (cont.)
I Método toArray
74/120
Vector
75/120
[Link]
p ub l ic c l a s s VectorTest
{
p r i v a t e s t a t i c f i n a l S t r i n g c o l o r s [ ] = { " red " , "
white " , " blue " } ;
p ub l ic VectorTest ()
{
V e c t o r < S t r i n g > v e c t o r = new V e c t o r < S t r i n g >() ;
printVector ( vector ) ;
// adiciona elementos
for ( String color : colors )
v e c t o r . add ( c o l o r ) ;
printVector ( vector ) ;
76/120
[Link] (cont.)
77/120
[Link] (cont.)
78/120
[Link] (cont.)
p r i v a t e v o i d p r i n t V e c t o r ( Vector< S t r i n g >
vectorToOutput ) {
i f ( vectorToOutput . isEmpty ( ) )
System . o u t . p r i n t ( " vector is empty " ) ;
e l s e { // itera pelos elementos
System . o u t . p r i n t ( " vector contains : " ) ;
//exibe os elementos
f o r ( S t r i n g element : vectorToOutput )
System . o u t . p r i n t f ( " %s " , e l e m e n t ) ;
}
System . o u t . p r i n t l n ( "\n" ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
new V e c t o r T e s t ( ) ;
}
}
79/120
[Link] (cont.)
v e c t o r i s empty
vector contains : red white blue
F i r s t element : red
Last element : blue
Size : 2
C a p a c i t y : 10
80/120
Algoritmos
81/120
Algoritmos (cont.)
82/120
Algoritmos (cont.)
83/120
Pilhas
84/120
Pilhas (cont.)
p u b l i c c l a s s StackTest
{
p u b l i c StackTest ()
{
Stack < Number > s t a c k = new Stack < Number >() ;
85/120
Pilhas (cont.)
86/120
Pilhas (cont.)
try
{
Number r e m o v e d O b j e c t = n u l l ;
while ( true )
{
r e m o v e d O b j e c t = s t a c k . pop ( ) ;
System . o u t . p r i n t f ( " %s popped \n" ,
removedObject ) ;
printStack ( stack ) ;
}
}
catch ( EmptyStackException emptyStackException )
{
emptyStackException . printStackTrace () ;
}
}
87/120
Pilhas (cont.)
p r i v a t e v o i d p r i n t S t a c k ( Stack < Number > s t a c k ) {
i f ( s t a c k . isEmpty ( ) )
System . o u t . p r i n t ( " stack is empty \n\n" ) ; //
pilha vazia
else {
System . o u t . p r i n t ( " stack contains : " ) ;
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
new S t a c k T e s t ( ) ;
}
}
88/120
Pilhas (cont.)
s t a c k c o n t a i n s : 12 ( t o p )
s t a c k c o n t a i n s : 12 34567 ( t o p )
s t a c k c o n t a i n s : 12 34567 1 . 0 ( t o p )
s t a c k c o n t a i n s : 12 34567 1 . 0 1 2 3 4 . 5 6 7 8 ( t o p )
1 2 3 4 . 5 6 7 8 popped
s t a c k c o n t a i n s : 12 34567 1 . 0 ( t o p )
1 . 0 popped
s t a c k c o n t a i n s : 12 34567 ( t o p )
89/120
Pilhas (cont.)
34567 popped
s t a c k c o n t a i n s : 12 ( t o p )
12 popped
s t a c k i s empty
java . u t i l . EmptyStackException
at j a v a . u t i l . Stack . peek ( Stack . j a v a : 1 0 2 )
a t j a v a . u t i l . S t a c k . pop ( S t a c k . j a v a : 8 4 )
a t j a v a s t a c k . J a v a S t a c k .< i n i t >( J a v a S t a c k . j a v a : 3 3 )
a t j a v a s t a c k . J a v a S t a c k . main ( J a v a S t a c k . j a v a : 5 5 )
90/120
Filas de Prioridade
91/120
Filas de Prioridade (cont.)
92/120
[Link]
93/120
[Link] (cont.)
94/120
[Link] (cont.)
P o l l i n g from queue : 3 . 2 5 . 4 9 . 8
95/120
Conjuntos (Set)
96/120
Conjuntos (Set) (cont.)
97/120
[Link]
p u b l i c c l a s s SetTest
{
p r i v a t e s t a t i c f i n a l S t r i n g c o l o r s [ ] = { " red " , "
white " , " blue " , " green " , " gray " , " orange " , " tan "
, " white " , " cyan " , " peach " , " gray " , " orange " } ;
98/120
[Link] (cont.)
//cria o conjunto a partir do vetor, para eliminar duplicatas
private void printNonDuplicates ( Collection < String
> collection ) {
// cria o HashSet
Set< S t r i n g > s e t = new HashSet< S t r i n g >(
collection ) ;
System . o u t . p r i n t l n ( ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
new S e t T e s t ( ) ;
}
}
99/120
[Link] (cont.)
A r r a y L i s t : [ r e d , w h i t e , b l u e , g r e e n , g r a y , o r a n g e , tan ,
w h i t e , cyan , peach , g r a y , o r a n g e ]
Nonduplicates are :
red cyan white tan gray green orange b l u e peach
100/120
Mapas
101/120
Mapas (cont.)
102/120
[Link]
p u b l i c c l a s s WordTypeCount {
p r i v a t e Map< S t r i n g , I n t e g e r > map ;
p r i v a t e Scanner scanner ;
p u b l i c WordTypeCount ( ) {
// cria o HashMap
map = new HashMap< S t r i n g , I n t e g e r >() ;
s c a n n e r = new S c a n n e r ( System . i n ) ;
c r e a t e M a p ( ) ; // cria o mapa baseado na entrada
d i s p l a y M a p ( ) ; // exibe o conteúdo do mapa
}
103/120
[Link] (cont.)
p r i v a t e v o i d createMap ( ) {
System . o u t . p r i n t l n ( " Enter a string :" ) ;
String input = scanner . nextLine () ;
104/120
[Link] (cont.)
// processa o texto da entrada
w h i l e ( t o k e n i z e r . hasMoreTokens ( ) ) // enquanto
houver entrada
{
S t r i n g word = t o k e n i z e r . n e x t T o k e n ( ) .
t o L o w e r C a s e ( ) ; // pega a
palavra
// ordena as chaves
T r e e S e t < S t r i n g > s o r t e d K e y s = new T r e e S e t <
S t r i n g >( k e y s ) ;
106/120
[Link] (cont.)
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
new WordTypeCount ( ) ;
}
}
107/120
[Link] (cont.)
E n t e r a s t r i n g : To be o r n o t t o be : t h a t i s t h e
q u e s t i o n Whether ’tis nobler to suffer
Map contains :
Key Value
’tis 1
be 1
be : 1
is 1
nobler 1
not 1
or 1
question 1
suffer 1
that 1
the 1
to 3
whether 1
s i z e :13
isEmpty : f a l s e
108/120
Mapas
109/120
Mapas (cont.)
110/120
Exemplo de ordenação
Seja a classe Conta que consta dos atributos codigo e nome. Gerar
uma lista que contenha um conjunto de Contas, ordenar os dados
baseados no código da conta usando a interface Comparable (a
comparação é realizada entre o objeto que chama o método e o
objeto passado como parâmetro)
111/120
Exemplo de ordenação (cont.)
p u b l i c c l a s s Conta i m p l e m e n t s Comparable<Conta> {
p r i v a t e i n t num ;
p r i v a t e S t r i n g nome ;
p u b l i c Conta ( ) {
t h i s ( 0 , "" ) ;
}
112/120
Exemplo de ordenação (cont.)
p u b l i c i n t compareTo ( Conta o ) {
i f ( t h i s . num < o . num )
r e t u r n −1;
i f ( t h i s . num > o . num )
return 1;
return 0;
}
p u b l i c S t r i n g getNome ( ) {
r e t u r n nome ;
}
}
113/120
Exemplo de ordenação (cont.)
import java . util . ArrayList ;
import java . util . Collections ;
import java . util . Comparator ;
import java . util . List ;
p u b l i c c l a s s JAvaOrdena {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i s t <Conta> c o n t a = new A r r a y L i s t <>() ;
Conta o b j = new Conta ( 2 6 6 , " Marco " ) ;
c o n t a . add ( o b j ) ;
Conta o b j 2 = new Conta ( 2 9 , " Pedro " ) ;
c o n t a . add ( o b j 2 ) ;
Conta o b j 3 = new Conta ( 1 0 1 , " Andre " ) ;
c o n t a . add ( o b j 3 ) ;
115/120
Exemplo de ordenação
Seja a classe Conta que consta dos atributos codigo e nome. Gerar
uma lista que contenha um conjunto de Contas, ordenar os dados
baseados no nome usando a interface Comparator (a comparação é
realizada entre dois objetos passados como parâmetro)
116/120
Exemplo de ordenação (cont.)
p u b l i c c l a s s Conta {
p r i v a t e i n t num ;
p r i v a t e S t r i n g nome ;
p u b l i c Conta ( ) {
t h i s ( 0 , "" ) ;
}
p u b l i c S t r i n g getNome ( ) {
r e t u r n nome ;
}
} 117/120
Exemplo de ordenação (cont.)
i m p o r t j a v a . u t i l . Comparator ;
118/120
Exemplo de ordenação (cont.)
import java . util . ArrayList ;
import java . util . Collections ;
import java . util . Comparator ;
import java . util . List ;
p u b l i c c l a s s JAvaOrdena {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i s t <Conta> c o n t a = new A r r a y L i s t <>() ;
Conta o b j = new Conta ( 2 6 6 , " Marco " ) ;
c o n t a . add ( o b j ) ;
Conta o b j 2 = new Conta ( 2 9 , " Pedro " ) ;
c o n t a . add ( o b j 2 ) ;
Conta o b j 3 = new Conta ( 1 0 1 , " Andre " ) ;
c o n t a . add ( o b j 3 ) ;
C o l l e c t i o n s . s o r t ( c o n t a , new SortbyName ( ) ) ;
f o r ( Conta elem : c o n t a )
System . o u t . p r i n t l n ( elem ) ;
}
}
119/120
FIM
120/120