简体   繁体   中英

Java compare elements from an array in an arraylist

I am trying to compare input with values in an arraylist.

public compare(number)

Ex; I have an arraylist:

[100,1102,14051,1024, / 142,1450,15121,1482,/ 141,1912,14924,1001] // the / represents each entry

Each index of the array represents a unique attribute in my program. eg index 0 represents user id , index 1 represents room number etc. If I do arraylist.get(2) it returns the second array (142,1450,15121,1482) .

I'm trying to compare number to the 2nd element in each array. So say I run this compare(1102) , I want it to iterate through each [1] in each array, and return true if there is a match at that index.

So I want it to compare the 'number' (1102) with each 1st index element (1102,1450,1912) and because 1102 is in the arraylist, return true

I've been searching around and couldn't find how to implement this, or word the question in the right way

The Stream API can accomplish this.

public class MyCompare 
{
    private static Optional<Integer> compare(Integer valueToCompare)
    {
        Optional<Integer[]> matchingArray = listToCompare.stream()
                .filter(eachArray -> eachArray[1].equals(valueToCompare))
                .findFirst();

        if(matchingArray.isPresent())
        {
            for(int i = 0; i < listToCompare.size(); i++)
            {
                if(listToCompare.get(i).equals(matchingArray.get()))
                {
                    return Optional.of(i);
                }
            }
        }

        return Optional.empty();
    }

    private static List<Integer[]> listToCompare = new ArrayList<>();

    public static void main(String[] args)
    {
        listToCompare.add(new Integer[] { 100, 1102, 14051, 1024 });
        listToCompare.add(new Integer[] { 142, 1450, 15121, 1482 });
        listToCompare.add(new Integer[] { 141, 1912, 14924, 1001 });

        Optional<Integer> listIndex = compare(1102);
        if(listIndex.isPresent())
        {
            System.out.println("Match found in list index " + listIndex.get());
        }
        else
        {
            System.out.println("No match found");
        }
    }
}

Match found in list index 0

The straight-forward way is to use an enhanced for-loop:

for (int[] items : list) {
    // do your checking in here
}

The somewhat more advanced, but much more elegant way would be to use streams:

list.stream()       // converts the list into a Stream.
    .flatMap(...)   // can map each array in the list to its nth element.
    .anyMatch(...); // returns true if any value in the stream matches the Predicate passed.

Stream API: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

Iterate over your list and compare every second element:

public static boolean compare (int number){
    for( int i = 0; i<arraylist.size(); i++){
         if(number == arraylist.get(i)[1]){
            return true;
          }
     }
    return false;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM