Java 从列表中对象的属性创建列表

Java 从列表中对象的属性创建列表,java,list,Java,List,我希望我的标题能更简洁,但我不太确定如何描述我想做什么。我有一个列表,其中包含我创建的类的许多对象。每个对象都有许多属性,可由一些getter和setter访问。我想为每个属性创建一个列表,获取每个对象的属性并将它们放入各自的列表中 我目前正在使用以下代码执行此操作: ArrayList<Integer> counts = new ArrayList<Integer>(); ArrayList<String> colors = new ArrayList<

我希望我的标题能更简洁,但我不太确定如何描述我想做什么。我有一个列表,其中包含我创建的类的许多对象。每个对象都有许多属性,可由一些getter和setter访问。我想为每个属性创建一个列表,获取每个对象的属性并将它们放入各自的列表中

我目前正在使用以下代码执行此操作:

ArrayList<Integer> counts = new ArrayList<Integer>();
ArrayList<String> colors = new ArrayList<String>();
ArrayList<String> shapes = new ArrayList<String>();
ArrayList<String> fills = new ArrayList<String>();

for (Card card: cards) {
    counts.add(card.getCount());
    colors.add(card.getColor());
    shapes.add(card.getShape());
    fills.add(card.getFill());
}
ArrayList counts=new ArrayList();
ArrayList colors=新的ArrayList();
ArrayList形状=新的ArrayList();
ArrayList fills=新的ArrayList();
用于(卡片:卡片){
counts.add(card.getCount());
添加(card.getColor());
shapes.add(card.getShape());
fills.add(card.getFill());
}

但我想知道是否有一个更短,也许更好的方法来做到这一点?还是我已经在做“正确的”了?谢谢

你的方式看起来不错。如果您使用Java8流,那么您可以用更少的代码行编写它,但这将需要在
卡片
列表上进行4次迭代,而不是当前的单个迭代

例如,要创建计数列表,可以编写:

List<Integer> counts = cards.stream().map(Card::getCount).collect(Collectors.toList());
List counts=cards.stream().map(Card::getCount).collect(Collectors.toList());
如果您正在开发,以下是检查和完成卡集的解决方案:

Card.java:

import java.util.Locale;

public final class Card {
    public static void main(String[] args) {
        Card a = new Card(Count.ONE, Color.RED, Fill.STRIPED, Shape.DIAMOND);
        Card b = new Card(Count.TWO, Color.RED, Fill.SOLID, Shape.DIAMOND);
        Card c = completeSet(a, b);
        System.out.println(c);
    }

    public int count;
    public int color;
    public int fill;
    public int shape;

    public Card() {

    }

    public Card(Count count, Color color, Fill fill, Shape shape) {
        setCount(count);
        setColor(color);
        setFill(fill);
        setShape(shape);
    }

    // getters and setters to operate with the attribute enumerations
    public Count getCount() {
        return Count.values()[count];
    }

    public Color getColor() {
        return Color.values()[color];
    }

    public Shape getShape() {
        return Shape.values()[shape];
    }

    public Fill getFill() {
        return Fill.values()[fill];
    }

    public void setCount(Count count) {
        this.count = count.ordinal();
    }

    public void setColor(Color color) {
        this.color = color.ordinal();
    }

    public void setShape(Shape shape) {
        this.shape = shape.ordinal();
    }

    public void setFill(Fill fill) {
        this.fill = fill.ordinal();
    }

    public static int completeAttribute(int a, int b) {
        if (a == b) {
            // attribute for each same
            return a;
        } else {
            // attribute for each different
            int c;
            for (c = 0; c < 3; c++) {
                if (c != a && c != b) {
                    break;
                }
            }
            return c;
        }
    }

    public static Card completeSet(Card a, Card b) {
        // determine missing card to make a set
        Card result = new Card();
        result.count = completeAttribute(a.count, b.count);
        result.color = completeAttribute(a.color, b.color);
        result.shape = completeAttribute(a.shape, b.shape);
        result.fill = completeAttribute(a.fill, b.fill);
        return result;
    }

    public static boolean isSet(Card a, Card b, Card c) {
        // check if it is a set by completing it
        return completeSet(a, b).equals(c);
    }

    @Override
    public boolean equals(Object that) {
        // currently unused
        if (this == that) {
            return true;
        }
        return that != null && that instanceof Card && this.equals((Card) that);
    }

    public boolean equals(Card that) {
        if (this == that) {
            return true;
        }
        return that != null
                && this.color == that.color
                && this.count == that.count
                && this.fill == that.fill
                && this.shape == that.shape;

    }

