arrays - Best way to make conjunctions and disjunctions over a collection in java -


What is the most effective way of creating a and and or On two array list?

// is not coded in JavaHashet & lt; String & gt; First = {"hello", "to", "you"} hashset & lt; String & gt; Second = {"hello", "to", "me"} hashset & lt; String & gt; And = and (first, second) = {"hello", "to"} hashaseet & lt; String & gt; Or I need to apply those two methods (very easy), but I (i.e., second) = {"hello", "to", "you", "me"}

It will need to be done efficiently, because I will do and and or on the archive with hundreds of strings. Any tips? To avoid confusion I call methods and

And Union as the meaning of and OR is a bit unclear.

There is a method at set it will work in the intersection

archive is a method that will employ the union.

Here's an example:

  Public static zero major (string [] args throws exceptions {last set < String & gt; First = new hashet & lt; & Gt; (Arrays.asList ("hello", "to", "you")); Last set & lt; String & gt; Second = new hashset & lt; & Gt; (Arrays.asList ("hello", "to", "me")); System.out.println (intersection (first, second)); System.out.println (association (first, second)); } Set Public Stabilization & lt; String & gt; Intersection (last set & lt; string & gt; first, last set & lt; string & gt; seconds) {last set & lt; String & gt; Copy = new hashset & lt; & Gt; (First); Copy.retainAll (second); Return copy; } Set Public Stabilization & lt; String & gt; Union (last set & lt; string & gt; first, last set & lt; string & gt; seconds) {last set & lt; String & gt; Copy = new hashset & lt; & Gt; (First); Copy.addAll (second); Return copy; }  

Use the list instead of set . It works in two purposes: O

  1. In set , there is O (1) included (N) for a list . This helps in the case of intersection.
  2. set guarantees the uniqueness it helps in the union case. Also keep in mind that I copy the collections before completing operations - as the Java passes in the context of unmodested values, the original collection should be changed. Could.

    If you need to protect the order, you do not have to place any order as LinkedHashSet as HashSet .


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -