Инициализировать List ClothesList ; ЯваJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Инициализировать List ClothesList ; Ява

Сообщение Anonymous »

У меня 3 шкафа, в каждый из них я добавила немного одежды
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 clothesList = new ArrayList(); // is an empty array lsit
List wardrobeList = new ArrayList(Arrays.asList(wardrobe, wardrobe1, wardrobe2));

Map numberClothesPerType = getNumberClothesPerTypeClothes(clothesList);
System.out.println("Number Clothes per Type: " + numberClothesPerType);

}

Поэтому я хочу получить количество по типу такой одежды
public static Map getNumberClothesPerTypeClothes(List clothesList) {
return clothesList.stream()
.collect(Collectors.groupingBy(Clothes::getTypeClothes, Collectors.counting()));
}

Мой вывод верен:
Number Clothes per Type: {blouse=4, pants=2, shoes=1, t_shirt=3, vest=2}

Но поскольку я не хочу, чтобы мой список одежды выглядел так, а скорее так, как я это сделал выше:
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 clothesList = new ArrayList(Arrays.asList(clothes, clothes1, clothes2, clothes3, clothes4, clothes5, clothes6, clothes7, clothes8, clothes9, clothes10, clothes11));

Как мне это сделать, чтобы получить список одежды из тех, которые я добавил в гардероб, но не создавать одежду, а затем добавлять, а только из тех, которые я добавил?
Добавление класса гардероба:
import java.util.ArrayList;
import java.util.List;

public class Wardrobe {

List 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();
}
}


Подробнее здесь: https://stackoverflow.com/questions/718 ... slist-java
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»