daily pastebin goal
23%
SHARE
TWEET

TryThisToo

JunkPile77 Mar 21st, 2019 (edited) 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class TreeMapArrayList {
  5.  
  6.   public static void main(String[] args) throws FileNotFoundException, IOException {
  7.  
  8.     TreeMap<Integer, ArrayList<Integer>> TM = new TreeMap<Integer, ArrayList<Integer>>();
  9.     TreeMap<Integer, ArrayList<Integer>> TM2 = new TreeMap<Integer, ArrayList<Integer>>();
  10.     BufferedReader br = new BufferedReader(new FileReader("SampleText.txt"));
  11.  
  12.     // [0] is the num, [1] is the value
  13.     // if [0] is not in TreeMap, add to TreeMap
  14.     // with 0 as key, and the [1] as the first value in the treemap
  15.     // else, append the [1] to [0]'s value
  16.  
  17.     String st;
  18.     while ((st = br.readLine()) != null) {
  19.       String[] splitString = st.split("\t");
  20.  
  21.       Integer val0 = Integer.parseInt(splitString[0]);
  22.       Integer val1 = Integer.parseInt(splitString[1]);
  23.  
  24.       // check if exists
  25.       ArrayList<Integer> valList = TM.get(val0);
  26.  
  27.       // if nothing is returned, add it to the map.
  28.       // else, if the value is found in the map, append the val1 to the list
  29.       if (valList == null) {
  30.         valList = new ArrayList<Integer>();
  31.         valList.add(val1);
  32.         TM.put(val0, valList);
  33.       } else {
  34.         valList.add(val1);
  35.       }
  36.     }
  37.  
  38.       //TM.forEach((k, v) -> System.out.println("UserID: " + k + "\nValue: " + v + "\n\n"));
  39.       // populate the second TM; this one will contain UserID: Friend List
  40.       TM.forEach((k,v) -> {
  41.                 TM.forEach((k2,v2) -> {
  42.                   // determine if k and k2 are friends
  43.                     boolean isFriend = (v2.stream().filter(item -> v.contains(item.intValue())).findAny().orElse(null)) == null ? false : true;
  44.                     if(isFriend) {
  45.                       // if in map2, append value to list,
  46.                       // else create entry with k as key, and k2 as first value
  47.                       // in list
  48.                         if(TM2.containsKey(k)) {
  49.                             TM2.get(k).add(k2);
  50.                           } else {
  51.                             ArrayList<Integer> friendList = new ArrayList<Integer>();
  52.                             friendList.add(k2);
  53.                             TM2.put(k, friendList);
  54.                           }
  55.                         }
  56.                       });
  57.                     });
  58.  
  59.       TM2.forEach((k, v) -> System.out.println("UserID: " + k + "\nFriend List: " + v + "\n\n"));
  60.  
  61.       // TM2.forEach((k, v) -> {
  62.       //     ArrayList<Integer> temp = TM2.get(k);
  63.       //     System.out.println("List for Key: " + k);
  64.       //     for (Integer tempVal : temp)
  65.       //     {
  66.       //       System.out.println("Key: "+ k + " Value: " + tempVal);
  67.       //     }
  68.       //     System.out.println("\n");
  69.       // });
  70.  
  71.     }
  72.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top