可以一个lambda用于更改列表的价值观就地(不创建一个新的列表)? [英] Can a lambda be used to change a List's values in-place ( without creating a new list)?

查看:197
本文介绍了可以一个lambda用于更改列表的价值观就地(不创建一个新的列表)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于每按计算器 - 亮主题时,这是一个特定的编程问题,一个软件算法,有关软件工具常用的程序员和它是一种实用的,可回答的问题是唯一的软件开发。

...

我试图确定在列表使用新的lambda表达式在即将发布的功能的Java 8 而无需创建一个 **新**名单

这涉及到时候一个列表传入呼叫者,需要有一个功能适用于所有的内容更改为新的值。例如,顺便 Col​​lections.sort(名单)改变就地列表。

什么是给这个转换功能最简单的方法,而这个首发名单,以功能应用到原始列表中的所有值未做一个新的列表?

 字符串函数(String s)将{
    返回s.toUppercase(); //或别的东西更复杂
}

名单<字符串>表= Arrays.asList(鲍勃,史蒂夫,吉姆,Arbby);
 

申请更改就地所有值通常使用的方法是这样的:

 的for(int i = 0; I<则为list.size();我++){
    list.set(ⅰ,功能(list.get(ⅰ));
}
 

请问lambda表达式和Java 8报价:

  • 在一个更方便,更EX pressive方式?
  • 办法做到这一点没有建立的(..)循环的所有脚手架?
解决方案

 名单,其中,字符串>表= Arrays.asList(鲍勃,史蒂夫,吉姆,Arbby);
    list.replaceAll(字符串::与toUpperCase);
 

As per as per StackOverflow-On Topic, this is a specific programming problem, a software algorithm, about software tools commonly used by programmers and it is a practical, answerable problems that is unique to software development.

...

I am trying to determine the correct way of changing all the values in a List using the new lambdas feature in the upcoming release of Java 8 without creating a **new** List.

This pertains to times when a List is passed in by a caller and needs to have a function applied to change all the contents to a new value. For example, the way Collections.sort(list) changes a list in-place.

What is the easiest way given this transforming function, and this starting list, to apply the function to all the values in the original list without making a new list?

String function(String s){ 
    return s.toUppercase(); // or something else more complex
}

List<String> list = Arrays.asList("Bob", "Steve", "Jim", "Arbby");

The usual way of applying a change to all the values in-place was this:

for (int i = 0; i < list.size(); i++) {
    list.set(i, function( list.get(i) );
}

Does lambdas and Java 8 offer:

  • an easier and more expressive way?
  • a way to do this without setting up all the scaffolding of the for(..) loop?

解决方案

    List<String> list = Arrays.asList("Bob", "Steve", "Jim", "Arbby");
    list.replaceAll(String::toUpperCase);

这篇关于可以一个lambda用于更改列表的价值观就地(不创建一个新的列表)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