hello007
hello007

Reputation: 29

Create another ArrayList only containing some value from another ArrayList

I have a array-list which has data from my table. I want to create another array-list only containing integer from 0 to 23. The data has strings, negative numbers. It would be great if someone could give me example of it.

 int col = 2; 
 List values = new ArrayList(table.getRowCount());

 for (int row = 0; row < table.getRowCount(); row++) {
  values.add(table.getValueAt(row, col));
  }

Upvotes: 0

Views: 45

Answers (4)

prasanth
prasanth

Reputation: 3602

You could do the following-
1. Check if it is a String. If it is, then try parsing the string and if it is successful and it is between 0 and 23, then add to a new list.
2. Check if it is an Integer. If it is, then check if it is between 0 and 23, then add to a new list.

List<Object> inputList; // List containing all kinds of objects
List<Integer> newList = new ArrayList<>();

for (Object o: inputList) {
     if (o instanceof String) {
        String s = (String) o;
        try {
            int n = Integer.parseInt(s)
            if (n >= 0 && n <= 23) {
               newList.add(n);
            }
        } catch (NumberFormatException e) {
         System.out.println(s + " is not an integer");
        }
     }
     else if (o instanceof Integer) {
        int n = (Integer)o;
        if (n >= 0 && n <= 23) {
           newList.add(n);
        }
     }
}

Upvotes: 1

Mikhail Antonov
Mikhail Antonov

Reputation: 1367

Given the original list is a list of strings List<String> originalList = new ArrayList<>(); the solution would be:

Java8

List<Integer> filteredList = originalList
            .stream()
            .filter(s -> s.matches("-?[0-9]+")) // filter only strings which contain numbers
            .map(Integer::parseInt) // convert them to integers
            .filter(v -> v >= 0 && v <= 23) // check if it fits the range
            .collect(Collectors.toList());

Java 7

for (String s: originalList) {
    if (s.matches("-?[0-9]+")) {
        int n = Integer.parseInt(s);
        if (n >= 0 && n <= 23) {
            filteredList.add(n);
        }
    }
}

Sample output:

original list: [-10, -9, str, str, str, -5, -4, str, str, -1, 0, 1, 2, str, str, str, str, 7, str, 9]

filtered list: [0, 1, 2, 7, 9]

Upvotes: 1

Misys
Misys

Reputation: 127

List<Object> tableValues = new ArrayList<>(Arrays.asList(
            new Long(100), new ArrayList(), 3, -6, 20, new Float(1.232), 4));
List<Integer> newValues = new ArrayList<>();

for (Object o : tableValues) {
    if (o instanceof Integer) {
        if ((Integer)o >= 0 && (Integer)o <= 23) {
            newValues.add((Integer)o);
        }
    }
}

System.out.println(newValues);

Output is [3, 20, 4]

Upvotes: 0

Devendra Lattu
Devendra Lattu

Reputation: 2812

So you just to copy few items from one list to another and I am assuming they are in sequence.

    //Consider you have this list already
    List<Object> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));

    //This is the new list you want to create
    List<Object> newList = new ArrayList<>();

    int i = 0, count = 5;
    for(Object o : list){
        if(i++ == count) {
            break;
        }
        newList.add(o);
    }
    System.out.println(newList);

Output: newList : [1, 2, 3, 4, 5]

Upvotes: 0

Related Questions