Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Michael Nitschinger
    @daschl
    initialize it with an empty Map (or set) and then on every call you get access to it and add it
    then it will be emitted
    Javier Domingo Cansino
    @txomon
    hmmm ok
    Michael Nitschinger
    @daschl
    check out the docs for it.. scan is normally used to add values for example, but since the Map will be stored as state
    its pretty neat
    let me see maybe I can show it quickly
    Javier Domingo Cansino
    @txomon
    I will be using just a list
    Michael Nitschinger
    @daschl
    don’t use a list
    Javier Domingo Cansino
    @txomon
    I don't really need a Map, do I?
    why?
    Michael Nitschinger
    @daschl
    well, you only want the last version of each
    if you append to a list you get an ever growing list (unless you cut it down) of changes
    Javier Domingo Cansino
    @txomon
    yes, so I remove the item and append the new one
    Michael Nitschinger
    @daschl
    I thought you only want the latest version of each contact
    Javier Domingo Cansino
    @txomon
    that way I can show them by update time
    Michael Nitschinger
    @daschl
    try this
        public static void main(String... args) throws Exception {
            Observable
              .interval(1, TimeUnit.SECONDS)
              .map(num -> "user" + (num%5) + "-v" + num)
              .scan(new HashMap<>(), (map, value) -> {
                  String[] info = value.split("-");
                  map.put(info[0], info[1]);
                  return map;
              })
              .filter(map -> !map.isEmpty())
              .toBlocking()
              .forEach(System.out::println);
    Javier Domingo Cansino
    @txomon
        return Observable.concat(
                contactRepository.getContactList().flatMap((a) -> Observable.from(a)),
                contactRepository.getContactUpdates()
        ).scan(
                new ArrayList<Contact>(),
                (list, contact) -> {
                    for(Contact contactInList : list){
                        if(contactInList.getUserId() == contact.getUserId()){
                            list.remove(contactInList);
                        }
                    }
                    list.add(contact);
                    return list;
                }
        );
    Michael Nitschinger
    @daschl
    in my sample code, you’ll see how the map builds up and then refreshes:
    {user0=v0}
    {user1=v1, user0=v0}
    {user1=v1, user2=v2, user0=v0}
    {user1=v1, user2=v2, user0=v0, user3=v3}
    {user1=v1, user2=v2, user0=v0, user3=v3, user4=v4}
    {user1=v1, user2=v2, user0=v5, user3=v3, user4=v4}
    {user1=v6, user2=v2, user0=v5, user3=v3, user4=v4}
    {user1=v6, user2=v7, user0=v5, user3=v3, user4=v4}
    {user1=v6, user2=v7, user0=v5, user3=v8, user4=v4}
    {user1=v6, user2=v7, user0=v5, user3=v8, user4=v9}
    {user1=v6, user2=v7, user0=v10, user3=v8, user4=v9}
    Javier Domingo Cansino
    @txomon
    hmmm
    Michael Nitschinger
    @daschl
    so it always keeps the latest state emitted for the object
    @txomon your for loop has o(N) complexity while the hash replace is just O(1)
    Javier Domingo Cansino
    @txomon
    @daschl yeah, but I need it in order
    Michael Nitschinger
    @daschl
    okay, then maybe use a queue where you can push/pop?
    Javier Domingo Cansino
    @txomon
    but items are not updated in order
    Michael Nitschinger
    @daschl
    so what order do you need?
    Javier Domingo Cansino
    @txomon
    yes, I have just realized I haven't described the app
    Michael Nitschinger
    @daschl
    Note that there is also a TreeMap which sorts the keys by its Comparator
    Javier Domingo Cansino
    @txomon
    the app is meant to be a view of your contacts, where instead of being ordered by alphabetical order or so, it's ordered by the latest updated
    such as the call app in android, which shows the recently contacted ones
    Michael Nitschinger
    @daschl
    @txomon implement the comparable on that property and use a https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
    so your set will be ordered by the update date
    Dorus
    @Dorus
    @txomon This somehow remind me of DynamicData. It's in C# but it also has observable collections, might give you some ideas.
    Javier Domingo Cansino
    @txomon
    oh so instead of ordering them by the Container you use, you order them by a property of the object
    ok
    Michael Nitschinger
    @daschl
    so you get an automatic updating list of contacts which emits a new version of itself on every change!
    Javier Domingo Cansino
    @txomon
    also, @daschl how may I have that hashmap in my business logic?
    Michael Nitschinger
    @daschl
    like in my example above
    instead of your scan use mine, but use the TreeSet
    Javier Domingo Cansino
    @txomon
    hmmm but that doesn't let me create a layer above that...
    Michael Nitschinger
    @daschl
    why?
    Javier Domingo Cansino
    @txomon
    let me think about it
    Javier Domingo Cansino
    @txomon
    so ListViewAdapter needs a list underneath, which I should ping when the dataset is updated
    @daschl but isn't generating a new list...
    ohhhh
    ok
    so I am not really generating a new list each time
    is just that I am getting pinged when it updates