CodeGym /Java 博客 /随机的 /Java 中的集合类
John Squirrels
第 41 级
San Francisco

Java 中的集合类

已在 随机的 群组中发布
你好!在过去的几节课中,我们对.的掌握有了很大的进步ArrayList。然而,到目前为止,我们只执行了最简单的操作:移除、插入和显示。当然,这并未涵盖开发人员在使用ArrayList. 还记得关于数组和类的课程Arrays吗?Java 的创建者专门设计了这个类来处理程序员在使用数组时面临的最常见任务。那又怎样ArrayList?当然,有一些需要用它执行的常见任务列表。是不是都在具体的类中实现了,还是每次都得自己写实现?当然,您不需要自己编写所有内容。涉及集合的最常见操作已经在特殊静态Collections类中实现。 收藏类 - 1 在 Java 中,一组数据结构通常被称为集合。数据可以以许多不同的方式存储。到目前为止,我们只研究了ArrayList数据存储在数组中的类。我们稍后会熟悉其他集合。现在,只需要了解该类的Collections设计不仅可以与ArrayList,还有其他类型的集合(因此得名)。那么,Collections在使用 时,班级实际上帮助完成了哪些任务ArrayList?第一个也是最明显的是排序。在关于数组的课程中,我们考虑了一个数字示例。现在我们将考虑一个字符串示例。该类Collections实现了sort()对集合内容进行排序的方法:

public class Main {

   public static void main(java.lang.String[] args) {

       String mercury = new String("Mercury");
       String venus = new String("Venus");
       String earth = new String("Earth");
       String mars = new String("Mars");
       String jupiter = new String("Jupiter");
       String saturn = new String("Saturn");
       String uranus = new String("Uranus");
       String neptune = new String("Neptune");

       ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(mercury, venus, earth, mars,
               jupiter, saturn, uranus, neptune));
       Collections.sort(solarSystem);
       System.out.println(solarSystem);

   }
}
输出: [Earth, Jupiter, Mars, Mercury, Neptune, Saturn, Uranus, Venus] 字符串按字母顺序排序!但是为什么要按字母顺序呢?该类String实际上实现了控制字符串比较方式的逻辑(碰巧是按字母顺序排列的)。对于您自己创建的类,您可以实现自己的比较逻辑,但我们将在其他课程中讨论这一点。该类Collections还可以帮助您找到ArrayList. 这是使用min()max()方法完成的:

public static void main(java.lang.String[] args) {

   ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7));
   System.out.println(Collections.max(numbers));
   System.out.println(Collections.min(numbers));

}
Output: 7 1 当然,这比手动编写代码遍历所有元素并找到最大/最小元素要方便得多:)另一个非常有用的方法是reverse()。如果我们必须“翻转”列表以使元素以相反的顺序排列,我们将如何做?自己编写这样的算法可能不会那么容易:)幸运的是,该reverse()方法已经知道如何实现。假设我们不喜欢该方法按字母顺序对我们的行星进行排序的事实sort(),并且我们想要颠倒它们的顺序:从 Z 到 A:

public class Main {

   public static void main(java.lang.String[] args) {

       String mercury = new String("Mercury");
       String venus = new String("Venus");
       String earth = new String("Earth");
       String mars = new String("Mars");
       String jupiter = new String("Jupiter");
       String saturn = new String("Saturn");
       String uranus = new String("Uranus");
       String neptune = new String("Neptune");

       ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(mercury, venus, earth, mars,
               jupiter, saturn, uranus, neptune));
       Collections.sort(solarSystem);
       Collections.reverse(solarSystem);
       System.out.println(solarSystem);

   }
}
输出: [Venus, Uranus, Saturn, Neptune, Mercury, Mars, Jupiter, Earth] 我们一直在谈论排序,元素的顺序等。 但是如果我们有相反的目标呢? 例如,假设我们正在尝试实现一个宾果游戏。我们将 100 个数字添加到鼓中。它们应该一次一个地出现在屏幕上。第一个划掉他票上所有数字的玩家获胜。使用以下方法很容易实现shuffle()

public class Main {