    @Override
    public int hashCode() {
        // currently unused
        int result = count;
        result = 31 * result + color;
        result = 31 * result + shape;
        result = 31 * result + fill;
        return result;
    }

    @Override
    public String toString() {
        // pretty print attributes
        String result = getCount() + " " + getColor() + " " + getFill() + " " + getShape();
        result = result.toLowerCase(Locale.ROOT);
        if(getCount() != Count.ONE) {
            result += "s";
        }
        return result;
    }

    // enumerations for pretty names
    public enum Count {
        ONE,
        TWO,
        THREE
    }

    public enum Color {
        RED,
        GREEN,
        VIOLET
    }

    public enum Shape {
        ELLIPSE,
        DIAMOND,
        TWIRL
    }

    public enum Fill {
        OPEN,
        STRIPED,
        SOLID
    }
}
import java.util.Locale;
公开期末考试卡{
公共静态void main(字符串[]args){
卡a=新卡(计数1、颜色、红色、填充、条纹、形状、菱形);
卡片b=新卡片(计数2,颜色.红色,填充.实心,形状.菱形);
卡c=完整集(a,b);
系统输出打印ln(c);
}
公共整数计数;
公共int颜色;
公共整数填充;
公共int形状;
公共卡(){
}
公共卡(计数、颜色、填充、形状){
设置计数(计数);
设置颜色(颜色);
设置填充(填充);
设置形状(形状);
}
//使用属性枚举操作的getter和setter
公共计数getCount(){
返回Count.values()[Count];
}
公共颜色getColor(){
返回Color.values()[Color];
}
公共形状getShape(){
返回Shape.values()[Shape];
}
公共填充getFill(){
返回Fill.values()[Fill];
}
公共无效集合计数(计数){
this.count=count.ordinal();
}
公共空间设置颜色(颜色){
this.color=color.ordinal();
}
公共空间设置形状(形状){
this.shape=shape.ordinal();
}
公共空白设置填充(填充){
this.fill=fill.ordinal();
}
公共静态int completeAttribute(int a,int b){
如果(a==b){
//每个相同属性的属性
返回a;
}否则{
//为每个不同的
INTC;
对于(c=0;c<3;c++){
如果(c!=a&&c!=b){
打破
}
}
返回c;
}
}
公共静态卡完整集(卡a、卡b){
//确定丢失的卡以制作一套
卡片结果=新卡片();
result.count=completeAttribute(a.count,b.count);
result.color=完整属性(a.color,b.color);
result.shape=完整属性(a.shape,b.shape);
result.fill=完整属性(a.fill,b.fill);
返回结果;
}
公共静态布尔isSet(卡a、卡b、卡c){
//通过完成它来检查它是否是一个集合
返回完整集(a,b)。等于(c);
}
@凌驾
公共布尔等于(该对象){
//当前未使用
如果(这个==那个){
返回true;
}
返回该值!=null&&this.equals((卡片)that);
}
公共布尔等于(卡片){
如果(这个==那个){
返回true;
}
返回该值!=null
&&this.color==that.color
&&this.count==that.count
&&this.fill==that.fill
&&this.shape==that.shape;
}
@凌驾
公共int hashCode(){
//当前未使用
int结果=计数;
结果=31*结果+颜色;
结果=31*结果+形状;
结果=31*结果+填充;
返回结果;
}
@凌驾
公共字符串toString(){
//漂亮的打印属性
字符串结果=getCount()+“”+getColor()+“”+getFill()+“”+getShape();
result=result.toLowerCase(Locale.ROOT);
如果(getCount()!=Count.ONE){
结果+=“s”;
}
返回结果;
}
//漂亮名字的枚举
公共枚举计数{
一,,
二,,
三
}
公共枚举颜色{
红色
绿色
紫罗兰色
}
公共枚举形状{
椭圆,
钻石,
旋转
}
公共枚举填充{
打开
条纹的,
固体
}
}


输出:
三颗红方块

我很好奇:你为什么要这样做?为什么将属性存储在多个列表中比将包含属性的对象存储在一个列表中要好?也许有更好的解决方案来解决我想做的事情。我想检查每个属性列表是否包含完全不同的元素,或者完全相同的元素。看起来您需要开发一个作为家庭作业的属性列表…不完全相同。我正在使用OpenCV制作一个集合解算器(如果你不知道,它是一个计算机视觉库)。我想如果我能把手机摄像头对准一张卡片桌,马上就能找到解决方案,那就太酷了。这正是我一直在寻找的东西。我可能不会使用这段代码,因为它不太容易阅读,但很高兴知道您可以用另一种方法来完成它。@TobLoef