Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting arrays to linked lists en masse

I’m currently writing a JUnit test that requires a Tree Map as an expected value.

Said TreeMap looks like this:

Map<Integer, LinkedList<String>> expected = new TreeMap<>();

I filled said Tree Map with the data that should return from the Test:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    expected.put(1, (LinkedList<String>) Arrays.asList("18_2_2_3_java.lang.String_", "27_0_0_93_java.sql.Timestamp_", "256_6_6_0_java.lang.String_", "200_6_6_12_java.lang.String_", "8_0_0_12_java.lang.String_", "4_2_2_5_java.lang.Integer_", "5_6_6_5_java.lang.Short_", "23_0_0_93_java.util.Date_", "5_0_0_5_java.lang.Integer_", "35_0_0_12_java.lang.String_", "20_6_6_0_java.lang.String_", "19_0_0_12_java.util.Date_", "12_6_6_12_java.lang.String_", "112_0_0_12_java.lang.String_", "1_0_0_5_java.lang.Short_", "4_0_0_2_java.lang.String_", "7_6_6_12_java.lang.String_", "3_0_0_5_java.lang.Integer_", "0_6_6_5_java.lang.Boolean_", "-1_0_0_2005_byte[]_", "19_6_6_1_java.lang.String_", "3_0_0_4_java.lang.String_", "4365_6_6_93_java.sql.Timestamp_", "0_6_6_5_java.lang.Integer_", "23_6_6_93_java.sql.Timestamp_", "1_250_250_12_java.lang.String_"));
    expected.put(2, (LinkedList<String>) Arrays.asList("2_6_6_12_java.lang.String_", "-1_6_6_2005_java.lang.String_", "20_0_0_12_java.lang.Long_", "25_0_0_12_java.lang.String_", "2_6_6_5_java.lang.Integer_", "500_0_0_12_java.lang.String_", "5_6_6_5_java.lang.Boolean_", "128_6_6_0_java.lang.String_", "512_6_6_12_java.lang.String_", "6_2_2_3_java.math.BigDecimal_", "25_6_6_12_java.lang.String_", "4_6_6_12_java.lang.String_", "19_6_6_0_java.lang.String_", "12_0_0_12_java.lang.String_", "1_6_6_5_java.lang.Boolean_", "2_0_0_5_java.lang.Short_", "50_6_6_12_java.lang.String_", "0_6_6_0_java.lang.Boolean_", "0_0_0_5_java.lang.Boolean_", "-1_6_6_-1_java.lang.String_", "4_0_0_12_java.lang.String_", "10_0_0_4_java.lang.Boolean_", "80_0_0_12_java.lang.String_", "20_8_8_3_java.math.BigDecimal_", "18_3_3_3_java.math.BigDecimal_"));
    expected.put(3, (LinkedList<String>) Arrays.asList("150_0_0_12_java.lang.String_", "0_6_6_0_java.sql.Timestamp_", "0_6_6_0_java.lang.Integer_", "1_0_0_5_java.lang.Boolean_", "32_6_6_12_java.lang.String_", "4_0_0_93_java.sql.Timestamp_", "2_0_0_12_java.lang.String_", "0_6_6_91_java.util.Date_"));
    expected.put(4, (LinkedList<String>) Arrays.asList("1024_6_6_1_java.lang.String_", "-1_6_6_-4_byte[]_", "30_6_6_12_java.lang.String_", "27_6_6_93_java.sql.Timestamp_", "40_0_0_12_java.lang.String_", "2_6_6_5_java.lang.String_", "2_2_2_5_java.lang.Short_", "2_0_0_4_java.lang.Integer_", "2000_6_6_12_java.lang.String_", "120_6_6_12_java.lang.String_", "4365_6_6_93_java.util.Date_", "256_6_6_12_java.lang.String_"));
//There is a lot more but you get the point

As some of you maybe already have noticed; the Lists are wrong.
They have to be Linked Lists.

Eclipse recommended Type Casting the List to a Linked List which is what I tried, but that didn’t work out.

I’m pretty lost on this one.

>Solution :

lance-java’s suggestions would work, but they are quite cumbersome.

I would suggest defining a method like this:

public static LinkedList<String> linkedList(String... values) {
  return new LinkedList<>(List.of(values));
}

and then invoke this in your test:

expected.put(1, linkedList("18_2_2_3_java.lang.String_", "27_0_0_93_java.sql.Timestamp_" /* etc */);
// etc

It’s just a bit neater.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading