我想把一个人的名单分组.一个人有一些属性,如姓名,国家,城镇,邮政编码等.我写了静态代码,非常有效:
Object groupedData = data.stream().collect(groupingBy(Person::getName, Collectors.groupingBy(Person::getCountry, Collectors.groupingBy(Person::getTown))));
Run Code Online (Sandbox Code Playgroud)
但问题是,这不是动态的.有时我想按名称和城镇分组,有时候按属性分组.我怎样才能做到这一点?非Java 8解决方案也是受欢迎的.
我试图通过使用Java 8 Collection-Stream按多个属性对对象列表进行分组.
这非常有效:
public class MyClass
{
public String title;
public String type;
public String module;
public MyClass(String title, String type, String module)
{
this.type = type;
this.title = title;
this.module= module;
}
}
List<MyClass> data = new ArrayList();
data.add(new MyClass("1","A","B"));
data.add(new MyClass("2","A","B"));
data.add(new MyClass("3","A","C"));
data.add(new MyClass("4","B","A"));
Object result = data.stream().collect(Collectors.groupingBy((MyClass m)
-> m.type, Collectors.groupingBy((MyClass m) -> m.module)));
Run Code Online (Sandbox Code Playgroud)
但我想让它更有活力.我只想指定一个应该用于GroupBy的String-Array(或List).
就像是:
Object groupListBy(List data, String[] groupByFieldNames)
{
//magic code
}
Run Code Online (Sandbox Code Playgroud)
我想打电话给:
groupListBy(data, new String[]{"type","module"});
Run Code Online (Sandbox Code Playgroud)
如何使groupBy-Method更具动态性,就像在我的例子中一样?
我正在从 PHP 7.4 升级到 PHP 8,突然这个错误出现在我的 cURL 请求中:
Fatal error: Uncaught ValueError: curl_setopt_array(): Argument #2 ($options) contains an invalid cURL option in ...
Run Code Online (Sandbox Code Playgroud)
我使用以下代码来构建curl:
$options = array (
"CURLOPT_POST" => true,
"CURLOPT_HEADER" => true,
"CURLOPT_URL" => "https://example.example.com/api/example.php",
"CURLOPT_FRESH_CONNECT" => true,
"CURLOPT_RETURNTRANSFER" => true,
"CURLOPT_FORBID_REUSE" => true,
"CURLOPT_TIMEOUT" => 10,
"CURLOPT_FAILONERROR" => true,
"CURLOPT_POSTFIELDS" => $this->buildPostFields($postData),
"CURLOPT_HTTPAUTH" => "CURLAUTH_BASIC",
"CURLOPT_SSL_VERIFYPEER" => false //REMOVE IN PRODUCTION, IGNORES SELFSIGNED SSL
);
$ch = curl_init();
curl_setopt_array($ch, $options);
Run Code Online (Sandbox Code Playgroud)
目标文件始终是 php 扩展名。'buildPostFields' 返回数据数组。
可能出现此错误是因为我的 php 升级到版本 …
grouping ×2
java ×2
java-8 ×2
java-stream ×2
collections ×1
collectors ×1
curl ×1
php ×1
php-8 ×1