String/Object in Array Testing in Java – using Arrays.asLi

  • 时间:2020-10-07 14:38:56
  • 分类:网络文摘
  • 阅读:94 次

This tutorial presents three ways of testing if a given element (integer, doubles, objects, strings) is in a given Array using Java programming language.

2091E060-120E-4C44-BAA3-7E4E0DF7BD55 String/Object in Array Testing in Java - using Arrays.asList and Arrays.Stream Parallel java

Java

Java’s Naive Implementation of Element (String) In Array Tests using Generic

In Java, if we want to test a given element in array, we can implement a naive version (let’s take String for example):

1
2
3
4
5
6
private static boolean stringInArray1(String str, String[] arr) {
    for (var x: arr) {
        if (x.equals(str)) return true;
    }
    return false;
}
private static boolean stringInArray1(String str, String[] arr) {
    for (var x: arr) {
        if (x.equals(str)) return true;
    }
    return false;
}

If we want to go with generic type, we can of course:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.helloacm;
 
public class Main<T> {
    private boolean inArray(T val, T[] arr) {
        for (T x: arr) {
            if (x.equals(val)) return true;
        }
        return false;
    }
 
    public static void main(String[] args)  {
        var arr = new String[] {"a", "ab", "abc", "abcde"};
        var test = new Main<String>();
        System.out.println(test.inArray(null, arr));
        System.out.println(test.inArray("adfasd", arr));
        System.out.println(test.inArray("a", arr));
    }
}
package com.helloacm;

public class Main<T> {
    private boolean inArray(T val, T[] arr) {
        for (T x: arr) {
            if (x.equals(val)) return true;
        }
        return false;
    }

    public static void main(String[] args)  {
        var arr = new String[] {"a", "ab", "abc", "abcde"};
        var test = new Main<String>();
        System.out.println(test.inArray(null, arr));
        System.out.println(test.inArray("adfasd", arr));
        System.out.println(test.inArray("a", arr));
    }
}

In-Array Test using Arrays.AsList().contains

We can convert the array type to List using Arrays.asList then in this case, we can simply use its contain method:

1
2
3
private boolean stringInArray1(T val, T[] arr) {
    return Arrays.asList(arr).contains(val);
}
private boolean stringInArray1(T val, T[] arr) {
    return Arrays.asList(arr).contains(val);
}

Parallel version of Element-In-Array Tests using Arrays.Stream

Even better, we can convert the array to stream, then make a parallel version of the in-array test. It is cool, and it’s potentially faster!

1
2
3
4
5
private boolean stringInArray1(T val, T[] arr) {
    return Arrays.stream(arr).
        parallel().
        anyMatch(s -> s.equals(val));
}
private boolean stringInArray1(T val, T[] arr) {
    return Arrays.stream(arr).
        parallel().
        anyMatch(s -> s.equals(val));
}

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
静湖公园作文  《青铜葵花》有感300字  儿童的负重最好不要超过体重的20分之3  仓库先后运来两批粮食,如果只有第一批增加20%  小东的爷爷积攒了5000元钱打算存入银行  绿地长24米,宽15米,中间有宽为2米的道路  一群兔子在一块地里拔萝卜  学校组织春游,组了几条船让学生们划  三个人平均分一包糖,没有剩余  商店同时卖出两件上衣,每件售价均为120 
评论列表
添加评论