javaset中保存自定义对象 java定义set数组_字符串


java set与list

Java programming language provides the Set structure which implements array or list like collections. It is an unordered collection of objects where duplicate values cannot be stored. Objects can be generally string, integer, or complex objects like classes. HashSet, LinkedHashSet, and TreeSet structures implement the Set as base or ancestor.

Java编程语言提供了Set结构,该结构实现数组或类似集合的列表。 它是无法存储重复值的对象的无序集合。 对象通常可以是字符串,整数或类之类的复杂对象。 HashSet,LinkedHashSet和TreeSet结构将Set实施为基础或祖先。

(Create Set)

We will start by creating a set. We need to import the java.util.* in order to use Set. We will create a string set that can be added with add() function.

我们将从创建一个集合开始。 我们需要导入java.util.*才能使用Set。 我们将创建一个可以通过add()函数add()的字符串集。


javaset中保存自定义对象 java定义set数组_列表_02

Create Set 创建集

// Set Examples In Java Programming Language 
import java.util.*;
public class Set_Example
{
        public static void main(String[] args)
        {
                // Set deonstration using HashSet 
                Set<String> mySet = new HashSet<String>();
                mySet.add("Poftut");
                mySet.add("İsmail");
                mySet.add("Ahmet");
                mySet.add("Ali");
                mySet.add("Ali");

                System.out.println(mySet);

        }
}

When we run the code given the added hash elements like below. We can see in the code that Ali is added twice but printed once because duplicate entries cannot be stored as set logic.

当我们运行代码时,如下所示添加了哈希元素。 我们在代码中可以看到, Ali被添加了两次,但被打印了一次,因为重复的条目无法存储为集合逻辑。

$ java Set.java


javaset中保存自定义对象 java定义set数组_python_03

Create Set 创建集

创建整数集(Create Integer Set)

We can create an integer set. In this example, we will use digits but some of the multiple times. Multiply added digits will be stored as single. We need to create Integer sets like below.

我们可以创建一个整数集。 在此示例中,我们将使用数字,但是会多次使用。 相乘的数字将被存储为单个。 我们需要创建如下的Integer集。

// Set Examples In Java Programming Language 
import java.util.*;
public class Set_Example
{
        public static void main(String[] args)
        {
                // Set deonstration using HashSet 
                Set<Integer> mySet = new HashSet<Integer>();
                mySet.add(1);
                mySet.add(1);
                mySet.add(2);
                mySet.add(2);
                mySet.add(3);

                System.out.println(mySet);

        }
}

We have added 1 and 2 multiple times but they are store only one time.

我们已多次添加1和2,但它们仅存储一次。


javaset中保存自定义对象 java定义set数组_列表_04

Create Integer Set 创建整数集

创建一个字符串集(Create a String Set)

We can also create a string set. Actually, we have created a string set in the previous examples but I wanted to express string set explicitly. Keep in mind that we need to set the set type as a string like below.

我们还可以创建一个字符串集。 实际上,我们在前面的示例中创建了一个字符串集,但是我想显式地表达字符串集。 请记住,我们需要将设置类型设置为如下所示的字符串。

// Set Examples In Java Programming Language 
import java.util.*;
public class Set_Example
{
   public static void main(String[] args)
   {
      // Set deonstration using HashSet 
      Set<String> mySet = new HashSet<String>();
      mySet.add("Poftut");
      mySet.add("İsmail");
      mySet.add("Ahmet");
      mySet.add("Ali"); mySet.add("Ali");

      System.out.println(mySet);

   }
}

(Remove Element From Set)

After adding some elements we may need to remove some of them. We can use the remove() function by providing the object like string, integer, etc. In this example, we will remove the element Ali from the set mySet.

添加一些元素后,我们可能需要删除其中一些元素。 我们可以通过提供对象(例如字符串,整数等remove()来使用remove()函数。在本示例中,我们将从集合mySet删除元素Ali

mySet.remove("Ali");

(Clear Set or Remove All Elements)

We can also remove all elements from the given set with a single function. We can use clear() function which will clear the set. In this example, we will clear all elements of mySet.

我们还可以使用单个函数从给定集中删除所有元素。 我们可以使用clear()函数来清除集合。 在此示例中,我们将清除mySet所有元素。

mySet.clear();

(Get Size of Set)

A set can store a lot of elements. We can get the size of the set or the element count of the set with the size() function like below.

一组可以存储很多元素。 我们可以使用size()函数获得集合的size()或集合的元素计数,如下所示。

mySet.size();

(Union of Set)

Union means joining multiple sets together. Sets cannot hold duplicate items we can union sets by adding them together. This will not create duplicate items in the new union set. We will use the addAll() function and the set we want to add. We will create a set named unionSet and add two sets named mySet1, mySet2.

联合意味着将多个集合连接在一起。 集合不能容纳重复的项目,我们可以通过将集合加在一起来合并集合。 这不会在新的联合集中创建重复的项目。 我们将使用addAll()函数和我们要添加的集合。 我们将创建一个名为unionSet的集合,并添加两个名为unionSetmySet1 mySet2

Set unionSet=new HashSet();

unionSet.addAll(mySet1);

unionSet.addAll(mySet2);

(The Intersection of Two Sets)

The intersection of sets is a very popular and useful function to get common elements of two sets. We can use retainAll() function with the first set and provide the second set. In this example, we will see the intersection of sets mySet1 and mySet2 into a set named intersectionSet.

集的交集是获取两个集的公共元素的非常流行且有用的函数。 我们可以在第一个集合中使用retainAll()函数,并提供第二个集合。 在此示例中,我们将看到集合mySet1和mySet2的交集到名为交集的集合中。

Set intersectionSet=new HashSet(mySet1);

intersectionSet.retainAll(mySet1);

(The Difference Between  Two Sets)

The difference is the elements that do not exist in the other set. We can find the difference from mySet1 from mySet2 by removing all elements of the mySet1 of mySet2 like below.

区别在于其他集合中不存在的元素。 通过删除mySet2的mySet1的所有元素,我们可以从mySet2中找到与mySet1的区别,如下所示。

mySet2.removeAll(mySet1);

(Check If Set Is Empty)

We can check if the set is empty in different ways. But in this case, we will use the size() function if the set is empty. If the size is 0 this means the set is empty.

我们可以用不同的方法检查集合是否为空。 但是在这种情况下,如果集合为空,我们将使用size()函数。 如果大小为0,则表示集合为空。

if(mySet1.size()==0){

   System.out.println("mySet1 is empty");

}

(Check If Set Contains Given Element)

Another important function checks if the given set contains the given element. We can use contains() function by providing the element or object we want to check. This function will return true if the given element or object exists in the given set and return false if not.

另一个重要功能是检查给定集合是否包含给定元素。 我们可以通过提供要检查的元素或对象来使用contains()函数。 如果给定的元素或对象存在于给定的集合中,则此函数将返回true;否则,将返回false。

if(mySet1.contains("Ali"){

   System.out.println("mySet1 contains 'Ali'");

}

(Iterate Over Set)

In Java programming language Set base on Collections. Collections provide an array or list like features for example iteration. We can use iteration in Set too. we can use different loop statements like for, while, foreach, etc.

在Java编程语言中基于集合设置。 集合提供数组或类似列表的功能,例如迭代。 我们也可以在Set中使用迭代。 我们可以使用不同的循环语句,例如for,while,foreach等。

for(Iterator<E> iterator = mySet.iterator(); iterator.hasNext()) {

   E element = iterator.next();

   element.someMethod();

   iterator.remove(element);

}


LEARN MORE  How To Define Array In Programming Languages Java, JavaScript, PHP, C, C++, Python, C#, PowerShell?


了解更多信息如何使用编程语言Java,JavaScript,PHP,C,C ++,Python,C#,PowerShell定义数组?


翻译自: https://www.poftut.com/java-set-tutorial-with-examples/

java set与list