如何使用Java 8 Lambdas将对象列表转换为Map <object,object =“”>

时间:2015-08-18 10:54:33

标签: java list dictionary lambda java-8

我有对象列表说需要转换为地图的汽车。

    Public Class Car {
        private Integer carId;
        private Integer companyId;
        private Boolean isConvertible;
        private String carName;
        private String color;
        private BigDecimal wheelBase;
        private BigDecimal clearance;
    }

我有另一个对象,我想将其视为Map的关键。

    public class Key<L, C, R> {
        private L left;
        private C center;
        private R right; 
   }

我想从List of Car对象创建一个地图。

List<Car> cars;
Map<Key, Car> -> This map contains Key object created from 3 field of Car object namely carId, companyId, isConvertible.

我无法弄清楚如何使用Java 8 Lambda

来做到这一点
cars.stream.collect(Collectors.toMap(?, (c) -> c);

在上面的语句中,代替?,我想使用当前car对象中存在的值创建Key类的对象。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:10)

你可以这样做:

<div>
    <input type="checkbox" id = "Select_All" name="Main"  />Select All Information
    <table width="100%" border="1" >
    <tbody>
    <core:forEach var="mdata" items="${metaData.metaData}" varStatus="toprowCount">
    <core:if test="${toprowCount.index % 2 == 0 }"><tr></core:if>
    <td >
    <table width="100%" border="1" >
    <tr>
    <th >
    <div  class="sidebar-left sidebar-header sidebar-title"> 
    <input type="checkbox" id = "Parent_ID" name="Parent" class = "Parent_Selection" />${mdata.key}
    </div>
    </th>
    </tr>
    </tbody>
    </table>
	<table width="100%" border="1">
    <tbody>
    <core:forEach var="m" items="${mdata.value}" varStatus="rowCount"  >
    <core:if test="${rowCount.index % 3 == 0 }"><tr></core:if>
    <td>
    <input type="checkbox" id = "Child_ID"  name="Child" class="Child_Selection"/>${m} 
    </td>
    <core:if test="${rowCount.count % 3 == 0}"></tr></core:if>
    </core:forEach>
    </tbody>
    </table>
    </td>
    <core:if test="${toprowCount.count % 2 == 0}"></tr></core:if>
    </core:forEach>
    </td>
    </tr>
    </table>
</div>

答案 1 :(得分:0)

public class ImageAdapter1 extends PagerAdapter {
Context context;
Activity activity;
private static final String TAG = "Touch" ;
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF start = new  PointF();
public static PointF mid = new PointF();
private ProgressDialog pDialig;

// We can be in one of these 3 states
public static final int NONE = 0;
public static final int DRAG = 1;
public static final int ZOOM = 2;
public static int mode = NONE;

float oldDist;

private float[] matrixValues = new float[9];
private int[] GalImages = new int[] {
        R.drawable.ic_animal,
        R.drawable.ic_animal,
        R.drawable.ic_animal
};
ImageAdapter1(Context context ,Activity activity){
    this.activity = activity;
    this.context=context;
}
@Override
public int getCount() {
    return GalImages.length;
}


@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imageView = new ImageView(context);
    int padding =                                                                           context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
    imageView.setPadding(padding, padding, padding, padding);
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    imageView.setImageResource(GalImages[position]);
    ((ViewPager) container).addView(imageView, 0);
    imageView.setOnClickListener(new OnImageClickListener(position));
    imageView.setOnTouchListener(new OnImageTouch());

    return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((ImageView) object);
}

class OnImageClickListener implements View.OnClickListener {
    int _postion;

    // constructor
    public OnImageClickListener(int position) {
        this._postion = position;
    }
    @Override
    public void onClick(View v) {
        // on selecting grid view image
         // launch full screen activity
        System.out.println("Hey --->"+_postion);
        Intent i = new Intent(context, Touch.class);
        i.putExtra("position", _postion);
        context.startActivity(i);

    }
}

class OnImageTouch implements View.OnTouchListener{

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        ImageView view = (ImageView) v;
        System.out.println("1");

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:
                savedMatrix.set(matrix);
                start.set(event.getX(), event.getY());
                Log.d(TAG, "mode=DRAG");
                mode = DRAG;
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                Log.d(TAG, "oldDist=" + oldDist);
                if (oldDist > 10f) {

                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode = ZOOM;
                    Log.d(TAG, "mode=ZOOM" );
                }
                break;

            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {

                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
                }
                else if (mode == ZOOM) {

                    float newDist = spacing(event);
                    Log.d(TAG, "newDist=" + newDist);
                    if (newDist > 10f) {

                        matrix.set(savedMatrix);
                        float scale = newDist / oldDist;
                        matrix.postScale(scale, scale, mid.x, mid.y);
                    }
                }
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:

                mode = NONE;
                Log.d(TAG, "mode=NONE" );
                break;
        }

        // Perform the transformation
        view.setImageMatrix(matrix);

        return true;
    }
}

private float spacing(MotionEvent event) {
    float x = event.getX(0) - event.getX(1);
    float y = event.getY(0) - event.getY(1);
    return FloatMath.sqrt(x * x + y * y);
}

private void midPoint(PointF point, MotionEvent event) {

    float x = event.getX(0) + event.getX(1);
    float y = event.getY(0) + event.getY(1);
    point.set(x / 2, y / 2);
}}

答案 2 :(得分:0)

你也可以试试这个:

    Map<Key, Car> carMapper = new HashMap<>();

    carlist.forEach(car -> carMapper.put(new Key(car.getCarId(), car.getCompanyId(), car.getIsConvertible()), car));