简体   繁体   English

是JDK 1.6中的IntStream类?

[英]is IntStream class in JDK 1.6?

Is there any other class which and be use like java.util.stream.IntStream (of java 8) in java 6 还有其他任何可以使用的类,例如java 6中的java.util.stream.IntStream (java 8)

I wanted to achieve 我想实现

  List<Integer> list = IntStream.of(intArray).boxed().collect(Collectors.toList());

how it is possible in java 6 or below 在Java 6或更低版本中怎么可能

No, there's no simple way to do this in JDK6, that's exactly why they added it in JDK8. 不,在JDK6中没有简单的方法可以做到这一点,这正是他们在JDK8中添加它的原因。

You can either: 您可以:

  1. iterate over the int array and add the numbers one by one to the new collection 遍历int数组并将数字一一添加到新集合中

     int[] intArray = { 1,2,3 }; List<Integer> ints = new ArrayList<>(intArray.length); for (int i : intArray) { ints.add(i); } 
  2. or you can reach out for a library to do this for you. 或者您可以联系图书馆来为您完成此操作。 For example, Guava does this pretty well: Ints.asList() 例如,番石榴做得很好: Ints.asList()

If all you want is to convert an int[] into an List<Integer> 如果只需要将int[]转换为List<Integer>

you can either: 您可以:

create your own ArrayList and add the elements manually 创建自己的ArrayList并手动添加元素

    List<Integer> list = new ArrayList<Integer>(intArray.length);
    for(int value : intArray){
        list.add(value);
    }

Or you can easily write your own class that implements List using an primitive array backing: 或者,您可以使用原始数组支持轻松编写自己的实现List的类:

public final class IntArrayList extends AbstractList<Integer> implements RandomAccess {

    private final int[] array;


    public IntArrayList(int[] array) {
        this.array = array;
    }

    @Override
    public Integer get(int index) {
        return array[index];
    }

    @Override
    public int size() {
        return array.length;
    }

    @Override
    public Integer set(int index, Integer element) {
        Integer old = array[index];
        array[index] = element.intValue();
        return old;
    }

    public int indexOf(Object o) {
       if(o==null){
           return -1;
       }
       if(!(o instanceof Integer)){
           return -1;
       }
       int val = ((Integer)o).intValue();

        for (int i=0; i<array.length; i++){
            if (val==array[i]){
                return i;
            }
        }
        return -1;
    }

    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }
    /**
     * Optimization of equals since
     * we know we are have an array of ints
     * this should reduce boxing/unboxing
     * on our end at least.
     */
    @SuppressWarnings("rawtypes")
    public boolean equals(Object o) {
        if (o == this){
            return true;
        }
        if (!(o instanceof List)){
            return false;
        }

        int currentOffset=0;
        ListIterator e2 = ((List) o).listIterator();
        while(currentOffset<array.length && e2.hasNext()) {
            Object o2 = e2.next();
            //will return false if o2 is null
            if(!(o2 instanceof Integer)){
                return false;
            }
            if(array[currentOffset] !=((Integer)o2).intValue()){
                return false;
            }
            currentOffset++;
        }
        return !(currentOffset<array.length || e2.hasNext());
        }

        /**
         * Optimization of hashcode since
         * we know we have an array of ints.
         */
        public int hashCode() {
            return Arrays.hashCode(array);
        }

}

And then it's just 然后就是

 List<Integer> list = new IntArrayList(intArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM