ใน Java 4 วิธีในการตรวจสอบว่า Array มีค่าเฉพาะหรือไม่ IntStream, Arrays.asList (อัลกอริทึมการค้นหาเชิงเส้น)
เผยแพร่แล้ว: 2019-09-11
โปรแกรม Java สำหรับอัลกอริธึมการค้นหาเชิงเส้น
นี่เป็นโซลูชันที่ง่ายและสมบูรณ์ที่สุดสำหรับคุณ หากคุณต้องการตรวจสอบว่า ArrayList มีค่าเฉพาะ เช่น String, Integer, Long หรือ Double
ด้วยการผสมผสานระหว่าง Java7 และ Java8 – there are 4 different ways
คุณสามารถดำเนินการตรวจสอบได้
- Legacy String.contains() method
- IntStream ของ
IntStream -> anyMatch()
method - LongStream ของ
LongStream -> anyMatch()
method -
Arrays.asList
() วิธีการ
เราจะพูดถึงและตรวจสอบวิธีการทั้ง 4 วิธีข้างต้นในโปรแกรม Java นี้ บทช่วยสอน Java นี้จะใช้ได้หากคุณมีคำถามด้านล่าง:
- ฉันจะทดสอบว่าอาร์เรย์มีค่าที่แน่นอนได้อย่างไร
- ตรวจสอบว่าอาร์เรย์มีค่าใน java . หรือไม่
- java array มี int
- ตรวจสอบว่าอาร์เรย์มีสตริง javascript
หากคุณสงสัยว่ามีวิธีใดที่จะแทนที่ประกอบด้วย () วิธีการใน Java หรือไม่? จากนั้นทำตามบทช่วยสอนโดยละเอียดเกี่ยวกับวิธีการแทนที่ประกอบด้วย() / findMe() วิธีการด้วยตัวเอง
มาเริ่มต้นใช้งานโปรแกรมด้วยวิธีการทั้งหมด 4 วิธีข้างต้น:
ขั้นตอนที่ 1
มาทำความเข้าใจตรรกะของเราก่อนและสิ่งที่เราจะทำในบทช่วยสอน Java นี้
- สร้างคลาส Java
CrunchifyCheckIfContains
.java - สร้าง int[]
crunchifyIntArray
ด้วย 15 องค์ประกอบในนั้น - พิมพ์ (system.out.println) ทั้งหมด 15 องค์ประกอบ
- จากนั้นทดสอบ-1: รับค่าสุ่ม 5 ค่าแล้วตรวจสอบ
- หากมีค่าเราจะพิมพ์
Matched
- หากค่าไม่ตรงกันให้พิมพ์
No Match
- หากมีค่าเราจะพิมพ์
- Test-2: ทำการตรวจสอบเดียวกันกับ Java8 Utility โดยใช้ IntStream -> anyMatch()
- การทดสอบ-3: สร้าง
crunchifyLongArray
ด้วยองค์ประกอบ 15 รายการและดำเนินการตรวจสอบด้วยยูทิลิตี้ Java8 โดยใช้ LongStream -> anyMatch() - การทดสอบที่ 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
รันโปรแกรมใน Eclipse โดยคลิกขวาที่โปรแกรมแล้วคลิก Run as Java Application คุณควรเห็นผลลัพธ์ของคอนโซลคล้ายกับสิ่งนี้:

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 |
แจ้งให้เราทราบหากคุณมีคำถามใดๆ