Contents
You đang tìm kiếm từ khóa Compare each element in List Java được Cập Nhật vào lúc : 2022-03-13 05:03:20 . Với phương châm chia sẻ Thủ Thuật về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi Read nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.
Comparing two ArrayLists could be a tricky task for beginner programmers. On this page, we are going to share some of the best ways to compare two ArrayLists.
Nội dung chính
You can easily compare the two ArrayLists by simply using the equals() method. This method returns a boolean value(True or False). So, if the result is true, the ArrayLists are equal; otherwise, they are not equal.
import java.util.ArrayList;
public class ComparingArrayLists //using equals()
public static void main(String[] args)
//initializing two array lists
ArrayList list1 = new ArrayList ();
ArrayList list2 = new ArrayList ();
//storing data in list 1
list1.add(“Java”);
list1.add(“Python”);
list1.add(“C++”);
//storing data in list 2
list2.add(“Java”);
list2.add(“Python”);
list2.add(“C++”);
//printing both lists
System.out.println(“List 1:” + list1);
System.out.println(“List 2:” + list2);
//comparing both lists
if (list1.equals(list2))
System.out.println(“Lists are equal”);
else
System.out.println(“Lists are not equal”);
//adding another element to list 1
list1.add(“HTML”);
//comparing both lists again
if (list1.equals(list2))
System.out.println(“Lists are equal”);
else
System.out.println(“Lists are not equal”);
run:
List 1:[Java, Python, C++]
List 2:[Java, Python, C++]
Lists are equal
Lists are not equal
BUILD SUCCESSFUL (total time: 5 seconds)
In case you want to compare two ArrayLists with respect to common elements, then you should be trying retainAll() method. It will return all common elements between two ArrayLists.
import java.util.ArrayList;
public class ComparingArrayLists //using retainAll()
public static void main(String[] args)
//initializing two array lists
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
//storing data in list 1
list1.add(“Mike”);
list1.add(“Sara”);
list1.add(“John”);
//storing data in list 2
list2.add(“Mike”);
list2.add(“Sara”);
list2.add(“Diaz”);
list2.add(“Sam”);
//printing both lists
System.out.println(“List 1:” + list1);
System.out.println(“List 2:” + list2);
//it will return common elements
list2.retainAll(list1);
//printing common elements
System.out.println(“Common elements are: ” + list2);
run:
List 1:[Mike, Sara, John]
List 2:[Mike, Sara, Diaz, Sam]
Common elements are: [Mike, Sara]
BUILD SUCCESSFUL (total time: 14 seconds)
An alternate way of retainAll() is to compare the elements of the ArrayLists using contains() method. The contains() method checks whether List 1 has the same element as List 2 the same index.
import java.util.ArrayList;
import java.util.Arrays;
public class ComparingArrayLists //using contains()
public static void main(String[] args)
//initializing two array lists
ArrayList list1 = new ArrayList(Arrays.asList(“Apple”, “Banana”, “Peach”, “Apricot”));
ArrayList list2 = new ArrayList(Arrays.asList(“Cabbage”, “Carrots”, “Cucumber”, “Banana”));
//printing both lists
System.out.println(“List 1:” + list1);
System.out.println(“List 2:” + list2);
//Finding similar items in both lists
int i=0;
while(i<list1.size() && i<list2.size())
if(list2.contains(list1.get(i)))
System.out.println(list1.get(i) + " is in both lists");
i++;
run:
List 1:[Apple, Banana, Peach, Apricot]
List 2:[Cabbage, Carrots, Cucumber, Banana]
Banana is in both lists
BUILD SUCCESSFUL (total time: 7 seconds)
Happy Learning ?
Ok, I’m getting a little confused on how to do this. Basically i got N stacks of cards. And need to check the topCard of every stack and check that N – 1 cards match then remove them.
I peeked every stack of cards to get the top card, and added them to an arraylist. But I’m not sure how to go through the arraylist to check that n-1 match or not. (Each card has a getCard method which returns the number then suite as a String (2 Hearts or Ace Diamonds).
(both suite and number have to match)
I’m just getting a bit confused on the best/most efficient way of implementing this.
java
Recommended Answers
There’s a for loop in Java that lets you go over each element in a collection. It looks something like this:
for ( : )
// do something here with
A more concrete example. Say you have an ArrayList called myList, and it stores data of type String. The for loop would look like this:
for (String str : myList) // you could call the variable something other than str if you want
// do something here with str
Let me get this straight – you have an array list consisting of N cards and you want to see that each one of the cards is different?
Yeah i understand while, if and for loops. But im stuck on how to compare the elements within the arraylist. I can’t compare the next element with the last, as they may be different.
EG:
2 Hearts
3 Spade
2 Hearts
2 Hears
If i compared to last element, it would only get to the end and find 2 matching cards( not the 3 it needs (N -1 (4 – 1)))
Let me get this straight – you have an array list consisting of N cards and you want to see that each one of the cards is different?
No, i need to check whether N – 1 of the cards match. If theres 4 stack, i need to check if 3 are identical cards, if they are remove them. There is any number of stacks (N)
To get the element from ArrayList, use get(index) and you will get the card object. Then you can use getCard() from it to obtain the String for comparison. Is that what you are looking for?
Basically you don’t need to compare all of the elements in the array – if you start with card type A, you iterate over the array cells and you are allowed to only once encounter a different card. If you encounter a different card more than once then there is no way that N-1 cards will be equal, and you return false. Just keep two counters for two different card types.
Basically you don’t need to compare all of the elements in the array – if you start with card type A, you iterate over the array cells and you are allowed to only once encounter a different card. If you encounter a different card more than once then there is no way that N-1 cards will be equal, and you return false. Just keep two counters for two different card types.
Ahhh, I think that is a great way of doing it, i will try to implement it soon and see if it works!
ok, heres my attempt, but i don’t think its gr8. Also, using this method, once i’ve found that N-1 do match, how can i remove just the matching ones from the arraylist and the stack they orginated from?
private boolean compareCards()
Iterator it = topCards.iterator();
String last = “”;
int i = 0;
while(i != 2)
while(it.hasNext())
last = it.next().toString();
if(it.next()!= last)
i++;
if(i <= 2)
return false;
else
return true;
In a bit of a hurry but I hope this will give you some idea what to do. It includes some cases that you have missed, and let you know which is the type to remove.
private boolean compareCards()
Iterator it = topCards.iterator();
String type1 = null, type2 = null, current;
int numCards, counter1, counter2;
if(it.hasNext())
type1 = it.next();
++counter1;
++numCards;
while(it.hasNext() && !(counter1 > 1 && counter2 > 1))
current = it.next().toString();
if(current.equals(type1))
++counter1;
else
if(type2 == null)
type2 = current;
++counter2;
++numCards;
if(counter1 > 1 && counter2 > 1)
return false;
else if(counter1 <= 1)
//you know that the type is type2, you need to remove type2 from the array and the stack.
return true;
else
//you know that the type is type1, you need to remove type1 from the array and the stack.
return true;
I will come back later today and see if I have some better idea. Hope that will help you for now.
Ok thanks for your help. Just to let you know, theres not just two types of cards, there are 52 cards in a deck, so 52 different types. But N number of cards will be compared. At one time, there could be N different cards on top.
It’s quite complicated and has got me pulling my hair out 😛
How about looping thru the top cards (no need for the ArrayList) and building a HashTable showing the number of occurrences of each card, ie
for each top card:
if hashtable’s keys contain this card, increment its counter
else add a new entry (card, 1)
Then you know exactly how many of each card you’ve got.
If the count is right, remove all occurrences of that card from the stacks
Yeah that seems like a great idea, a lot more efficient than using an arraylist then iterating through that. I’ll give it a go and post back.
Ok thanks for your help. Just to let you know, theres not just two types of cards, there are 52 cards in a deck, so 52 different types. But N number of cards will be compared. At one time, there could be N different cards on top.
It’s quite complicated and has got me pulling my hair out 😛
I know that there are 52 different types, but if you have more than two types there, than it’s false. You only need to check whether the first type appears N-1 times, or the second one. If a third type appears – you automatically return false.
Oh yeah i see what you are saying. You seem to understand the problem better than me, haha. Going to try using a hashtable as it seems simplier. However, do you think its best to put the Card objects in the table or a string representation of the card (ie. 2 Hearts) Which would be best, because only thing different in the card objects is the number and suite value. So maybe it is best to use a String value, what do you think?
I’m an OO purist. I’d put the Card every time. I’d also write an equals(Card otherCard) method in the Card class and not rely on the other code knowing about how the Strings work.
apines has a good point – you can incorporate that by aborting if the hashtable size goes >2.
I’m an OO purist. I’d put the Card every time. I’d also write an equals(Card otherCard) method in the Card class and not rely on the other code knowing about how the Strings work.
apines has a good point – you can incorporate that by aborting if the hashtable size goes >2.
Was in the middle of writing the same thing 🙂 Override the equals method is the best practice here in my opinion.
ok Thanks for your help, so how do i update the integer number in the hashtable?
Also should i use containsKey instead of contains? as I’m checking the key?
ok Thanks for your help, so how do i update the integer number in the hashtable?
The Put method with the same key suppose to do this for you, and return the previous value. So get the current value, increment it, and put it back to the table.
Also should i use containsKey instead of contains? as I’m checking the key?
Seems like the correct choice.
Ok, ive implemented abit of this, tell me what you think so far, am i doing it correctly?
public void checkMatch()
for(CardHolder h: holders)
if(topCards.containsKey(h.topCard()))
Integer i = (Integer) topCards.get(h.topCard());
topCards.put(h.topCard(),i++);
else
topCards.put(h.topCard(), 1);
if(topCards.size() <= 2)
for(CardHolder h: holders)
player.add(h.takeTopCard());
I know the last if statement isn’t finished yet, need to check the hashmap for keys with values more than 1, and just remove those from the cardHolders.
public void checkMatch()
for(CardHolder h: holders)
if(topCards.containsKey(h.topCard()))
Integer i = (Integer) topCards.get(h.topCard());
topCards.put(h.topCard(),i++);
else
topCards.put(h.topCard(), 1);
if(topCards.size() <= 2)
for(CardHolder h: holders)
player.add(h.takeTopCard());
I know the last if statement isn’t finished yet, need to check the hashmap for keys with values more than 1, and just remove those from the cardHolders.
You can insert the if statement into loop, terminating the loop when the statement is true. Other than that it seems that the usage of the table is correct. I am not certain what you are trying to do inside the if, but I guess this is part of what your program needs to do.
You know – the best way to be certain that you are correct is to build a small test class for it.
Ok, just had a few days break and come back to this and looking it, I’m not sure how i can get this working. I’m a litle confused by what i wrote. It isn’t working, but I’m not sure where, am i even doing it write?
public void checkMatch()
for(CardHolder h: holders)
if(topCards.size() <= 2) //if
for(int n=0; n 1)
topCards.get(n);
player.add(h.takeTopCard());
System.out.println(“Matcha!”);
else if(topCards.containsKey(h.topCard()))
Integer i = (Integer) topCards.get(h.topCard());
topCards.put(h.topCard(),i++);
else
topCards.put(h.topCard(), 1);
topCards.clear();
Try and explain the changes from your last code. The previous code that you have posted seemed more clear…
Yeah, did some testing and that last method was silly. Ok here goes:
public void checkMatch()
for(CardHolder h: holders)
System.out.println(“Checking stack”);
if(topCards.containsKey(h.topCard())) //else no match add card(if contained update)
Integer i = (Integer) topCards.get(h.topCard());
topCards.put(h.topCard(),i++);
System.out.println(“update existing card”);
else //first time card added, add and put value as 1
topCards.put(h.topCard(), 1);
System.out.println(“adding new card”);
if(topCards.size() <= 2) //matcha has been found if true
System.out.println("matched");
for(CardHolder h: holders)
for(int n=0; n 1)
topCards.get(n);
player.add(h.takeTopCard());
System.out.println(“Matcha!”);
System.out.println(“clearing hashtable”);
topCards.clear();
Right, as far as testing. I’ve managed to get it almost working. Heres my output:
Card Holder 1: 10 Clubs Card Holder 2: 2 Diamonds Card Holder 3: 2 Diamonds
Checking stack
adding new card
Checking stack
adding new card
Checking stack
update existing card
matched
clearing hashtable
Card Holder 1: 10 Clubs Card Holder 2: 2 Diamonds Card Holder 3: 2 Diamonds
So it finds that 2 diamonds was found in the hashtable and updated it. Then it was found that a matched state occurreed (2, 2 of diamonds) however, the cards and not being removed (pop’d) from the stack. Not sure why not.
The fact that topCards.size() 2 then for certain you don’t have a match.
ok, so like that?
public void checkMatch()
for(CardHolder h: holders)
System.out.println(“Checking stack”);
if(topCards.containsKey(h.topCard())) //else no match add card(if contained update)
Integer i = (Integer) topCards.get(h.topCard());
topCards.put(h.topCard(),i++);
System.out.println(“update existing card”);
else //first time card added, add and put value as 1
topCards.put(h.topCard(), 1);
System.out.println(“adding new card”);
if(topCards.size() > 2) //matcha has been found if not true
else
System.out.println(“matched”);
for(CardHolder h: holders)
for(int n=0; n 1)
topCards.get(n);
player.add(h.takeTopCard());
System.out.println(“Matcha!”);
System.out.println(“clearing hashtable”);
topCards.clear();
Again – even if the table size is <= 2, it does not necessary means that n-1 of the cards are of the same type – if you have only type1 and type2 cards, when half is type1, the other half is type2 – you will consider it a match when it's not. The condition on the table size should let you break the loop sooner but you cannot rely only on it when considering whether a match has been made.
Ok, so your saying like this, if i understand you correctly that is:
public void checkMatch(){
for(CardHolder h: holders)
System.out.println(“Checking stack”);
if(topCards.size() > 2)
else if(topCards.containsKey(h.topCard())) //else no match add card(if contained update)
Integer i = (Integer) topCards.get(h.topCard());
topCards.put(h.topCard(),i++);
System.out.println(“update existing card”);
else //first time card added, add and put value as 1
topCards.put(h.topCard(), 1);
System.out.println(“adding new card”);
But how am i supposed to check whether a match has been made?
*EDIT*
would a possible way to return the Integer value from the hashtable, then just check if that value equals N-1? that would find the match correctly wouldn’t it?
would a possible way to return the Integer value from the hashtable, then just check if that value equals N-1? that would find the match correctly wouldn’t it?
Yes (that’s what I had in mind when I first suggested HashTable). You can loop thru the keyset checking the corresponding values, something like:
for (Card c : topcards.keySet)
if (topcards.get(c) == (n-1)) // card c appears (n-1) times
://.youtube/watch?v=Ldv1hbBXjt0
Bạn vừa tìm hiểu thêm nội dung bài viết Với Một số hướng dẫn một cách rõ ràng hơn về Clip Compare each element in List Java tiên tiến và phát triển nhất
Pro đang tìm một số trong những ShareLink Download Compare each element in List Java miễn phí.
Nếu sau khi đọc nội dung bài viết Compare each element in List Java vẫn chưa hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Admin lý giải và hướng dẫn lại nha
#Compare #element #List #Java
Tra Cứu Mã Số Thuế MST KHƯƠNG VĂN THUẤN Của Ai, Công Ty Doanh Nghiệp…
Các bạn cho mình hỏi với tự nhiên trong ĐT mình gần đây có Sim…
Thủ Thuật về Nhận định về nét trẻ trung trong môi trường tự nhiên vạn…
Thủ Thuật về dooshku là gì - Nghĩa của từ dooshku -Thủ Thuật Mới 2022…
Kinh Nghiệm Hướng dẫn Tìm 4 số hạng liên tục của một cấp số cộng…
Mẹo Hướng dẫn Em hãy cho biết thêm thêm nếu đèn huỳnh quang không còn…