我有一个有趣的 JSON 解析问题,至少对我来说是这样,因为我是第一次这样做。我有以下示例 JSON,我想将其映射到等效的 DTO:
{
"modules":
[
{
"name":"module1",
"shortId":23425,
"pmns":
[
{
"name":"pmn1",
"position":1,
"pmnType":"D3"
},
{
"name":"pmn3",
"position":3,
"pmnType":"R2"
},
{
"name":"pmn7",
"position":5,
"pmnType":"S1"
},
]
},
{
"name":"module2",
"shortId":1572,
"pmns":
[
{
"name":"pmn1",
"position":3,
"pmnType":"D3"
},
{
"name":"pmn12",
"position":35,
"pmnType":"R2"
},
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
这是我的 ModuleDTO 类:
public class ModuleDTO {
private String _name;
private short _shortId;
private PmnDTO[] _pmns;
public String getName() {
return _name;
}
public short getShortId() {
return _shortId;
}
public …Run Code Online (Sandbox Code Playgroud) 为什么没有仅接受类型的toArray变量List,例如:
Foo[] array = list.toArray(Foo.class);
// or
Foo[] array = list.toArray(Foo[].class);
Run Code Online (Sandbox Code Playgroud)
我见过
// existing array
Foo[] array = list.toArray(array);
// Fake array
Foo[] array = list.toArray(new Foo[0]);
Run Code Online (Sandbox Code Playgroud)
但是,当我只想指定类型而不创建不必要的一次性数组时,创建一个空数组对我来说似乎效率低下并且违反直觉。
我有以下一段 IntelliJ 建议我应该更改的代码:
String[] normalizedNames = rawToNormalized.values().stream().toArray(String[]::new);
Run Code Online (Sandbox Code Playgroud)
进入
String[] normalizedAliases = rawToNormalized.values().toArray(new String[0]);
Run Code Online (Sandbox Code Playgroud)
Aleksey Shipil?v 的帖子 ( https://shipilev.net/blog/2016/arrays-wisdom-ancients/ ) 建议:
toArray(new T[0]) 看起来更快、更安全、更干净,因此现在应该是默认选择。”
可有人请详细说明究竟是如何toArray(new String[0])不同于toArray(String[]::new)由流去,并使用toArray无Collector。
可toArray(new String[0])仍然流中使用?它仍然是一个性能更高/更好的选择吗?
我有一个ArrayList,当我尝试将其转换为一个整数数组,因为我需要做涉及整数的操作,我得到这个错误:
需要不兼容的类型:
找到int [] :java.lang.Object []
这是我的代码:
List<Integer> ids_rdv = new ArrayList<Integer>();
// I execute an SQL statement, then :
while (resultat.next()) {
ids_rdv.add(resultat.getInt("id_rdv"));
}
int[] element_rdv_id = ids_rdv.toArray(); //Problem
Run Code Online (Sandbox Code Playgroud)
你对此有什么想法吗?
我有以下方法:
public static <T, U> T[] getKeysForValue(Map<T,U> map,U value){
if(map == null || map.isEmpty()) {
return null;
}
Set<T> keys = new HashSet<T>();
for (Map.Entry<T,U> entry : map.entrySet()) {
if (entry.getValue().equals(value)) {
keys.add(entry.getKey());
}
}
return keys.toArray(new T[keys.size()]);
}
Run Code Online (Sandbox Code Playgroud)
我在线路上遇到编译错误:keys.toArray(new T[keys.size()])"无法创建T的通用数组",这很明显.我该如何解决这个问题?
我正在尝试从Mongo/Node中的术语集合构建类别树,但首先我使用$ in选择所有树元素:
console.time('termsCol.find');
var terms = await termsCol.find({term_id: {'$in': flatTree}});
console.timeEnd('termsCol.find');
console.time('termsCol.toArray');
terms = await terms.toArray();
console.timeEnd('termsCol.toArray');
Run Code Online (Sandbox Code Playgroud)
这表现:
termsCol.find: 0.162ms
termsCol.toArray: 30.910ms
Run Code Online (Sandbox Code Playgroud)
我已经阅读了有关SO上的Array性能的帖子,但想知道是否有任何变化,因为这会占用我在页面请求期间的大部分时间.
我有一个关于该集合的索引,它在~0.15ms内返回300个术语,但是当我不得不等待另外30ms在js中进一步使用该数据时,这对我没有帮助.
如果没有办法改进这个toArray业务,我可能会创建一个缓存集合并在那里存储完整的术语树(希望它们适合16MB).
我使用 Laravel Scout 和 TNTSearch 在我的模型之一(食谱)上使用了工作搜索功能:teamtnt/laravel-scout-tntsearch-driver。
我想将相同的搜索功能添加到不同的模型(成分)。我试图通过使用将搜索结果作为数组返回toSearchableArray().。为了进行测试,我在模型中执行了以下操作。
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Ingredient extends Model
{
use Searchable;
public $asYouType = true;
public function recipes()
{
return $this->belongsToMany('App\Recipe');
}
public function toSearchableArray()
{
$array = $this->toArray();
return $array;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我正在尝试这样做:
public function search(Request $request)
{
$results = Ingredient::search($request->q)->get()->toArray();
return $results;
}
Run Code Online (Sandbox Code Playgroud)
但是,我仍然将我的数据作为集合返回。我正在为我的其他模型(Recipe)使用类似的设置,它确实返回了预期的结果数组。
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Recipe extends Model
{
use Searchable;
public $asYouType = true;
public function ingredients()
{ …Run Code Online (Sandbox Code Playgroud) 为什么以下代码无法执行,尽管它不会从IDE检测到错误.它会编译好.
ArrayList<String> a = new ArrayList<String>();
a.add("one");
a.add("two");
a.add("three");
String [] b = (String[])a.toArray();
for(int i =0;i<b.length;++i){
System.out.println(b[i]);
}
Run Code Online (Sandbox Code Playgroud)
但它会给出以下错误.
嵌套异常是java.lang.ClassCastException:[Ljava.lang.Object; 无法转换为[Ljava.lang.String;
有人能说清楚吗?之前已经提出过同样的问题,并提供了一些解决方案.但是对这个问题的明确解释将非常感激.
在通过Reflector 查看System.Linq.Enumerable时,我注意到用于Select和Where扩展方法的默认迭代器- WhereSelectArrayIterator - 没有实现ICollection接口.如果我正确读取代码,这会导致一些其他扩展方法,如Count()和ToList()执行较慢:
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
{
// code above snipped
if (source is List<TSource>)
{
return new WhereSelectListIterator<TSource, TResult>((List<TSource>) source, null, selector);
}
// code below snipped
}
private class WhereSelectListIterator<TSource, TResult> : Enumerable.Iterator<TResult>
{
// Fields
private List<TSource> source; // class has access to List source so can implement ICollection
// code below snipped
}
public class …Run Code Online (Sandbox Code Playgroud) new Boolean[0]Java 中的or求值是什么new String[0]?
为什么我们需要一个号码?
以下哪项是正确的方法?
myList.toArray(new Boolean[0])
Run Code Online (Sandbox Code Playgroud)
或者
myList.toArray(new Boolean[myList.size()])
Run Code Online (Sandbox Code Playgroud) toarray ×10
java ×7
arrays ×4
arraylist ×2
collections ×2
c# ×1
dto ×1
generics ×1
icollection ×1
jackson ×1
java-8 ×1
java-stream ×1
json ×1
laravel ×1
list ×1
mongodb ×1
node.js ×1
performance ×1
string ×1