Note that there are some explanatory texts on larger screens.

plurals
  1. POChoosing the Correct Collections
    primarykey
    data
    text
    <p>I have a situation where I have set of Players in a Room and every Player will have set of cards in their hand.</p> <pre><code>HashMap&lt;Integer,ArrayList&lt;Integer&gt;&gt; useridToCardsinTheirHand = new HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;(); </code></pre> <p>Any Player can "Make a Call". I need to check if the Player who has made a 'call' has the least value (least value of the aggregate cards).</p> <p>To Proceed with the above logic I use again an LinkedhashMap</p> <pre><code>private static LinkedhashMap&lt;Integer,Integer&gt; useridTotalRank = new LinkedhashMap&lt;Integer,Integer&gt;(); </code></pre> <p>If there is a tie in the values then there will a priority algorithm carried on.</p> <p>I use hashMap in all the logics.</p> <p>I am facing problems since I am using HashMaps. The Logic flow is clumsy.</p> <p>I am planning to make a redesign.I am going through MultiMap , But still anticipate the similar problem. Can I have suggestions on the kind of Collections to be used.</p> <p>The psuedo code I have tried is ::</p> <pre><code>import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Map.Entry; public class NewCalling { static HashMap&lt;Integer,ArrayList&lt;Integer&gt;&gt; useridToPointsmap = new HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt;(); lamposHAshMap hashmapsort = new lamposHAshMap(); private static HashMap&lt;Integer,Integer&gt; useridTotalRank = new HashMap&lt;Integer,Integer&gt;(); private static int jokercard = 0x27; private static int closedjokercard =0x25 ; private static List&lt;Integer&gt; faceCards = new ArrayList&lt;Integer&gt;(); @SuppressWarnings("unchecked") public static void main(String args[]) { /** * Assuming the calling is made and the Player id is hard Coded */ boolean callingflag = true; int callinguserid = 2; /*************** Preparing the information which will be given by the ********/ ArrayList&lt;Integer&gt; cardsArray1 = new ArrayList&lt;Integer&gt;(); ArrayList&lt;Integer&gt; cardsArray2 = new ArrayList&lt;Integer&gt;(); ArrayList&lt;Integer&gt; cardsArray3 = new ArrayList&lt;Integer&gt;(); ArrayList&lt;Integer&gt; cardsArray4 = new ArrayList&lt;Integer&gt;(); cardsArray1.add(0x01); cardsArray1.add(0x1A); //cardsArray1.add(0x33); cardsArray2.add(0x21); cardsArray2.add(0x03); cardsArray2.add(0x32); cardsArray3.add(0x21); cardsArray3.add(0x03); cardsArray3.add(0x32); cardsArray4.add(0x01); cardsArray4.add(0x02); cardsArray4.add(0x32); cardsArray4.add(0x31); useridToPointsmap.put(1,cardsArray1); useridToPointsmap.put(2,cardsArray2); useridToPointsmap.put(3,cardsArray3); useridToPointsmap.put(4,cardsArray4); faceCards.add(0,10); faceCards.add(1,11); faceCards.add(2,12); faceCards.add(3,13); /*************** Preparing the information which will be given by the ********/ if(callingflag) { int calledUserTp = totalPointsByUserid(callinguserid,jokercard); System.out.println("Total Points of the User Who has made a Call is ::"+calledUserTp); HashMap&lt;Integer,Integer&gt; useridTotalRankMap = totalPointsforEveryUser(jokercard); LinkedHashMap&lt;Integer,Integer&gt; useridTotalRankMapSorted = new LinkedHashMap&lt;Integer, Integer&gt;(); useridTotalRankMapSorted = (LinkedHashMap&lt;Integer, Integer&gt;) sortByComparator(useridTotalRankMap); for(Map.Entry&lt;Integer, Integer&gt; entry :useridTotalRankMapSorted.entrySet()) { System.out.println( entry.getKey() +"----"+entry.getValue()); if(entry.getKey() == callinguserid ) { System.out.println( "GOOD CALL"); break; } } } } /** Gives the Cards Rank **/ static int getCardRank(int Cardhexvalue) { int rank = Cardhexvalue &amp; 15; return rank; } /** Gives the Cards Suit **/ static int getCardSuit(int Cardhexvalue) { int suit = (Cardhexvalue&gt;&gt;4); return suit; } // METHODS REQUIRED private static HashMap&lt;Integer,Integer&gt; totalPointsforEveryUser(int jokerCardVal) { for(Map.Entry&lt;Integer, ArrayList&lt;Integer&gt;&gt; entry :useridToPointsmap.entrySet()) { int sum = 0; int playerId = entry.getKey(); ArrayList&lt;Integer&gt; cardsList = entry.getValue(); for (Integer s : cardsList) { if (getCardRank(s) != getCardRank(jokerCardVal)) { if (faceCards.contains(s)) { sum += 10; } else { sum += getCardRank(s); } } } useridTotalRank.put(playerId, sum); } return useridTotalRank; } private static int totalPointsByUserid(int userId,int jokerCardVal) { ArrayList&lt;Integer&gt; cardsList = useridToPointsmap.get(userId); int sum = 0; for (Integer s : cardsList) { if (getCardRank(s) != getCardRank(jokerCardVal)) { if (faceCards.contains(s)) { sum += 10; } else { sum += getCardRank(s); } } } return sum; } @SuppressWarnings("unchecked") private static Map sortByComparator(Map unsortMap) { List list = new LinkedList(unsortMap.entrySet()); //sort list based on comparator Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getValue()) .compareTo(((Map.Entry) (o2)).getValue()); } }); //put sorted list into map again Map sortedMap = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry)it.next(); sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } private static boolean checkForNoDuplicates(HashMap&lt;Integer, Integer &gt; useridTotalRankMapSorted) { Collection&lt;Integer&gt; valuesList = useridTotalRankMapSorted.values(); Set&lt;Integer&gt; valuesSet = new HashSet&lt;Integer&gt;(useridTotalRankMapSorted.values()); System.out.println("-----"+ valuesList.size() +"----"+valuesSet.size()); if(valuesList.size() == valuesSet.size()) { return true; } else return false; } // Stack public static HashMap getDuplicateValues(HashMap in) { // Clone input HashMap because we're removing stuff from it in = (HashMap)in.clone(); HashMap rval = new HashMap(); Object[] keys = in.keySet().toArray(); // iterate through all keys for(int x=0;x&lt;keys.length;x++) { Object value = in.get(keys[x]); in.remove(keys[x]); // if value is in input HashMap, store it in duplicate HashMap because it has another value if(in.containsValue(value)) { rval.put(keys[x],value); } // if value is in duplicate HashMap, store it also because it HAD another value earlier if(rval.containsValue(value)) { rval.put(keys[x],value); } } return(rval); } public static HashMap&lt;Object, ArrayList&lt;Object&gt;&gt; gettingTheDuplicates(HashMap map) { HashMap&lt;Object, ArrayList&lt;Object&gt;&gt; newMap = new HashMap&lt;Object, ArrayList&lt;Object&gt;&gt;(); Set&lt;Entry&gt; set = map.entrySet(); for(Entry entry : set) { ArrayList list = new ArrayList(); if(newMap.containsKey(entry.getValue())) { list=newMap.get(entry.getValue()); } list.add(entry.getKey()); newMap.put(entry.getValue(), list); } return newMap; } private static void priorityCheck(int playerId1,int playerId2,int jokerCardVal) { int jokerCountPlayerOne = 0; int jokerCountPlayerTwo = 0; int openJokerCountPlayerOne = 0; int openJokerCountPlayerTwo = 0; List&lt;Integer&gt; playerOneCards = useridToPointsmap.get(playerId1); List&lt;Integer&gt; playerTwoCards = useridToPointsmap.get(playerId2); System.out.println("Current game player cards-----"+playerOneCards); System.out.println("Tied game player cards--------"+playerTwoCards); int playerOneCardCount = playerOneCards.size(); int playerTwoCardCount = playerTwoCards.size(); // Hard coded // playerOneCardCount = 4; //playerTwoCardCount = 4; // jokerCountPlayerOne = 1; // jokerCountPlayerTwo = 1; //openJokerCountPlayerOne =1; //openJokerCountPlayerTwo =2; System.out.println("---jokerCardVal---"+jokerCardVal); System.out.println("---playerOneCardCount---playerTwoCardCount"+playerOneCardCount+" "+playerTwoCardCount); if (playerOneCards.contains(jokerCardVal)) { openJokerCountPlayerOne++; } if (playerTwoCards.contains(jokerCardVal)) { openJokerCountPlayerTwo++; } if (playerOneCards.contains(0)) { jokerCountPlayerOne++; } if (playerTwoCards.contains(0)) { jokerCountPlayerTwo++; } if (playerOneCardCount == playerTwoCardCount) { if (jokerCountPlayerOne == jokerCountPlayerTwo) { if (openJokerCountPlayerOne == openJokerCountPlayerTwo) { System.out.println("Still Tie Occurring---------------"); } else { if (openJokerCountPlayerOne &gt; openJokerCountPlayerTwo) { System.out.println("First player has high rank based on open joker"); } else { System.out.println("Second player has high rank based on open joker"); } } } else { if (jokerCountPlayerOne &gt; jokerCountPlayerTwo) { System.out.println("First player has high rank based on joker"); } else { System.out.println("Second player has high rank based on joker"); } } } else { if (playerOneCardCount &lt; playerTwoCardCount) { System.out.println("First player has high rank based on Count"); } else { System.out.println("Second player has high rank based on count"); } } } // New Priority Check private static List&lt;Integer&gt; priorityNew(ArrayList&lt;Integer&gt; tocompare) { // ArrayList to array Integer[] sortedArray = (Integer[]) tocompare.toArray(); bubble_srt(sortedArray,sortedArray.length); List&lt;Integer&gt; mapValuesNew = Arrays.asList(sortedArray); return mapValuesNew; } public static void bubble_srt( Integer a[], int n ){ int i, j,t=0; for(i = 0; i &lt; n; i++){ for(j = 1; j &lt; (n-i); j++){ // System.out.print(" I am in first "+a[j-1]+" "+a[j] +"*********"+whichCardHigher(a[j-1],a[j])); if(WhichuserHigher(a[j-1],a[j])){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; } } } } public static boolean WhichuserHigher(int user1,int user2) { if(getNumberofCards(user1) == getNumberofCards(user1) ) { if(getNumberofJoker(user1) == getNumberofJoker(user2)) { if(getNumberofClosedJoker(user1) == getNumberofClosedJoker(user2)) { System.out.println("Its a Mega Tie"); } else { if(getNumberofClosedJoker(user1) &gt; getNumberofClosedJoker(user2)) { return true; } else { return false; } } } else { if(getNumberofJoker(user1) &gt; getNumberofJoker(user2)) { return true; } else { return false; } } } else { if(getNumberofCards(user1) &gt;getNumberofCards(user2)) { return true; } else { return false; } } return false; } public static int getNumberofCards(int user) { int noOfCards = 0; for(Map.Entry&lt;Integer, ArrayList&lt;Integer&gt;&gt; entry :useridToPointsmap.entrySet()) { if(entry.getKey() == user) { noOfCards = entry.getValue().size(); } } return noOfCards; } public static int getNumberofJoker(int user) { int noOfJokers = 0; int count = 0; for(Map.Entry&lt;Integer, ArrayList&lt;Integer&gt;&gt; entry :useridToPointsmap.entrySet()) { if(entry.getKey() == user) { for(int i= 0 ;i&lt; entry.getValue().size();i++) { if(Integer.parseInt(entry.getValue().toString()) == jokercard) { count ++; } } } } noOfJokers = count; return noOfJokers; } public static int getNumberofClosedJoker(int user) { int noOfClosedJokers = 0; int count = 0; for(Map.Entry&lt;Integer, ArrayList&lt;Integer&gt;&gt; entry :useridToPointsmap.entrySet()) { if(entry.getKey() == user) { for(int i= 0 ;i&lt; entry.getValue().size();i++) { if(Integer.parseInt(entry.getValue().toString()) == closedjokercard) { count ++; } } } } noOfClosedJokers = count; return noOfClosedJokers; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload