I have array list like this
ArrayList list = new ArrayList();
list.add("somethingold");
list.add(3);
list.add("somethingnew");
list.add(5);
Now if I print the list output will be like:
[somethingold, 3, somethingnew, 5 ]
Here i want to fetch only integer elements.
I want the output like, if it is an integer put it in some other list, else in one more list.
This is what i want:
[3,5]
[somethingold, somethingnew]
have you tried this?:
if(list.get(0) instanceof Integer) {
// is an integer
} else if (list.get(0) instanceof String) {
// is a String
}
Looping through each element in List
for loop:
for (int i = 0; i < list.size(); i++) {
if (list.get(i) instanceof Integer) {
// do your stuff
}
if (list.get(i) instanceof String) {
// do your stuff
}
}
for-each loop:
for (Object obj: list) {
if (obj instanceof Integer) {
// do your stuff
}
...
}
You can achieve it using instanceof operator.
for (Object object : list)
{
if(object instanceof Integer)
{
System.out.println(object); // Integer
}
else if(object instanceof String)
{
System.out.println(object); // String
}
}
its very simple. Just two lines of code
string[] stringList = list.OfType<string>().ToArray();
Int32[] intList = list.OfType<Int32>().ToArray();
It is recommended that should create typed Collection like List<Integer> or List<String> if you are using java 1.5+.
In case of generic array you can use instanceOf operator to differentiate .
for(Object obj: list){
if(obj instanceOf Ineteger){
...
}else if(obj instanceOf String){
...
}
}
you are trying to go over the JDK API , that was one of the most important reason why Generics were introduced in java, to provide you type safe collections.
And you have no need to worry while adding or fetching the elements(no explicit typecasting required).
A friendly suggestion go by the book.
Thanks
You want [3,5] [somethingold, somethingnew] which looks already like 2 separate lists.
So you could do
class IntegersAndStrings {
private final List<String> strings = new ArrayList<String>();
private final List<Integer> ints = new ArrayList<Integer>();
public void add(int i) {
ints.add(Integer.valueOf(i));
}
public void add(String s) {
strings.add(s);
}
#Override
public String toString() {
return ints.toString() + strings.toString();
}
}
and use it like before
class Main {
public static void main(String[] args) {
IntegersAndStrings list = new IntegersAndStrings();
list.add("somethingold");
list.add(3);
list.add("somethingnew");
list.add(5);
System.out.println(list);
}
}
and the output would be
[3, 5][somethingold, somethingnew]
You have to fetch list of objects by using for-loop, in that you can find out string and integers.
Below example shows how to get the values..
ArrayList list = new ArrayList();
for (Object o : list) {
if (o.getClass().equals(Integer.TYPE)) {
...
}
else if (o.getClass().equals(String.class)) {
...
}
}
Collections.sort(list);
int index = 0;
for (Object obj: list) {
index ++;
if (obj instanceof String) {
break;
}
}
List<Integer> integerList = list.subList(0.index);
List<String> stringList = list.subList(index,list.size());
System.out.println(integerList);
System.out.println(stringList);
Try this, i hope it works
Using guava library you can do it like this:
System.out.println(
Iterators.toString(Iterators.filter(list.iterator(), String.class)));
System.out.println(
Iterators.toString(Iterators.filter(list.iterator(), Integer.class)));
Related
I have two arraylists and I want to remove identical values between these lists, but only one occurrence of this. For example:
ArrayList<Integer> a = new ArrayList<>(Arrays.asList(1,2,3,4,5,6));
ArrayList<Integer> b = new ArrayList<>(Arrays.asList(1,2,3,1,2,3));
With these, function unique(a,b) would return:
[[4,5,6],[1,2,3]]
Assuming you want to return an ArrayList of ArrayLists, you can achieve this using this method:
private static ArrayList<ArrayList<Integer>> unique(ArrayList<Integer> a, ArrayList<Integer> b) {
ArrayList<ArrayList<Integer>> unique = new ArrayList<>();
unique.add(new ArrayList<>());
unique.add(new ArrayList<>());
for (Integer i: a) {
if (!b.contains(i) && !unique.get(0).contains(i)) {
unique.get(0).add(i);
}
}
for (Integer i: b) {
if (a.contains(i) && !unique.get(1).contains(i)) {
unique.get(1).add(i);
}
}
return unique;
}
If you only need to know the unique elements in a bunch of lists:
public static <T> ArrayList getUnique(List<T> ... lists){
Set<T> unique = new HashSet<T>();
for (List<T> eachList : lists){
unique.addAll(eachList);
}
return new ArrayList(unique);
}
And you can call it like this:
List<Integer> unique = getUnique(a,b);
#lmiguelvargasf - I took your function and modified it a little because it turns out your version only works if the numbers are all different already. Here is the new function:
private static ArrayList<ArrayList<Integer>> unique(ArrayList<Integer> a, ArrayList<Integer> b) {
ArrayList<ArrayList<Integer>> unique = new ArrayList<>();
unique.add(new ArrayList<>());
unique.add(new ArrayList<>());
for (Integer i: a) {
if (!b.contains(i)) {
unique.get(0).add(i);
}else{
b.remove(i);
}
}
for (Integer i: b) {
if (!a.contains(i)) {
unique.get(1).add(i);
}else{
b.remove(i);
}
}
return unique;
}
I have an array list that looks like:
[new Class1(), new Class2(), new Class1(), new Class1()]
I want to know the most efficient way to extract the number of instances of Class1 in the array list.
For the above example, I want the answer 3.
I am using Java version "1.7.0_79".
You could iterate through the ArrayList and check using the instanceof operator.
for (Object e : lst)
{
if (e instanceof Car)
{
carCount++;
}
}
if the arraylist is like below,
newClass1 = new Class1();
[newClass1, new Class2(), newClass1, newClass1]
then you can check the frequency like below,
Collections.frequency(arrayList, newClass1);
If you are always adding new instance of Class1 then below will be the solution
[new Class1(), new Class2(), new Class1(), new Class1()]
override equals method in Class1 like below,
#Override
public boolean equals(Object o){
if(!(o instanceof Class1 )){
return false;
}else{
return true;
}
}
then in your test class,
System.out.println(Collections.frequency(array, new Class1()));
You can need to check the class at any given position in the array by using the keyword instanceof
Example:
public static void main(String[] args) {
Number[] myN = new Number[5];
//populate... ignore this if want
for (int i = 0; i < myN.length; i++) {
if (i%2==0) {
myN[i]= new Integer(i);
}else{
myN[i]= new Double(i);
}
}
int classACounter=0;
int classBCounter=0;
//check
for (int i = 0; i < myN.length; i++) {
if (myN[i] instanceof Integer){
System.out.println(" is an int");
classACounter++;
}
if (myN[i] instanceof Double){
System.out.println(" is a double");
classBCounter++;
}
}
System.out.println("There are "+classACounter+" elements of the class A");
System.out.println("There are "+classBCounter+" elements of the class B");
}
Well, You can easily filter them by
result = Iterables.filter(collection, YourClass.java);
then you can apply .size() on the result.
I have Collection<Number> but I needed Collection<Integer>
How can I convert it safety?
And more common issue:
How to convert Collection<Parent> to Collection<Child>
You can try this
Collection<Number> numbers = ...
Collection<Integer> integers = numbers.getClass().newInstance();
for(Number n : numbers) {
integers.add(n == null ? null : n.intValue());
}
Example with Guava :
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
Collection<Number> collection = ...
return Collections2.transform(collection, new Function<Number, Integer>() {
#Nullable
#Override
public Integer apply(#Nullable Number input) {
if(input == null) {
return null;
}
return input.intValue();
}
});
Note : Guava is an external lib, you will need to import it it.
You could do it with something like this convert method -
public static Collection<Integer> convert(Collection<Number> in) {
List<Integer> al = new ArrayList<Integer>();
for (Number n : in) {
if (n != null) {
al.add(n.intValue());
}
}
return al;
}
You can use instanceof operator.Try this
Iterator<Number> itr = collection.iterator();
while(itr.hasNext())
{
Number number = itr.next();
if(number instanceof Integer)
{
Integer value = (Integer)number ;
System.out.print("Integer is "+value );
}
}
For each loop to iterate the collection having any numeric data type and then individually insert converting those numeric data types into Integer one's into another collection.
List<Integer> i = new ArrayList<Integer>();
for (Number n : in){
i.add(n.intValue());
}}
This is a pice of my code :
ArrayList<String> Alist= new ArrayList<String>();
ArrayList<String> Blist= new ArrayList<String>();
Alist.add("gsm");
Alist.add("tablet");
Alist.add("pc");
Alist.add("mouse");
Blist.add("gsm");
Blist.add("something");
Blist.add("pc");
Blist.add("something");
so i have two array list i want to compare all items and check if they are not equal and if they are to print out only the items that are not equal.
so i make something like this:
http://postimage.org/image/adxix2i13/
sorry for the image but i have somekind of bug when i post here a for looop.
and the result is :
not equals..:tablet
not equals..:pc
not equals..:mouse
not equals..:gsm
not equals..:tablet
not equals..:pc
not equals..:mouse
not equals..:gsm
not equals..:tablet
not equals..:pc
not equals..:mouse
not equals..:gsm
not equals..:tablet
i want to print only the 2 that are not equal in the example they are gsm and pc
not equals..:gsm
not equals..:pc
Don't use != to compare strings. Use the equals method :
if (! Blist.get(i).equals(Alist.get(j))
But this wouldn't probably fix your algorithmic problem (which isn't clear at all).
If what you want is know what items are the same at the same position, you could use a simple loop :
int sizeOfTheShortestList = Math.min(Alist.size(), Blist.size());
for (int i=0; i<sizeOfTheShortestList; i++) {
if (Blist.get(i).equals(Alist.get(i))) {
System.out.println("Equals..: " + Blist.get(i));
}
}
If you want to get items that are in both lists, use
for (int i = 0; i < Alist.size(); i++) {
if (Blist.contains(Alist.get(i))) {
System.out.println("Equals..: " + Alist.get(i));
}
}
You can use the RemoveAll(Collection c) on one of the lists, if you happen to know if one list always contains them all.
You could use the following code:
ArrayList<String> Alist = new ArrayList<String>();
ArrayList<String> Blist = new ArrayList<String>();
Alist.add("gsm");
Alist.add("tablet");
Alist.add("pc");
Alist.add("mouse");
Blist.add("gsm");
Blist.add("something");
Blist.add("pc");
Blist.add("something");
for (String a : Alist)
{
for (String b : Blist)
{
if (a.equals(b))
{
System.out.println("Equals " + a);
break;
}
}
}
Output is:
Equals gsm
Equals pc
right now your comparing each element to all of the other ones. Do something like
for (int i = 0; i < Alist.size(); i++) {
if (!Alist.get(i).equals(Blist.get(i)) {
// print what you want
}
}
Thats of course assuming both lists have the same length.
Rather than writing code to manually compare list elements you might consider using Apache Commons Collections.
import org.apache.commons.collections.CollectionUtils;
List listA = ...;
List listB = ...;
Collection intersection = CollectionUtils.intersection(listA, listB);
import java.util.HashSet;
public class CheckSet<T> extends HashSet<T>{
#Override
public boolean add(T e) {
if (contains(e)) {
remove(e);
return true;
} else {
return super.add(e);
}
}
}
Add all elements of both of your lists to a CheckSet intance, and at the end it will only contain the ones not equal.
Here is one way:
public static boolean compare(List<String> first, List<String> second) {
if (first==null && second==null) return true;
if (first!=null && second==null) return false;
if (first==null && second!=null) return false;
if ( first.size()!=second.size() ) return false;
HashMap<String, String> map = new HashMap<String, String>();
for (String str : first) {
map.put(str, str);
}
for (String str : second) {
if ( ! map.containsKey(str) ) {
return false;
}
}
return true;
}
public static void main(String args[] ) throws Exception {
List<String> arrayList1 = new ArrayList<String>();
arrayList1.add("a");
arrayList1.add("b");
arrayList1.add("c");
arrayList1.add("d");
List<String> arrayList2 = new ArrayList<String>();
arrayList2.add("a");
arrayList2.add("b");
arrayList2.add("c");
arrayList2.add("d");
boolean isEqual = false;
if(arrayList1.size() == arrayList2.size()){
List<String> arrayListTemp = new ArrayList<String>();
arrayListTemp.addAll(arrayList1);
arrayListTemp.addAll(arrayList2);
HashSet<Object> hashSet = new HashSet<Object>();
hashSet.addAll(arrayListTemp);
if(hashSet.size() == arrayList1.size() &&
hashSet.size() == arrayList2.size()){
isEqual = true;
}
}
System.out.println(isEqual);
}
we can compare two different size arrayList in java or Android as follow.
ArrayList<String> array1 = new ArrayList<String>();
ArrayList<String> array2 = new ArrayList<String>();
array1.add("1");
array1.add("2");
array1.add("3");
array1.add("4");
array1.add("5");
array1.add("6");
array1.add("7");
array1.add("8");
array2.add("1");
array2.add("2");
array2.add("3");
array2.add("4");
for (int i = 0; i < array1.size(); i++) {
for (int j=0;j<array2.size();j++) {
if (array1.get(i) == array2.get(j)) {
//if match do the needful
} else {
// if not match
}
}
}
import java.util.Arrays;
public class ExampleContains {
public static boolean EligibleState(String state){
String[] cities = new String[]{"Washington", "London", "Paris", "NewYork"};
boolean test = Arrays.asList(cities).contains(state)?true:false;
return test;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(EligibleState("London"));
}
}
In Java I want to convert a nested List which contains at the deepest level a uniform type into an multidimensional array of that type. For example, ArrayList<ArrayList<ArrayList<ArrayList<String>>>> into String[][][][]. I've tried several things and I only can obtain an array of objects like Object[][][][]. For 'simple lists' it seems that Apache Commons Lang does the work but I cannot figure out for nested cases.
Update:
In order to obtain a multidimensional array of Object type I'm using a recursive function so I cannot set the key type using toArray() see excerpt:
// the argument of this function is a (nested) list
public static Object convert(Object object) {
Object[] result = null;
List list = (List) object;
if (list != null) {
Object type = getElementType(list);
if (type instanceof List) {
int size = list.size();
result = new Object[size];
for (int counter = 0; counter < size; counter++) {
Object element = list.get(counter);
result[counter] = (element != null) ? convert(element) : null;
}
} else {
result = list.toArray();
}
}
return result;
}
private static Object getElementType(List list) {
Object result = null;
for (Object element : list) {
if (element != null) {
result = element;
break;
}
}
return result;
}
To create any kind of non-Object array, you need to pass a type key to the toArray method. This is because for generic types (e.g., ArrayList), the type argument is erased (so, at runtime, ArrayList<String> is treated as a plain ArrayList), whereas for arrays, the type is not.
It seems you already have the Object array creation sorted, so with that and the use of the type key, I think you're all sorted! :-)
This is the way that someone suggested to solved for String type. Cast2(List<?>) returns the multidimensional array. It may be generalized to use the class type as parameter. Thank you for your comments.
static int dimension2(Object object) {
int result = 0;
if (object instanceof List<?>) {
result++;
List<?> list = (List<?>) object;
for (Object element : list) {
if (element != null) {
result += dimension2(element);
break;
}
}
}
return result;
}
static Object cast2(List<?> l) {
int dim = dimension2(l);
if (dim == 1) {
return l.toArray(new String[0]);
}
int[] dims = new int[dimension2(l)];
dims[0] = l.size();
Object a = Array.newInstance(String.class, dims);
for (int i = 0; i < l.size(); i++) {
List<?> e = (List<?>) l.get(i);
if (e == null) {
Array.set(a, i, null);
} else if (dimension2(e) > 1) {
Array.set(a, i, cast2(e));
} else {
Array.set(a, i, e.toArray(new String[0]));
}
}
return a;
}
hehe, heres a answer too but i dunno if that really helps:
List<ArrayList<ArrayList<ArrayList<String>>>> x = new ArrayList<ArrayList<ArrayList<ArrayList<String>>>>();
public static void main(String[] args) throws MalformedURLException, IOException, SecurityException, NoSuchFieldException {
Type t = ((ParameterizedType)(jdomTEst.class.getDeclaredField("x").getGenericType())).getActualTypeArguments()[0];
int[] dims = new int[t.toString().split("List").length];
Object finalArray = Array.newInstance(String.class, dims);
System.out.println(finalArray);
}
this prints: [[[[Ljava.lang.String;#4e82701e
looks pretty messy but i love reflections :)
You can use transmorph :
ArrayList<ArrayList<ArrayList<ArrayList<String>>>> arrayList = new ArrayList<ArrayList<ArrayList<ArrayList<String>>>>();
/// populate the list ...
[...]
Transmorph transmorph = new Transmorph(new DefaultConverters());
String[][][][] array = transmorph.convert(arrayList, String[][][][].class);