I have 3 wardrobes , in each of them I added some clothes
public static void main(String[] args) {
Wardrobe wardrobe = new Wardrobe("LivingRoom", TypeCloset.LivingRoom);
wardrobe.add(new Clothes(TypeClothes.blouse, 95, "Patagonia"));
wardrobe.add(new Clothes(TypeClothes.vest, 15, "Zara"));
wardrobe.add(new Clothes(TypeClothes.pants, 125, "Adidas"));
wardrobe.add(new Clothes(TypeClothes.t_shirt, 55, "Nike"));
Wardrobe wardrobe1 = new Wardrobe("BedRoom", TypeCloset.BedRoom);
wardrobe1.add(new Clothes(TypeClothes.shoes, 120, "Nike"));
wardrobe1.add(new Clothes(TypeClothes.blouse, 19, "Nina"));
wardrobe1.add(new Clothes(TypeClothes.vest, 5, "China"));
wardrobe1.add(new Clothes(TypeClothes.t_shirt, 10, "Legend"));
wardrobe1.add(new Clothes(TypeClothes.blouse, 80, "Patagonia"));
Wardrobe wardrobe2 = new Wardrobe("Outdoor", TypeCloset.Outdoor);
wardrobe2.add(new Clothes(TypeClothes.t_shirt, 250, "Verse"));
wardrobe2.add(new Clothes(TypeClothes.pants, 700, "Louis"));
wardrobe2.add(new Clothes(TypeClothes.blouse, 360, "Balancing"));
List<Clothes> clothesList = new ArrayList<>(); // is an empty array lsit
List<Wardrobe> wardrobeList = new ArrayList<>(Arrays.asList(wardrobe, wardrobe1, wardrobe2));
Map<TypeClothes, Long> numberClothesPerType = getNumberClothesPerTypeClothes(clothesList);
System.out.println("Number Clothes per Type: " numberClothesPerType);
}
So I want to get quantity per typeClothes like this
public static Map<TypeClothes, Long> getNumberClothesPerTypeClothes(List<Clothes> clothesList) {
return clothesList.stream()
.collect(Collectors.groupingBy(Clothes::getTypeClothes, Collectors.counting()));
}
My output is correct :
Number Clothes per Type: {blouse=4, pants=2, shoes=1, t_shirt=3, vest=2}
But since I do not want to make my clothesList looking like this but rather like I have done it above:
Clothes clothes = new Clothes(TypeClothes.blouse, 95, "Patagonia");
Clothes clothes1 = new Clothes(TypeClothes.vest, 15, "Zara");
Clothes clothes2 = new Clothes(TypeClothes.pants, 125, "Adidas");
Clothes clothes3 = new Clothes(TypeClothes.t_shirt, 55, "Nike");
Clothes clothes4 = new Clothes(TypeClothes.blouse, 19, "Nina");
Clothes clothes5 = new Clothes(TypeClothes.vest, 5, "China");
Clothes clothes6 = new Clothes(TypeClothes.t_shirt, 10, "Legend");
Clothes clothes7 = new Clothes(TypeClothes.blouse, 80, "Patagonia");
Clothes clothes8 = new Clothes(TypeClothes.t_shirt, 250, "Verse");
Clothes clothes9 = new Clothes(TypeClothes.pants, 700, "Louis");
Clothes clothes10 = new Clothes(TypeClothes.blouse, 360, "Balancing");
Clothes clothes11 = new Clothes(TypeClothes.shoes, 120, "Nike");
List<Clothes> clothesList = new ArrayList<>(Arrays.asList(clothes, clothes1, clothes2, clothes3, clothes4, clothes5, clothes6, clothes7, clothes8, clothes9, clothes10, clothes11));
How should I do it to get the list of clothes from the ones I added to the wardrobe but not having to create clothes and then add, so just from the ones I added?
Adding wardrobe class:
import java.util.ArrayList;
import java.util.List;
public class Wardrobe {
List<Clothes> clothes = new ArrayList<>();
private String name;
private TypeCloset typeCloset;
public Wardrobe(String name, TypeCloset typeCloset) {
this.name = name;
this.typeCloset = typeCloset;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TypeCloset getType() {
return typeCloset;
}
public void setType(TypeCloset typeCloset) {
this.typeCloset = typeCloset;
}
@Override
public String toString() {
return "Wardrobe{"
"name='" name '\''
", typeCloset=" typeCloset
'}';
}
public void add(Clothes newClothes) {
clothes.add(newClothes);
}
public int getCount() {
return this.clothes.size();
}
public Integer getPricePerCloset() {
return this.clothes.stream()
.mapToInt(Clothes::getPrice)
.sum();
}
}
CodePudding user response:
If you have implemented a get method in your Wardrobe (which you should provide the definition) like:
public list<Clothes> getClothes()
From the list of wardrobes, you can do
wardrobesList.stream().flatMap(wardrobe -> wardrobe.get().stream()).collect(Collectors.toList())
to get a list of all the Clothes (classes) you have set in the wardrobes
CodePudding user response:
This depends on the implementation of your Wardrobe class. If the wardrobe.add method just adds the clothes to a List in wardrobe i would suggest for java 8:
List<Clothes> clothesList = Arrays.asList(wardrobe.getClothes(), wardrobe1.getClothes(), wardrobe2.getClothes())
.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
or for java 11 and higher
List<Clothes> clothesList = List.of(wardrobe.getClothes(),wardrobe1.getClothes(),wardrobe2.getClothes())
.stream()
.flatMap(List::stream)
.collect(Collectors.toList());