Java - como puedo extraer algo especifico de un array

   
Vista:

como puedo extraer algo especifico de un array

Publicado por Oscar (8 intervenciones) el 20/08/2015 23:03:58
Tengo un ArrayList, es de tipo "Usuario", suponiendo que usuario es una clase mia..necesito recorrerlo y extraer de el un atributo
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

como puedo extraer algo especifico de un array

Publicado por Sample (1 intervención) el 21/08/2015 19:19:06
Hi guy.
This is an example of using lambdas and ArrayList.


1
2
3
4
5
6
// Gender.java
package com.jdojo.lambda;
 
public enum Gender {
MALE, FEMALE
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Person.java
package com.jdojo.lambda;
 
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import static com.jdojo.lambda.Gender.MALE;
import static com.jdojo.lambda.Gender.FEMALE;
 
public class Person {
private String firstName;
private String lastName;
private LocalDate dob;
private Gender gender;
 
public Person(String firstName, String lastName, LocalDate dob, Gender gender) {
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
this.gender = gender;
}
public String getFirstName() {
return firstName;
}
 
public void setFirstName(String firstName) {
this.firstName = firstName;
}
 
public String getLastName() {
return lastName;
}
 
public void setLastName(String lastName) {
this.lastName = lastName;
}
 
public LocalDate getDob() {
return dob;
}
 
public void setDob(LocalDate dob) {
this.dob = dob;
}
 
public Gender getGender() {
return gender;
}
 
public void setGender(Gender gender) {
this.gender = gender;
}
 
@Override
public String toString() {
return firstName + " " + lastName + ", " + gender + ", " + dob;
}
 
// A utility method
public static List<Person> getPersons() {
ArrayList<Person> list = new ArrayList<>();
list.add(new Person("John", "Jacobs", LocalDate.of(1975, 1, 20), MALE));
list.add(new Person("Wally", "Inman", LocalDate.of(1965, 9, 12), MALE));
list.add(new Person("Donna", "Jacobs", LocalDate.of(1970, 9, 12), FEMALE));
return list;
}
}

/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/ FunctionUtil.java
package com.jdojo.lambda;
 
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
 
public class FunctionUtil {
// Applies an action on each item in a list
public static <T> void forEach(List<T> list, Consumer<? super T> action) {
for(T item : list) {
action.accept(item);
}
}
 
// Applies a filter to a list and returned the filtered list items
public static <T> List<T> filter(List<T> list, Predicate<? super T> predicate) {
List<T> filteredList = new ArrayList<>();
for(T item : list) {
if (predicate.test(item)) {
filteredList.add(item);
}
}
return filteredList;
}
 
// Maps each item in a list to a value
public static <T, R> List<R> map(List<T> list, Function<? super T, R> mapper) {
List<R> mappedList = new ArrayList<>();
for(T item : list) {
mappedList.add(mapper.apply(item));
 
}
return mappedList;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// FunctionUtilTest.java
package com.jdojo.lambda;
 
import static com.jdojo.lambda.Gender.MALE;
import java.util.List;
public class FunctionUtilTest {
public static void main(String[] args) {
List<Person> list = Person.getPersons();
 
// Use the forEach() method to print each person in the list
System.out.println("Original list of persons:");
FunctionUtil.forEach(list, p -> System.out.println(p));
 
// Filter only males
List<Person> maleList = FunctionUtil.filter(list, p -> p.getGender() == MALE);
 
System.out.println("\nMales only:");
FunctionUtil.forEach(maleList, p -> System.out.println(p));
 
// Map each person to his/her year of birth
List<Integer> dobYearList = FunctionUtil.map(list, p -> p.getDob().getYear());
 
System.out.println("\nPersons mapped to year of their birth:");
FunctionUtil.forEach(dobYearList, year -> System.out.println(year));
 
// Apply an action to each person in the list
// Add one year to each male's dob
FunctionUtil.forEach(maleList, p -> p.setDob(p.getDob().plusYears(1)));
 
System.out.println("\nMales only after ading 1 year to DOB:");
FunctionUtil.forEach(maleList, p -> System.out.println(p));
}
}
Result
Original list of persons:
John Jacobs, MALE, 1975-01-20
Wally Inman, MALE, 1965-09-12
Donna Jacobs, FEMALE, 1970-09-12

Males only:
John Jacobs, MALE, 1975-01-20
Wally Inman, MALE, 1965-09-12

Persons mapped to year of their birth:
1975
1965
1970

Males only after ading 1 year to DOB:
John Jacobs, MALE, 1976-01-20
Wally Inman, MALE, 1966-09-12
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar