collections - The Adapter Method examples

When we have one inteface and we need another one, writing an adapter solves the problem.

We want to add the ability to produce a reverse iterator to the default forward iterator, so We can’t override. Instead, we add a method that produces an Iterable object which can then be used in the for-in statement.

For example:

// collections/AdapterMethodIdiom.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// The "Adapter Method" idiom uses for-in
// with additional kinds of Iterables

import java.util.*;

class ReversibleArrayList<T> extends ArrayList<T> {
  ReversibleArrayList(Collection<T> c) {
    super(c);
  }

  public Iterable<T> reversed() { // another interface
    return new Iterable<T>() {
      public Iterator<T> iterator() {
        return new Iterator<T>() {
          int current = size() - 1;

          public boolean hasNext() {
            return current > -1;
          }

          public T next() {
            return get(current--);
          }

          public void remove() { // Not implemented
            throw new UnsupportedOperationException();
          }
        };
      }
    };
  }
}

public class AdapterMethodIdiom {
  public static void main(String[] args) {
    ReversibleArrayList<String> ral =
        new ReversibleArrayList<String>(Arrays.asList("To be or not to be".split(" ")));
    // Grabs the ordinary iterator via iterator():
    for (String s : ral) {
      System.out.print(s + " ");
    }
    System.out.println();
    // Hand it the Iterable of your choice
    for (String s : ral.reversed()) {
      System.out.print(s + " ");
    }
  }
}
/* Output:
To be or not to be
be to not or be To
*/

Using this approach, we add two adapter methods:

// collections/MultiIterableClass.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Adding several Adapter Methods

import java.util.*;

public class MultiIterableClass extends IterableClass {
  public Iterable<String> reversed() {
    return new Iterable<String>() {
      public Iterator<String> iterator() {
        return new Iterator<String>() {
          int current = words.length - 1;

          public boolean hasNext() {
            return current > -1;
          }

          public String next() {
            return words[current--];
          }

          public void remove() { // Not implemented
            throw new UnsupportedOperationException();
          }
        };
      }
    };
  }

  public Iterable<String> randomized() {
    return new Iterable<String>() {
      public Iterator<String> iterator() {
        List<String> shuffled = new ArrayList<String>(Arrays.asList(words));
        Collections.shuffle(shuffled, new Random(47));
        return shuffled.iterator();
      }
    };
  }

  public static void main(String[] args) {
    MultiIterableClass mic = new MultiIterableClass();
    for (String s : mic.reversed()) {
      System.out.print(s + " ");
    }
    System.out.println();
    for (String s : mic.randomized()) {
      System.out.print(s + " ");
    }
    System.out.println();
    for (String s : mic) {
      System.out.print(s + " ");
    }
  }
}
/* Output:
banana-shaped. be to Earth the know we how is that And
is banana-shaped. Earth that how the be And we know to
And that is how we know the Earth to be banana-shaped.
*/

please note blow example. 

// collections/ModifyingArraysAsList.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.*;

public class ModifyingArraysAsList {
  public static void main(String[] args) {
    Random rand = new Random(47);
    Integer[] ia = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    List<Integer> list1 = new ArrayList<>(Arrays.asList(ia));
    System.out.println("Before shuffling: " + list1);
    Collections.shuffle(list1, rand);
    System.out.println("After shuffling: " + list1);
    System.out.println("array: " + Arrays.toString(ia));

    List<Integer> list2 = Arrays.asList(ia); // diff
    System.out.println("Before shuffling: " + list2);
    Collections.shuffle(list2, rand);
    System.out.println("After shuffling: " + list2);
    System.out.println("array: " + Arrays.toString(ia)); // diff, the inital array order changed.
  }
}
/* Output:
Before shuffling: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
After shuffling: [4, 6, 3, 1, 8, 7, 2, 5, 10, 9]
array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Before shuffling: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
After shuffling: [9, 1, 6, 3, 7, 2, 5, 10, 4, 8]
array: [9, 1, 6, 3, 7, 2, 5, 10, 4, 8]
*/

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/AdapterMethodIdiom.java

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/IterableClass.java

4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/MultiIterableClass.java

5. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/Collections.java

6. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/ModifyingArraysAsList.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值