1

Hey guys I am learning and playing with ArrayList, I was able to create a basic ArrayList. I would like to accomplish the following: I was trying different methods but couldn't get it.

        - Get only Integer objects:
        - Get only String Objects
        - Get only even Integer objects
        - Get Integer and Double Objects.
        - Get all String Objects starts with "S".
        - Get all String Objects which contains a.

======================= Here is my program =========================

import java.io.Serializable;
import java.util.*;

public class ArrayListDemo1 {



    public static void main(String[] args) {
        ArrayList al = new ArrayList();

        System.out.println("Before = " + al.size());
        al.add(10);
        al.add(43);
        al.add(32.5);
        al.add(10);
        al.add(null);
        al.add('A');
        al.add("ABC");
        al.add(10.12);
        al.add(true);
        al.add("Hello");
        al.add(600);
        al.add(900);
//        System.out.println(al);
//        System.out.println("After = " + al.size());
//        System.out.println(al instanceof Cloneable); //true
//        System.out.println(al instanceof Serializable); //true
//        System.out.println(al instanceof RandomAccess);
//        System.out.println(al instanceof List);
//        System.out.println(al instanceof Collection);
//        System.out.println(al instanceof Set);
//        ArrayList<Integer> a1 = al;
//        System.out.println(a1);

  
    }
}
5
  • Take a look at Stream.filter, e.g. al.stream().filter(entry -> …).… Commented Jun 23, 2020 at 5:14
  • Note that you are using a raw type. ArrayList expects one generic type argument to be given, but you are providing none. This only compiles because of backward-compatibility. You should provide a type argument. Commented Jun 23, 2020 at 5:18
  • Thanks, do I need to convert my Arraylist or I can just give the above command, could you give me an examples please, as I am searching online for example.... Commented Jun 23, 2020 at 5:21
  • 1
    @MCEmperor I think I wanted ArrayList to be raw, only then I can extract other type of data like strings,Integer... objects? do I make sense? Commented Jun 23, 2020 at 5:22
  • You could declare your list as List<Object> al = new ArrayList<Object>(); Commented Jun 23, 2020 at 5:24

2 Answers 2

1

You could iterate over each Object using for loop and check if it is an Integer or String. Then you could check for all the sub conditions specified.

for (Object obj: al) {
    if (obj instanceof String){
        // this  is string 
    } else if (obj instanceof Integer) {
       // this  is Integer 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Just use instance of to check against the type of the given element. To get the elements of integers for example, try:

for (int i = 0; i < al.size() ; i++){
    if(al.get(i) instanceof Integer){
        System.out.println(al.get(i));
    }
}

Which outputs:

10
43
10
600
900

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.