合并列表<t>和列表<可选<t>&gt;

时间:2016-08-10 10:37:59

标签: java java-8 java-stream

鉴于:

user_list_id

是否有一些更好的解包方式 List<Integer> integers = new ArrayList<>(Arrays.asList( 10, 12 )); List<Optional<Integer>> optionalIntegers = Arrays.asList( Optional.of(5), Optional.empty(), Optional.of(3), Optional.of(2), Optional.empty() ); List<Integer> unwrappedOptionals = optionalIntegers.stream() .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); integers.addAll(unwrappedOptionals); 或其他方式将它们合并到Optional?在执行List<Integer>之前将它们收集到新List中感觉非常浪费。

4 个答案:

答案 0 :(得分:5)

如果您不想创建中间版List,请使用integers而不是{{1}将过滤后的元素直接添加到原始List forEach() }}:

collect()

或者,正如Sergey Lagutin建议的那样,您可以使用optionalIntegers.stream() .filter(Optional::isPresent) .map(Optional::get) .forEach(integers::add); 的{​​{1}}和Optional方法与map()

orElse()

答案 1 :(得分:5)

使用新的Java-9 Optional.stream()方法,可以这样编写:

public class Adapter extends ArrayAdapter<String> {
    private String responsedate[];
    private String responsecheckinto[];
    private String responsemessage[];
    private Activity context;
    //Context mcontext;
    ImageView pdf;
    public Adapter(Activity context, String[] responsedate, String[] responsecheckinto, String[] responsemessage) {
        super(context, R.layout.listview_response, responsedate);
        this.context = context;
        this.responsedate = responsedate;
        this.responsecheckinto = responsecheckinto;
        this.responsemessage = responsemessage;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.listview_response, null, true);
        TextView ResponseDate=(TextView)listViewItem.findViewById(R.id.txtdate);
        TextView checkito=(TextView)listViewItem.findViewById(R.id.txtcheckintoText);
        TextView message=(TextView)listViewItem.findViewById(R.id.messageText);
        TextView view=(TextView)listViewItem.findViewById(R.id.documents);
        view.setText("Document");
        ResponseDate.setText(responsedate[position]);
        checkito.setText(responsecheckinto[position]);
        message.setText(responsemessage[position]);
        pdf=(ImageView)view.findViewById(R.id.pdf);
        pdf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Adams_advice_Fragment fragment = new Adams_advice_Fragment();
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.fragment1, fragment).addToBackStack(null);
                ft.commit();
            }
        });
        return  listViewItem;
    }
}

在Java-9之前,您可以将此类方法添加到您自己的实用程序类中:

optionalIntegers.stream()
                .flatMap(Optional::stream)
                .forEach(integers::add);

并像这样使用它:

public class StreamUtil {
    public static <T> Stream<T> fromOptional(Optional<T> opt) {
        return opt.isEmpty() ? Stream.empty() : Stream.of(opt.get());
    }
}

答案 2 :(得分:2)

您可以使用ifPresent

的其他形式
ifPresent(Consumer<T>) void

使用简单的forEach,可以写:

optionalIntegers.stream().forEach(optional -> optional.ifPresent(integers::add));

答案 3 :(得分:2)

如果您想将它们合并到一个独立的List<Integer>中,您可以使用Stream::concat,如:

List<Integer> merged = Stream.concat(
         integers.stream(), 
         optionalIntegers.stream().filter(Optional::isPresent).map(Optional::get)
     ).collect(Collectors.toList());