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 There is a method at Here's an example: Use the If you need to protect the order, you do not have to place any order as and
and or
on the archive with hundreds of strings. Any tips? To avoid confusion I call methods
OR
is a bit unclear. set
it will work in the intersection archive
is a method that will employ the union.
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; }
list
instead of set
. It works in two purposes: O
for a
set
, there is O (1)
included
(N) list
. This helps in the case of intersection. 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. LinkedHashSet
as HashSet
.
Comments
Post a Comment