标签: toarray

将 Json 转换为 DTO 数组

我有一个有趣的 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)

java json dto jackson toarray

5
推荐指数
1
解决办法
4万
查看次数

为什么没有toArray(Class <T>)?

为什么没有仅接受类型的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)

但是,当我只想指定类型而不创建不必要的一次性数组时,创建一个空数组对我来说似乎效率低下并且违反直觉。

java arrays collections toarray

5
推荐指数
1
解决办法
93
查看次数

哪种 toArray 转换模式最适合使用?

我有以下一段 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)由流去,并使用toArrayCollector

toArray(new String[0])仍然流中使用?它仍然是一个性能更高/更好的选择吗?

java arrays toarray java-8 java-stream

5
推荐指数
1
解决办法
153
查看次数

转换对象[]到int []错误

可能重复:
如何在Java中将List <Integer>转换为int []?

我有一个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)

你对此有什么想法吗?

java arraylist type-conversion toarray

3
推荐指数
1
解决办法
6246
查看次数

为toArray方法创建通用数组

我有以下方法:

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的通用数组",这很明显.我该如何解决这个问题?

java generics toarray

3
推荐指数
1
解决办法
620
查看次数

MongoDB toArray性能

我正在尝试从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).

mongodb node.js toarray

3
推荐指数
1
解决办法
1072
查看次数

Laravel Scout toSearchableArray() 没有被调用?

我使用 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)

arrays collections toarray laravel laravel-scout

3
推荐指数
1
解决办法
3544
查看次数

ArrayList无法使用(String [])list.toArray()转换为String数组.为什么?

为什么以下代码无法执行,尽管它不会从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;

有人能说清楚吗?之前已经提出过同样的问题,并提供了一些解决方案.但是对这个问题的明确解释将非常感激.

java string arraylist toarray

2
推荐指数
1
解决办法
1182
查看次数

为什么WhereSelectArrayIterator没有实现ICollection?

在通过Reflector 查看System.Linq.Enumerable时,我注意到用于SelectWhere扩展方法的默认迭代器- 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)

c# performance list icollection toarray

1
推荐指数
1
解决办法
1445
查看次数

Java 中的“new Boolean[0]”或“new String[0]”计算结果是什么

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)

java arrays toarray

1
推荐指数
1
解决办法
989
查看次数