   public static void main(java.lang.String[] args) {

       ArrayList<Integer> bingoDrum = new ArrayList<>(100);
       for (int i = 1; i <= 100; i++) {

           bingoDrum.add(i);// add the numbers 1 to 100 to the drum
       }

       Collections.shuffle(bingoDrum);// Mix it up
       System.out.println ("Your attention, please! Here are the first 10 numbers from the drum!");
       for (int i = 0; i < 10; i++) {

           System.out.println(bingoDrum.get(i));
       }

   }
}
输出: 请注意!这是鼓的前 10 个数字!32 61 4 81 25 8 66 35 42 71 就这么简单!问题解决了,我们的游戏部分就写好了:) 现在让我们想象一个不同的情况。之前,我们创建了一个solarSystem包含行星的列表。它似乎在所有方面都适合我们,但只有一个:您可以从中删除项目并添加新项目!这显然不是我们期望的行为:太阳系在我们的程序中应该是不变的。该类Collections有一个非常有趣的方法:unmodifiableList()。它从作为参数传递的列表中创建一个不可变列表。您不能在此列表中添加或删除项目。在处理太阳系行星列表时,这正是我们想要的!

public class Main {

   public static void main(java.lang.String[] args) {

       String mercury = new String("Mercury");
       String venus = new String("Venus");
       String earth = new String("Earth");
       String mars = new String("Mars");
       String jupiter = new String("Jupiter");
       String saturn = new String("Saturn");
       String uranus = new String("Uranus");
       String neptune = new String("Neptune");

       List<String> solarSystem = Collections.unmodifiableList(new ArrayList<>(Arrays.asList(mercury, venus, earth, mars,
               jupiter, saturn, uranus, neptune)));
       solarSystem.add("Pluto");// Try to add a new element
   }
}
输出: 线程“main”中的异常 java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Collections.java:1075) at Main.main(Main.java:21) 这是一个错误:你不能添加任何东西solarSystem!此处唯一需要注意的是此方法返回List<>(而不是ArrayList<>),因为此类型对于所有类型的列表都是通用的。另一种很容易发生的常见情况是程序员以错误的顺序添加元素。如果出现这种情况,我们发现水星和海王星混淆了,我们可以使用以下swap()方法纠正这个错误:

public class Main {

   public static void main(java.lang.String[] args) {

       String mercury = new String("Mercury");
       String venus = new String("Venus");
       String earth = new String("Earth");
       String mars = new String("Mars");
       String jupiter = new String("Jupiter");
       String saturn = new String("Saturn");
       String uranus = new String("Uranus");
       String neptune = new String("Neptune");

       ArrayList<String> solarSystem = new ArrayList<>(Arrays.asList(neptune, venus, earth, mars
       , jupiter, saturn, uranus, mercury));// The planets are in the wrong order
       System.out.println(solarSystem);

       Collections.swap(solarSystem, solarSystem.indexOf(mercury), solarSystem.indexOf(neptune));
       System.out.println(solarSystem);

   }
}
我们将swap()我们的列表和需要交换的两个元素的索引传递给方法。请注意,该方法适用于索引,而不适用于引用。所以,这里我们不得不使用ArrayList.indexOf()方法。输出: [Neptune, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Mercury] [Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune] 最后,我们将熟悉一个非常有趣的方法disjoint()。 它检查两个集合是否相交,即它们是否至少有一个相同的元素。如果他们不这样做,则返回 true。他们这样做,然后它返回 false

public class Main {

   public static void main(java.lang.String[] args) {

       String mercury = new String("Mercury");
       String venus = new String("Venus");
       String earth = new String("Earth");
       String mars = new String("Mars");
       String jupiter = new String("Jupiter");
       String saturn = new String("Saturn");
       String uranus = new String("Uranus");
       String neptune = new String("Neptune");

       ArrayList<String> solarSystemPart1 = new ArrayList<>(Arrays.asList(mercury, venus, earth, mars));
       ArrayList<String> solarSystemPart2 = new ArrayList<>(Arrays.asList(jupiter, saturn, uranus, neptune));

       System.out.println(Collections.disjoint(solarSystemPart1, solarSystemPart2));

   }
}
如您所见,我们的两个列表具有完全不同的元素,因此程序输出true。这是一个有趣且非常有用的课程。就像Arrays,它为我们做了很多例行的、乏味的工作,让我们专注于其他事情。
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION