배열에 특정 값이 포함되어 있는지 확인하는 Java 4 방법? IntStream, Arrays.asList(선형 검색 알고리즘)
게시 됨: 2019-09-11
선형 탐색 알고리즘을 위한 자바 프로그램.
이것은 ArrayList에 String, Integer, Long 또는 Double과 같은 특정 값이 포함되어 있는지 확인하려는 경우 가장 간단하고 완벽한 솔루션입니다.
Java7과 Java8의 조합으로 - 포함 검사를 수행할 수 there are 4 different ways 이 있습니다.
- 레거시 String.contains() 메서드
- Java8의
IntStream -> anyMatch()메서드 - Java8의
LongStream -> anyMatch()메소드 -
Arrays.asList() 메서드
우리는 이 Java 프로그램에서 위의 4가지 방법을 모두 논의하고 확인할 것입니다. 이 자바 튜토리얼은 다음과 같은 질문이 있는 경우 작동합니다.
- 배열에 특정 값이 포함되어 있는지 어떻게 테스트할 수 있습니까?
- 배열에 자바 값이 포함되어 있는지 확인
- 자바 배열에는 int가 포함되어 있습니다.
- 배열에 문자열 자바 스크립트가 포함되어 있는지 확인
궁금한 점이 있으면 Java에서 contains() 메서드를 재정의할 수 있는 방법이 있습니까? 글쎄, 그런 다음 스스로 contains() / findMe() 메서드를 재정의하는 방법에 대한 자세한 자습서를 따르십시오.
위의 4가지 방법을 모두 사용하여 프로그램 구현을 시작해 보겠습니다.
1 단계
먼저 로직과 이 자바 튜토리얼에서 무엇을 할 것인지 이해합시다.
- Java 클래스
CrunchifyCheckIfContains.java 만들기 - 15개 요소가 포함된 int[]
crunchifyIntArray생성 - 15개 요소 모두 인쇄(system.out.println)
- 그런 다음 테스트-1: Random 5 값을 가져오고 확인
- 값에 다음이 포함되어 있으면
Matched를 인쇄합니다. - 값이 일치하지 않으면 일치하지
No Match을 인쇄합니다.
- 값에 다음이 포함되어 있으면
- 테스트-2: IntStream -> anyMatch()를 사용하여 Java8 유틸리티로 동일한 검사 수행
- 테스트-3: 15개 요소로
crunchifyLongArray생성 및 LongStream -> anyMatch()를 사용하여 Java8 유틸리티로 검사 수행 - 테스트-4: Arrays를 사용하여 동일한 포함 검사를 수행 합니다. asList() 메서드
2 단계
Eclipse IDE에서 아래 프로그램 전체를 복사합니다.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
package crunchify . com . tutorials ; import java . util . Arrays ; import java . util . List ; import java . util . stream . IntStream ; import java . util . stream . LongStream ; /** * @author Crunchify.com * version: 1.0 */ public class CrunchifyCheckIfContains { public static void main ( String [ ] args ) { int crunchifyRandomNo ; // Let's create integer array with 15 values in it int [ ] crunchifyIntArray = new int [ 15 ] ; for ( int i = 1 ; i < = 14 ; i ++ ) { crunchifyRandomNo = ( 7 + ( int ) ( Math . random ( ) * ( ( 95 - 7 ) ) ) ) ; crunchifyIntArray [ i ] = crunchifyRandomNo ; } log ( "Here is an array crunchifyIntArray[i] ==> " + Arrays . toString ( crunchifyIntArray ) ) ; log ( "\n" ) ; // Now let's run loop for 10 times to see if my random value is part of int[] crunchifyIntArray log ( "============= Test1: Legacy Java7 Way =============" ) ; for ( int j = 1 ; j < = 5 ; j ++ ) { crunchifyRandomNo = ( 7 + ( int ) ( Math . random ( ) * ( ( 95 - 7 ) ) ) ) ; if ( crunchifyContainsMethod ( crunchifyIntArray , crunchifyRandomNo ) ) { log ( "Matched: " + crunchifyRandomNo + " in array crunchifyIntArray[i]" ) ; } else { log ( "No Match for: " + crunchifyRandomNo ) ; } } log ( "\n" ) ; // this is java8 way to find if Array contains specified value. java8IntStreamLongStreamExample ( crunchifyIntArray ) ; java8ArraysAsListExample ( crunchifyIntArray ) ; } // Java7: Simple Legacy way to check if specified value is there in Array :) public static boolean crunchifyContainsMethod ( int [ ] crunchifyIntArray , int matchedValue ) { for ( int isMatched : crunchifyIntArray ) { if ( isMatched == matchedValue ) { return true ; } } return false ; } // Java8: IntStream -> anyMatch() and LongStream.anyMatch() Example public static void java8IntStreamLongStreamExample ( int [ ] crunchifyIntArray ) { // Intstream: A sequence of primitive int-valued elements supporting sequential and parallel aggregate operations. // anyMatch() returns whether any elements of this stream match the provided predicate. // Now let's run loop for 10 times to see if my random value is part of int[] crunchifyIntArray log ( "============= Test2: Java8 IntStream->anyMatch() Way =============" ) ; for ( int j = 1 ; j < = 5 ; j ++ ) { int crunchifyRandomNo = ( 7 + ( int ) ( Math . random ( ) * ( ( 95 - 7 ) ) ) ) ; if ( IntStream . of ( crunchifyIntArray ) . anyMatch ( myValue - > myValue == crunchifyRandomNo ) ) { log ( "Matched " + crunchifyRandomNo + " in array crunchifyIntArray[i]" ) ; } else { log ( "No Match for: " + crunchifyRandomNo ) ; } } log ( "\n" ) ; long [ ] crunchifyLongArray = new long [ 15 ] ; for ( int i = 1 ; i < = 14 ; i ++ ) { long crunchifyRandomLongNo = ( 7 + ( long ) ( Math . random ( ) * ( ( 95 - 7 ) ) ) ) ; crunchifyLongArray [ i ] = crunchifyRandomLongNo ; } log ( "Here is an array crunchifyLongArray[i] ==> " + Arrays . toString ( crunchifyLongArray ) ) ; log ( "\n============= Test3: Java8 LongStream->anyMatch() Way =============" ) ; for ( int j = 1 ; j < = 5 ; j ++ ) { long crunchifyRandomNo = ( 7 + ( long ) ( Math . random ( ) * ( ( 95 - 7 ) ) ) ) ; if ( LongStream . of ( crunchifyLongArray ) . anyMatch ( myValue - > myValue == crunchifyRandomNo ) ) { log ( "Matched " + crunchifyRandomNo + " in array crunchifyLongArray[i]" ) ; } else { log ( "No Match for: " + crunchifyRandomNo ) ; } } log ( "\n" ) ; } // Java8: Arrays.asList() contains() and containsAll() Example private static void java8ArraysAsListExample ( int [ ] crunchifyIntArray ) { String [ ] crunchifyCompany = new String [ ] { "Facebook" , "Twitter" , "Google" } ; // Convert String Array to List List < String > crunchifyList = Arrays . asList ( crunchifyCompany ) ; log ( "============= Test4: Arrays.asList() contains() and containsAll() Example =============" ) ; if ( crunchifyList . contains ( "Twitter" ) ) { log ( "Matched Found for Twitter" ) ; } if ( crunchifyList . contains ( "Twitter" ) | | crunchifyList . contains ( "Facebook" ) ) { log ( "Matched Found for Twitter and Facebook" ) ; } // A and B if ( crunchifyList . containsAll ( Arrays . asList ( "Google" , "Facebook" ) ) ) { log ( "Matched Found for Google and Facebook" ) ; } } // Simple log utility private static void log ( String string ) { System . out . println ( string ) ; } } |
3단계
프로그램을 마우스 오른쪽 버튼으로 클릭하고 Java 응용 프로그램으로 실행을 클릭하여 Eclipse에서 프로그램을 실행합니다. 다음과 유사한 콘솔 결과가 표시되어야 합니다.

|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
https : //crunchify.com/how-to-find-alexa-rank-meta-tag-value-for-all-in-one-webmaster-premium/comment-page-1/#comment-59190 Here is an array crunchifyIntArray [ i ] == > [ 0 , 72 , 26 , 87 , 12 , 45 , 27 , 34 , 12 , 89 , 9 , 65 , 79 , 15 , 16 ] ============= Test1 : Legacy Java7 Way ============= No Match for : 32 No Match for : 33 Matched 79 in array crunchifyIntArray [ i ] No Match for : 40 Matched 12 in array crunchifyIntArray [ i ] ============= Test2 : Java8 IntStream - > anyMatch ( ) Way ============= Matched 34 in array crunchifyIntArray [ i ] No Match for : 58 No Match for : 62 Matched 27 in array crunchifyIntArray [ i ] No Match for : 17 Here is an array crunchifyLongArray [ i ] == > [ 0 , 74 , 93 , 61 , 78 , 42 , 74 , 52 , 59 , 88 , 61 , 34 , 27 , 59 , 84 ] ============= Test3 : Java8 LongStream - > anyMatch ( ) Way ============= No Match for : 15 Matched 84 in array crunchifyLongArray [ i ] No Match for : 23 No Match for : 39 No Match for : 69 ============= Test4 : Arrays . asList ( ) contains ( ) and containsAll ( ) Example ============= Matched Found for Twitter Matched Found for Twitter and Facebook Matched Found for Google and Facebook |
질문이 있으면 알려주세요.
