Java Collections.replaceAll Examples

Java Collections.replaceAll - 9 examples found. These are the top rated real world Java examples of java.util.Collections.replaceAll extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
 public static void main(String[] args) {
   ArrayList nums = new ArrayList();
   nums.add(2);
   nums.add(-5);
   nums.add(3);
   nums.add(0);
   // 输出:[2, -5, 3, 0]
   System.out.println(nums);
   // 输出最大元素,将输出3
   System.out.println(Collections.max(nums));
   // 输出最小元素,将输出-5
   System.out.println(Collections.min(nums));
   // 将nums中的0使用1来代替
   Collections.replaceAll(nums, 0, 1);
   // 输出:[2, -5, 3, 1]
   System.out.println(nums);
   // 判断-5 在List集合中出现的次数,返回1
   System.out.println(Collections.frequency(nums, -5));
   // 对nums集合排序
   Collections.sort(nums);
   // 输出:[-5, 1, 2, 3]
   System.out.println(nums);
   // 只有排序后的List集合才可用二分法查询,输出3
   System.out.println(Collections.binarySearch(nums, 3));
 }
Example #2
0
  @Override
  public Movie update(Movie movie) {
    // TODO Auto-generated method stub

    for (Movie mov : movielist) {
      if (mov.getId().equals(movie.getId())) {
        Collections.replaceAll(movielist, mov, movie);
      } else throw new IllegalArgumentException("This movie is not present in the list");
    }
    if (movielist.contains(movie)) return movie;
    else return null;
  }
Example #3
0
    public static int countUniqueValues(Map<String, Integer> map) {
      int cnt = 0;
      ArrayList<Integer> valueList = new ArrayList<Integer>(map.values());
      for (Integer val : valueList) {
        if (val != -Integer.MAX_VALUE) {
          Collections.replaceAll(valueList, val, -Integer.MAX_VALUE);
          cnt++;
        }
      }

      return cnt;
    }
 public void addSubBuild(SubBuild subBuild) {
   String key =
       subBuild
           .getPhaseName()
           .concat(subBuild.getJobName())
           .concat(String.valueOf(subBuild.getBuildNumber()));
   if (subBuildsMap.containsKey(key)) {
     SubBuild e = subBuildsMap.get(key);
     Collections.replaceAll(getSubBuilds(), e, subBuild);
   } else {
     getSubBuilds().add(subBuild);
   }
   subBuildsMap.put(key, subBuild);
 }
  public static void replaceTheValueOfExistingElementInArrayList() {

    ArrayList<String> strings = new ArrayList<String>(200);

    strings.add("a");
    strings.add("b");
    strings.add("c");
    strings.add("d");
    strings.add("b");

    strings.set(2, "z");

    Collections.replaceAll(strings, "b", "z");
  }
 public static void main(String[] args) {
   print(list);
   print(
       "'list' disjoint (Four)?: "
           + Collections.disjoint(list, Collections.singletonList("Four")));
   print("max: " + Collections.max(list));
   print("min: " + Collections.min(list));
   print("max w/ comparator: " + Collections.max(list, String.CASE_INSENSITIVE_ORDER));
   print("min w/ comparator: " + Collections.min(list, String.CASE_INSENSITIVE_ORDER));
   List<String> sublist = Arrays.asList("Four five six".split(" "));
   print("indexOfSubList: " + Collections.indexOfSubList(list, sublist));
   print("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist));
   Collections.replaceAll(list, "one", "Yo");
   print("replaceAll: " + list);
   Collections.reverse(list);
   print("reverse: " + list);
   Collections.rotate(list, 3);
   print("rotate: " + list);
   List<String> source = Arrays.asList("in the matrix".split(" "));
   Collections.copy(list, source);
   print("copy: " + list);
   Collections.swap(list, 0, list.size() - 1);
   print("swap: " + list);
   Collections.shuffle(list, new Random(47));
   print("shuffled: " + list);
   Collections.fill(list, "pop");
   print("fill: " + list);
   print("frequency of 'pop': " + Collections.frequency(list, "pop"));
   List<String> dups = Collections.nCopies(3, "snap");
   print("dups: " + dups);
   print("'list' disjoint 'dups'?: " + Collections.disjoint(list, dups));
   // Getting an old-style Enumeration:
   Enumeration<String> e = Collections.enumeration(dups);
   Vector<String> v = new Vector<String>();
   while (e.hasMoreElements()) v.addElement(e.nextElement());
   // Converting an old-style Vector
   // to a List via an Enumeration:
   ArrayList<String> arrayList = Collections.list(v.elements());
   print("arrayList: " + arrayList);
 }