Collection in Java

Quick question ... Can collections in Java store multiple types? Or should they all be of the same type?

thank

+3
source share
9 answers

Simple answer

Yes.

More detailed answer

You can use a generic collection without a value <T>, for example:

ArrayList a = new ArrayList();
a.add(2);
a.add("String");

Using collections without <T>is a bad habit, and most IDEs / compilers give a warning here. You can get around it using the collection Object, i.e.:

ArrayList<Object> a = new ArrayList<Object>();

, , ArrayList<Number>, Number, BigDecimal, BigInteger, Byte, Double, Float, , , :

ArrayList<Number> a = new ArrayList<Number>();
a.add(2); // integer
a.add(42L); // long
a.add(123.45d); // double
System.out.println(a.toString()); // => [2, 42, 123.45]

, , a Number - .. - (, # isInfinite(), Number ), typecast , - typecast:

a.get(2).isInfinite()          // compile-time error
((Double) a.get(2)).isInfinite() // => false
((Double) a.get(1)).isInfinite() // run-time error (ClassCastException)

, , , .

, ArrayList<Integer> , ( ) ArrayList<Number>, .. :

public void printNumbers(ArrayList<Number> list) {
    list.forEach(System.out::println);
}
ArrayList<Integer> a = new ArrayList<Integer>();
printNumbers(a); // "incompatible types"

:

public void printIntegers(ArrayList<Integer> list) {
    list.forEach(System.out::println);
}
ArrayList<Number> a = new ArrayList<Number>();
printIntegers(a); // "incompatible types"

, ArrayList<Number>, , ArrayList<? extends Number> ArrayList<? super Number>. extends , (.. ) , super , (.. ). , , extends:

public void printNumbers(ArrayList<? extends Number> list) {
    list.forEach(System.out::println);
}

ArrayList<Integer> listInt = new ArrayList<Integer>();
printNumbers(listInt); // works
ArrayList<Double> listDbl = new ArrayList<Double>();
printNumbers(listDbl); // also works

<? T > < T > Java .

+7

, , Collection<Object>. , , , if (x instanceof MyType), .

+2

. , A, Collection<A> A A.

, Collection<Object>, .

, (Object) , . typeof, , .

+2

Collection , TreeSet TreeMap. TreeSet TreeMap . , , , .

+1

, , (, 5- jdk), , .

0

java , . , .

    ArrayList al = new ArrayList();
    al.add(1);
    al.add("name");
    al.add(1.2f);

    Iterator itr =al.iterator();
    while(itr.hasNext())
    {
        System.out.println(itr.next());
    }

, , . , , .

    ArrayList<Integer> al = new ArrayList<Integer>();
    al.add(1);
    al.add(2);
    al.add(3);

    Iterator itr =al.iterator();
    while(itr.hasNext())
    {
        System.out.println(itr.next());
    }

    ArrayList<String> al1 = new ArrayList<String>();
    al1.add("Words");
    al1.add("Names");
    al1.add("Characters");

    Iterator itr1 =al1.iterator();
    while(itr1.hasNext())
    {
        System.out.println(itr1.next());
    }

, .

0

,

- ,

ArrayList<Elements>()=new ArrayList();

ArrayList<E>()=new ArrayList();

, Generics in Collection.

class Test
{
   public static void main(String[] args)
   {
      // For Generic class of List
      ArrayList<E> arrL1 = new ArrayList<E>();
      arrL1.add("stackoverflow");
      arrL1.add(1);   
      Iterator itr1=list.iterator();

     while(itr1.hasNext())
     {
        System.out.println(itr1.next());
     }


  // for Particular datatype in List
    ArrayList<String> list=new ArrayList<String>(); // Creating arraylist
    list.add("Ravi"); // Adding object in arraylist
    list.add("Vijay");
    list.add("Ravi");
    list.add("Ajay");

    // transversing the values
    Iterator itr=list.iterator();
    while(itr.hasNext())
    {
        System.out.println(itr.next());
    }
}

}

1

stackoverflow

1

2

Ravi

Vijay

Ravi 

Ajay
0

, Collection<?>.

-1

, ArrayList .

class Test
{
    public static void main(String[] args)
    {
        // For Generic class of List
        ArrayList<> arrL1 = new ArrayList<>();
        arrL1.add("stackoverflow");
        arrL1.add(1);

        // for Particular datatype in List
        ArrayList<String> list=new ArrayList<String>(); // Creating arraylist
        list.add("Ravi"); // Adding object in arraylist
        list.add("Vijay");
        list.add("Ravi");
        list.add("Ajay");

        // transversing the values
        Iterator itr=list.iterator();
        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}

1:

StackOverflow

1

2:

-2

Source: https://habr.com/ru/post/1776570/


All Articles